stripe: Add `sync_license_ledger...` for RemoteServerBillingSession.

This commit implements the 'sync_license_ledger_if_needed' method
for RemoteServerBillingSession.
This commit is contained in:
Prakhar Pratyush 2023-12-12 14:54:03 +05:30 committed by Tim Abbott
parent 1d515a57cd
commit 4ad03804cf
2 changed files with 28 additions and 2 deletions

View File

@ -3681,7 +3681,29 @@ class RemoteServerBillingSession(BillingSession): # nocoverage
@override @override
def sync_license_ledger_if_needed(self) -> None: def sync_license_ledger_if_needed(self) -> None:
pass last_ledger = self.get_last_ledger_for_automanaged_plan_if_exists()
if last_ledger is None:
return
# New audit logs since last_ledger for the plan was created.
new_audit_logs = (
RemoteRealmAuditLog.objects.filter(
server=self.remote_server,
event_time__gt=last_ledger.event_time,
event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS,
)
.exclude(extra_data={})
.order_by("event_time")
)
current_plan = last_ledger.plan
for audit_log in new_audit_logs:
end_of_cycle_plan = self.update_license_ledger_for_automanaged_plan(
current_plan, audit_log.event_time
)
if end_of_cycle_plan is None:
return
current_plan = end_of_cycle_plan
def stripe_customer_has_credit_card_as_default_payment_method( def stripe_customer_has_credit_card_as_default_payment_method(

View File

@ -968,12 +968,16 @@ def remote_server_post_analytics(
last_audit_log_update=timezone_now() last_audit_log_update=timezone_now()
) )
# Update LicenseLedger using logs in RemoteRealmAuditlog. # Update LicenseLedger for remote_realm customers using logs in RemoteRealmAuditlog.
for remote_realm in remote_realms_set: for remote_realm in remote_realms_set:
if remote_realm: if remote_realm:
billing_session = RemoteRealmBillingSession(remote_realm=remote_realm) billing_session = RemoteRealmBillingSession(remote_realm=remote_realm)
billing_session.sync_license_ledger_if_needed() billing_session.sync_license_ledger_if_needed()
# Update LicenseLedger for remote_server customer using logs in RemoteRealmAuditlog.
remote_server_billing_session = RemoteServerBillingSession(remote_server=server)
remote_server_billing_session.sync_license_ledger_if_needed()
remote_realm_dict: Dict[str, RemoteRealmDictValue] = {} remote_realm_dict: Dict[str, RemoteRealmDictValue] = {}
remote_realms = RemoteRealm.objects.filter(server=server) remote_realms = RemoteRealm.objects.filter(server=server)
for remote_realm in remote_realms: for remote_realm in remote_realms: