Skip to content

Commit

Permalink
feat: Add subscription cache for new organisations (#4587)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan committed Sep 6, 2024
1 parent d669940 commit b2a1899
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
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
)
2 changes: 0 additions & 2 deletions api/tests/unit/organisations/test_unit_organisations_tasks.py
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

0 comments on commit b2a1899

Please sign in to comment.