Skip to content

Commit 03261dd

Browse files
fix(overage-billing): use relative delta and months, instead of days (#5060)
1 parent f2cc990 commit 03261dd

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

api/organisations/tasks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import timedelta
44

55
from app_analytics.influxdb_wrapper import get_current_api_usage
6+
from dateutil.relativedelta import relativedelta
67
from django.conf import settings
78
from django.core.mail import send_mail
89
from django.db.models import F, Max, Q
@@ -154,7 +155,7 @@ def charge_for_api_call_count_overages():
154155

155156
# Get the period where we're interested in any new API usage
156157
# notifications for the relevant billing period (ie, this month).
157-
api_usage_notified_at = now - timedelta(days=30)
158+
api_usage_notified_at = now - relativedelta(months=1)
158159

159160
# Since we're only interested in monthly billed accounts, set a wide
160161
# threshold to catch as many billing periods that could be roughly

api/tests/unit/organisations/test_unit_organisations_tasks.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pytest
77
from core.helpers import get_current_site_url
8+
from dateutil.relativedelta import relativedelta
89
from django.core.mail.message import EmailMultiAlternatives
910
from django.template.loader import render_to_string
1011
from django.utils import timezone
@@ -834,10 +835,19 @@ def test_charge_for_api_call_count_overages_scale_up(
834835
organisation.subscription.subscription_id = "fancy_sub_id23"
835836
organisation.subscription.plan = "scale-up-v2"
836837
organisation.subscription.save()
838+
839+
# In order to cover an edge case found in production use, we make the
840+
# notification date just outside the previous 30 days, because we want
841+
# to make sure that we cover the case where someone with very high usage
842+
# is notified in the first day of their subscription period (in a 31-day
843+
# month).
844+
notification_date = now - (timedelta(days=30) + timedelta(minutes=30))
845+
assert notification_date > now - relativedelta(months=1)
846+
837847
OrganisationAPIUsageNotification.objects.create(
838848
organisation=organisation,
839849
percent_usage=100,
840-
notified_at=now,
850+
notified_at=notification_date,
841851
)
842852

843853
mocker.patch("organisations.chargebee.chargebee.chargebee.Subscription.retrieve")

0 commit comments

Comments
 (0)