From 3e169ebc1518e3a19e239ab8cc975cd8ccdb0bf7 Mon Sep 17 00:00:00 2001 From: Tessa Walsh Date: Tue, 6 May 2025 20:36:58 -0400 Subject: [PATCH] Add API endpoint to check if subscription is activated (#2582) Subscription Management: used check to ensure subscription can be auto-canceled if not activated. --------- Co-authored-by: Ilya Kreymer --- backend/btrixcloud/orgs.py | 11 +++++++++++ backend/btrixcloud/subs.py | 13 +++++++++++++ backend/test/test_org_subs.py | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/backend/btrixcloud/orgs.py b/backend/btrixcloud/orgs.py index ac939391..bbaa790c 100644 --- a/backend/btrixcloud/orgs.py +++ b/backend/btrixcloud/orgs.py @@ -548,6 +548,17 @@ class OrgOps: ) return Organization.from_dict(org_data) if org_data else None + async def is_subscription_activated(self, sub_id: str) -> bool: + """return true if subscription for this org was 'activated', eg. at least + one user has signed up and changed the slug + """ + org_data = await self.orgs.find_one({"subscription.subId": sub_id}) + if not org_data: + return False + + org = Organization.from_dict(org_data) + return len(org.users) > 0 and org.slug != str(org.id) + async def update_custom_storages(self, org: Organization) -> bool: """Update storage on an existing organization""" diff --git a/backend/btrixcloud/subs.py b/backend/btrixcloud/subs.py index fd4a3596..79738f47 100644 --- a/backend/btrixcloud/subs.py +++ b/backend/btrixcloud/subs.py @@ -34,6 +34,7 @@ from .models import ( UserRole, AddedResponseId, UpdatedResponse, + SuccessResponse, PaginatedSubscriptionEventResponse, REASON_CANCELED, ) @@ -392,6 +393,18 @@ def init_subs_api( assert org_ops.router + @app.get( + "/subscriptions/is-activated/{sub_id}", + tags=["subscriptions"], + dependencies=[Depends(user_or_shared_secret_dep)], + response_model=SuccessResponse, + ) + async def is_subscription_activated( + sub_id: str, + ): + result = await org_ops.is_subscription_activated(sub_id) + return {"success": result} + @app.get( "/subscriptions/events", tags=["subscriptions"], diff --git a/backend/test/test_org_subs.py b/backend/test/test_org_subs.py index 9d795d1a..dd21035f 100644 --- a/backend/test/test_org_subs.py +++ b/backend/test/test_org_subs.py @@ -68,6 +68,15 @@ def test_create_sub_org_and_invite_new_user(admin_auth_headers): new_subs_oid = org_id +def test_validate_new_org_not_activated(admin_auth_headers): + r = requests.get( + f"{API_PREFIX}/subscriptions/is-activated/123", + headers=admin_auth_headers, + ) + assert r.status_code == 200 + assert r.json()["success"] is False + + def test_validate_new_org_with_quotas_and_name_is_uid(admin_auth_headers): r = requests.get(f"{API_PREFIX}/orgs/{new_subs_oid}", headers=admin_auth_headers) assert r.status_code == 200 @@ -126,6 +135,15 @@ def test_validate_new_org_with_quotas_and_update_name(admin_auth_headers): assert "subscription" in data +def test_validate_new_org_is_activated(admin_auth_headers): + r = requests.get( + f"{API_PREFIX}/subscriptions/is-activated/123", + headers=admin_auth_headers, + ) + assert r.status_code == 200 + assert r.json()["success"] is True + + def test_create_sub_org_and_invite_existing_user_dupe_sub(admin_auth_headers): r = requests.post( f"{API_PREFIX}/subscriptions/create",