From 4ad03804cf0f81bb9dcb4ec151225b3d1281c61f Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Tue, 12 Dec 2023 14:54:03 +0530 Subject: [PATCH] stripe: Add `sync_license_ledger...` for RemoteServerBillingSession. This commit implements the 'sync_license_ledger_if_needed' method for RemoteServerBillingSession. --- corporate/lib/stripe.py | 24 +++++++++++++++++++++++- zilencer/views.py | 6 +++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 0fb81bf990..951bbcd753 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -3681,7 +3681,29 @@ class RemoteServerBillingSession(BillingSession): # nocoverage @override 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( diff --git a/zilencer/views.py b/zilencer/views.py index ae11a5e58e..1dfda4c3f8 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -968,12 +968,16 @@ def remote_server_post_analytics( 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: if remote_realm: billing_session = RemoteRealmBillingSession(remote_realm=remote_realm) 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_realms = RemoteRealm.objects.filter(server=server) for remote_realm in remote_realms: