Skip to content

Commit

Permalink
fix: Create a check for billing started at in API usage task helper (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Aug 1, 2024
1 parent 4da9069 commit e2853d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/organisations/task_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ def handle_api_usage_notification_for_organisation(organisation: Organisation) -
subscription_cache = organisation.subscription_information_cache
billing_starts_at = subscription_cache.current_billing_term_starts_at

if billing_starts_at is None:
# Since the calling code is a list of many organisations
# log the error and return without raising an exception.
logger.error(
f"Paid organisation {organisation.id} is missing billing_starts_at datetime"
)
return

# Truncate to the closest active month to get start of current period.
month_delta = relativedelta(now, billing_starts_at).months
period_starts_at = relativedelta(months=month_delta) + billing_starts_at
Expand Down
36 changes: 36 additions & 0 deletions api/tests/unit/organisations/test_unit_organisations_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
SCALE_UP,
)
from organisations.subscriptions.xero.metadata import XeroSubscriptionMetadata
from organisations.task_helpers import (
handle_api_usage_notification_for_organisation,
)
from organisations.tasks import (
ALERT_EMAIL_MESSAGE,
ALERT_EMAIL_SUBJECT,
Expand Down Expand Up @@ -244,6 +247,39 @@ def test_send_org_subscription_cancelled_alert(db: None, mocker: MockerFixture)
)


def test_handle_api_usage_notification_for_organisation_when_billing_starts_at_is_none(
organisation: Organisation,
inspecting_handler: logging.Handler,
mocker: MockerFixture,
) -> None:
# Given
api_usage_mock = mocker.patch("organisations.task_helpers.get_current_api_usage")
organisation.subscription.plan = SCALE_UP
organisation.subscription.subscription_id = "fancy_id"
organisation.subscription.save()
OrganisationSubscriptionInformationCache.objects.create(
organisation=organisation,
allowed_seats=10,
allowed_projects=3,
allowed_30d_api_calls=100,
chargebee_email="test@example.com",
current_billing_term_starts_at=None,
current_billing_term_ends_at=None,
)
from organisations.task_helpers import logger

logger.addHandler(inspecting_handler)

# When
handle_api_usage_notification_for_organisation(organisation)

# Then
api_usage_mock.assert_not_called()
assert inspecting_handler.messages == [
f"Paid organisation {organisation.id} is missing billing_starts_at datetime"
]


@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
def test_handle_api_usage_notifications_when_feature_flag_is_off(
mocker: MockerFixture,
Expand Down

0 comments on commit e2853d7

Please sign in to comment.