diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 968d8986fb..3679322709 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -1457,33 +1457,36 @@ class BillingSession(ABC): assert plan.status == CustomerPlan.ACTIVE old_end_date = plan.end_date plan.end_date = new_end_date - old_next_invoice_date = plan.next_invoice_date # Legacy plans should be invoiced once on the end_date to # downgrade or switch to a new tier. + next_invoice_date_changed_extra_data = None if plan.tier == CustomerPlan.TIER_SELF_HOSTED_LEGACY: + next_invoice_date_changed_extra_data = { + "old_value": plan.next_invoice_date, + "new_value": new_end_date, + "property": "next_invoice_date", + } plan.next_invoice_date = new_end_date plan.save(update_fields=["end_date", "next_invoice_date"]) - self.write_to_audit_log( - event_type=AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED, - event_time=timezone_now(), - extra_data={ - "old_value": old_end_date, - "new_value": new_end_date, - "plan_id": plan.id, - "property": "end_date", - }, - ) - if old_next_invoice_date != plan.next_invoice_date: + + def write_to_audit_log_plan_property_changed(extra_data: Dict[str, Any]) -> None: + extra_data["plan_id"] = plan.id self.write_to_audit_log( event_type=AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED, event_time=timezone_now(), - extra_data={ - "old_value": old_next_invoice_date, - "new_value": new_end_date, - "plan_id": plan.id, - "property": "next_invoice_date", - }, + extra_data=extra_data, ) + + end_date_changed_extra_data = { + "old_value": old_end_date, + "new_value": new_end_date, + "property": "end_date", + } + write_to_audit_log_plan_property_changed(end_date_changed_extra_data) + + if next_invoice_date_changed_extra_data: + write_to_audit_log_plan_property_changed(next_invoice_date_changed_extra_data) + return f"Current plan for {self.billing_entity_display_name} updated to end on {end_date_string}." raise SupportRequestError( f"No current plan for {self.billing_entity_display_name}."