From 54ab1174a921c1d37817c3b065d4be7a3f861cca Mon Sep 17 00:00:00 2001 From: Sebastien Mirolo Date: Mon, 6 Jan 2025 10:41:29 -0800 Subject: [PATCH] enables specification of `ends_at` query parameter to balance API This makes it possible to run tests against use charges in the future. --- Makefile | 11 ++++++----- saas/api/transactions.py | 4 +++- saas/models.py | 6 ++++++ testsite/fixtures/160-renewals.json | 6 +++--- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index b365448e..4d81477c 100644 --- a/Makefile +++ b/Makefile @@ -55,10 +55,16 @@ clean:: clean-dbs find $(srcDir) -name '__pycache__' -exec rm -rf {} + find $(srcDir) -name '*~' -exec rm -rf {} + + clean-dbs: [ ! -f $(DB_NAME) ] || rm $(DB_NAME) +doc: + $(installDirs) build/docs + cd $(srcDir) && sphinx-build -b html ./docs $(PWD)/build/docs + + initdb-with-dummydata: initdb cd $(srcDir) && $(MANAGE) load_test_transactions @@ -81,11 +87,6 @@ initdb: clean-dbs testsite/fixtures/170-billing.json -doc: - $(installDirs) build/docs - cd $(srcDir) && sphinx-build -b html ./docs $(PWD)/build/docs - - vendor-assets-prerequisites: $(libDir)/.npm/djaodjin-saas-packages diff --git a/saas/api/transactions.py b/saas/api/transactions.py index dfb24741..83002306 100644 --- a/saas/api/transactions.py +++ b/saas/api/transactions.py @@ -755,8 +755,10 @@ def get_serializer_class(self): def get(self, request, *args, **kwargs): #pylint:disable=attribute-defined-outside-init + at_time = self.ends_at self.balance_amount, self.balance_unit \ - = Transaction.objects.get_statement_balance(self.organization) + = Transaction.objects.get_statement_balance( + self.organization, until=at_time) return super(StatementBalanceAPIView, self).get( request, *args, **kwargs) diff --git a/saas/models.py b/saas/models.py index 86e6647c..e72d0176 100644 --- a/saas/models.py +++ b/saas/models.py @@ -4994,12 +4994,18 @@ def record_use_charge(subscription, use_charge, quantity=1, created_at=None): subscription=subscription, use=use_charge, defaults={ 'expiring_quota': use_charge.quota }) + LOGGER.debug("[record_use_charge] %d units at %s "\ + "- (expiring_quota=%d, rollover_quota=%d)", + quantity, created_at, usage.expiring_quota, usage.rollover_quota) if usage.expiring_quota >= quantity: # We are still below period quota usage.expiring_quota -= quantity else: invoiced_quantity = quantity - usage.expiring_quota usage.expiring_quota = 0 + LOGGER.debug("[record_use_charge] %d units at %s "\ + "- rollover_quota(%d) >= invoiced_quantity(%d)", + quantity, created_at, usage.rollover_quota, invoiced_quantity) if usage.rollover_quota >= invoiced_quantity: # We are still below reserved usage paid in advance usage.rollover_quota -= invoiced_quantity diff --git a/testsite/fixtures/160-renewals.json b/testsite/fixtures/160-renewals.json index cad4d8cb..c511d6c8 100644 --- a/testsite/fixtures/160-renewals.json +++ b/testsite/fixtures/160-renewals.json @@ -4,7 +4,7 @@ "full_name": "Club160", "email": "club+160@localhost.localdomain", "created_at": "2025-01-01T00:00:00+00:00", - "billing_start": "2025-11-09", + "billing_start": "2025-02-01", "processor": 1, "is_provider": 1, "is_active": 1 @@ -13,8 +13,8 @@ }, { "fields": { - "created_at": "2025-09-10T00:00:00+00:00", - "ends_at": "2025-11-09T23:59:59-00:00", + "created_at": "2025-01-01T00:00:00+00:00", + "ends_at": "2025-01-31T23:59:59-00:00", "organization": 160, "plan": 3 },