Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add subscription cache for new organisations #4587

Merged
merged 8 commits into from
Sep 6, 2024
5 changes: 5 additions & 0 deletions api/organisations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ def cancel_subscription(self):
def create_subscription(self):
Subscription.objects.create(organisation=self)

@hook(AFTER_CREATE)
def create_subscription_cache(self):
if is_saas() and not self.has_subscription_information_cache():
OrganisationSubscriptionInformationCache.objects.create(organisation=self)

@hook(AFTER_SAVE)
def clear_environment_caches(self):
from environments.models import Environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def test_get_usage_data__ninety_day_period(
week_from_now = now + timedelta(days=7)
four_weeks_ago = now - timedelta(days=28)
ninety_days_ago = now - timedelta(days=90)

OrganisationSubscriptionInformationCache.objects.create(
organisation=organisation,
current_billing_term_starts_at=four_weeks_ago,
Expand Down
21 changes: 21 additions & 0 deletions api/tests/unit/organisations/test_unit_organisations_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,24 @@ def test_reset_of_api_notifications(organisation: Organisation) -> None:
# Then
assert OrganisationAPIUsageNotification.objects.count() == 1
assert OrganisationAPIUsageNotification.objects.first() == oapiun


def test_organisation_creates_subscription_cache(
db: None, mocker: MockerFixture
) -> None:
# Given
mocker.patch("organisations.models.is_saas", return_value=True)

# When
organisation = Organisation.objects.create(name="Test org")

# Then
assert organisation.subscription_information_cache
assert (
organisation.subscription_information_cache.allowed_seats
== MAX_SEATS_IN_FREE_PLAN
)
assert (
organisation.subscription_information_cache.allowed_30d_api_calls
== MAX_API_CALLS_IN_FREE_PLAN
)
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,6 @@ def test_handle_api_usage_notifications_missing_info_cache(
from organisations.task_helpers import logger

logger.addHandler(inspecting_handler)

assert organisation.has_subscription_information_cache() is False

mock_api_usage = mocker.patch(
Expand Down Expand Up @@ -1264,7 +1263,6 @@ def test_charge_for_api_call_count_overages_with_exception(
from organisations.tasks import logger

logger.addHandler(inspecting_handler)

OrganisationSubscriptionInformationCache.objects.create(
organisation=organisation,
allowed_seats=10,
Expand Down
Loading