From c55e675b023567b38e9750a3a5cc6b0a1859c209 Mon Sep 17 00:00:00 2001 From: Matthew Elwell Date: Thu, 13 Jul 2023 14:56:54 +0100 Subject: [PATCH] fix: (sales dashboard) correct api call overage data (#2434) * Fix api call overage data * Add unit test --- api/organisations/models.py | 5 ++++- api/organisations/tests/test_models.py | 22 +++++++++++++++++++ .../templates/sales_dashboard/home.html | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/api/organisations/models.py b/api/organisations/models.py index 72fd6c780044..8dad0adcae9f 100644 --- a/api/organisations/models.py +++ b/api/organisations/models.py @@ -241,7 +241,10 @@ def add_single_seat(self): def get_api_call_overage(self): subscription_info = self.organisation.subscription_information_cache - return subscription_info.allowed_30d_api_calls - subscription_info.api_calls_30d + overage = ( + subscription_info.api_calls_30d - subscription_info.allowed_30d_api_calls + ) + return overage if overage > 0 else 0 class OrganisationWebhook(AbstractBaseExportableWebhookModel): diff --git a/api/organisations/tests/test_models.py b/api/organisations/tests/test_models.py index 4dce5ecfa8b3..ec3e87305218 100644 --- a/api/organisations/tests/test_models.py +++ b/api/organisations/tests/test_models.py @@ -9,6 +9,7 @@ from organisations.models import ( TRIAL_SUBSCRIPTION_ID, Organisation, + OrganisationSubscriptionInformationCache, Subscription, ) from organisations.subscriptions.constants import ( @@ -367,3 +368,24 @@ def test_organisation_update_clears_environment_caches( # Then mock_environment_cache.delete_many.assert_called_once_with([environment.api_key]) + + +@pytest.mark.parametrize( + "allowed_calls_30d, actual_calls_30d, expected_overage", + ((1000000, 500000, 0), (1000000, 1100000, 100000), (0, 100000, 100000)), +) +def test_subscription_get_api_call_overage( + organisation, subscription, allowed_calls_30d, actual_calls_30d, expected_overage +): + # Given + OrganisationSubscriptionInformationCache.objects.create( + organisation=organisation, + allowed_30d_api_calls=allowed_calls_30d, + api_calls_30d=actual_calls_30d, + ) + + # When + overage = subscription.get_api_call_overage() + + # Then + assert overage == expected_overage diff --git a/api/sales_dashboard/templates/sales_dashboard/home.html b/api/sales_dashboard/templates/sales_dashboard/home.html index fb899cf44655..82b7abf1b10e 100644 --- a/api/sales_dashboard/templates/sales_dashboard/home.html +++ b/api/sales_dashboard/templates/sales_dashboard/home.html @@ -91,7 +91,7 @@

Organisations

{{org.subscription_information_cache.api_calls_24h|intcomma|default:0}} {{org.subscription_information_cache.api_calls_7d|intcomma|default:0}} {{org.subscription_information_cache.api_calls_30d|intcomma|default:0}} - {% if org.subscription.get_api_call_overage > 0 %}{{org.subscription.get_api_call_overage}}{% endif %} + 0 %}class="badge badge-danger"{% endif %}>{{org.subscription.get_api_call_overage|intcomma}} {% endfor %}