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.
This commit is contained in:
Prakhar Pratyush 2024-02-20 12:27:36 +05:30 committed by Tim Abbott
parent 1093ff841f
commit 648d0286bc
1 changed files with 21 additions and 18 deletions

View File

@ -1457,33 +1457,36 @@ class BillingSession(ABC):
assert plan.status == CustomerPlan.ACTIVE assert plan.status == CustomerPlan.ACTIVE
old_end_date = plan.end_date old_end_date = plan.end_date
plan.end_date = new_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 # Legacy plans should be invoiced once on the end_date to
# downgrade or switch to a new tier. # downgrade or switch to a new tier.
next_invoice_date_changed_extra_data = None
if plan.tier == CustomerPlan.TIER_SELF_HOSTED_LEGACY: 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.next_invoice_date = new_end_date
plan.save(update_fields=["end_date", "next_invoice_date"]) plan.save(update_fields=["end_date", "next_invoice_date"])
self.write_to_audit_log(
event_type=AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED, def write_to_audit_log_plan_property_changed(extra_data: Dict[str, Any]) -> None:
event_time=timezone_now(), extra_data["plan_id"] = plan.id
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:
self.write_to_audit_log( self.write_to_audit_log(
event_type=AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED, event_type=AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED,
event_time=timezone_now(), event_time=timezone_now(),
extra_data={ extra_data=extra_data,
"old_value": old_next_invoice_date,
"new_value": new_end_date,
"plan_id": plan.id,
"property": "next_invoice_date",
},
) )
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}." return f"Current plan for {self.billing_entity_display_name} updated to end on {end_date_string}."
raise SupportRequestError( raise SupportRequestError(
f"No current plan for {self.billing_entity_display_name}." f"No current plan for {self.billing_entity_display_name}."