From 648d0286bc3d11cc3587881234b3059b7c336584 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Tue, 20 Feb 2024 12:27:36 +0530 Subject: [PATCH] stripe: Extract repetitive write_to_audit_log code into a function. This is a prep commit which extracts the write_to_audit_log code block in `update_end_date_of_current_plan` into a function named 'write_to_audit_log_plan_property_changed'. This would be helpful to avoid repetition as we plan to include another such block when the plan's property reminder_to_review_plan_email_sent is changed. --- corporate/lib/stripe.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) 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}."