Skip to content

Commit

Permalink
enables specification of ends_at query parameter to balance API
Browse files Browse the repository at this point in the history
This makes it possible to run tests against use charges in the future.
  • Loading branch information
smirolo committed Jan 6, 2025
1 parent f39b4be commit 54ab117
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down
4 changes: 3 additions & 1 deletion saas/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions saas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions testsite/fixtures/160-renewals.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"full_name": "Club160",
"email": "[email protected]",
"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
Expand All @@ -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
},
Expand Down

0 comments on commit 54ab117

Please sign in to comment.