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

Adds Subscription API #1914

Merged
merged 46 commits into from
Jul 11, 2024
Merged

Adds Subscription API #1914

merged 46 commits into from
Jul 11, 2024

Conversation

ikreymer
Copy link
Member

@ikreymer ikreymer commented Jul 6, 2024

Fixes #1905

(Different approach than originally suggested, but accomplishes same ends for tracking subscription history in SaaS setup)

Adds a new top-level /api/subscriptions endpoint and SubOps handler on the backend.

The subscription api supports /api/subscriptions/create, /api/subscriptions/update and /api/subscriptions/cancel.

  • /api/subscriptions/create creates a new org with subscription, extending the work done in Extends Org Create endpont + shared secret auth #1897. The original /api/orgs/create is reverted as before, while this endpoint can be used to create a new org with additional options.

  • /api/subscriptions/update can be used to update subscription data, including status, readOnlyOnCancel and extra details (see below)

  • /api/subscriptions/cancel can be used to cancel a subscription. By default, this will result in the org also be deleted, unless readOnlyOnCancel was previous set.

All subscription events are timestamped and also logged in the MongoDB subscription collection.

All operations can only be accessed via the shared secret auth added in #1897, or with superadmin login.

The Subscription optionally stored on the org in subscription is as follows:

class Subscription(BaseModel):
    """subscription data"""

    subId: str
    status: str
    planId: str

    futureCancelDate: Optional[datetime] = None
    readOnlyOnCancel: bool = False

The subId is used to uniquely identify subscriptions, and must be unique if set (via sparse unique index on the Org).
The planId field is used to identify an arbitrary plan for the subscription.
status and futureCancelDate can be used to display status and cancellation info for the user.
readOnlyOnCancel indicates where the whole Org should be deleted when a subscription is canceled via /api/subscriptions/cancel.

TODOs:

  • new POST /subscriptions/create, /subscriptions/update, /subscriptions/cancel API endpoints
  • Subscriptions mongo collection storing timestamped /subscription API events
  • GET /subscriptions/events API to get subscription events
  • Subscription data model
  • Support for setting and handling readOnlyOnCancel on org
  • /org/subscription info with external API call to get portalUrl
  • subscription in org getter and list views
  • subscription API endpoints available only if billing_enabled is set in helm chart
  • Mark org as readOnly for subscription status paused_payment_failed, clears it on status active

- new subscriptions api which supports /create, /update, /cancel
- move /orgs/create with subscriptions to /subscriptions/create,
keep /orgs/create simple
- store subscription api calls as entries in new subscriptions db, with types 'create', 'update', 'cancel'
- add SubscriptionData type for org.subData, also SubscriptionCreate, SubscriptionUpdate, SubscriptionCancel
- add sparse unique index on org.subData.subId to avoid dupe subs
- tests: move subscription tests to test_org_subs, add test for dupe subscription
- support /subscriptions/update updates SubscriptionData on matching org
- support /subscriptions/cancel delete SubscriptionData on matching org, and deletes the whole org if deleteOnCancel is set to true (the default)
tests: test cancelling subscription with and without readOnlyOnCancel set
collection delete: add option to ignore missing collection
@ikreymer ikreymer requested a review from tw4l July 6, 2024 19:09
@ikreymer ikreymer marked this pull request as ready for review July 9, 2024 07:45
backend/btrixcloud/invites.py Show resolved Hide resolved
backend/btrixcloud/subs.py Outdated Show resolved Hide resolved
backend/btrixcloud/subs.py Outdated Show resolved Hide resolved
backend/btrixcloud/models.py Show resolved Hide resolved
Co-authored-by: Tessa Walsh <tessa@bitarchivist.net>
Copy link
Collaborator

@SuaYoo SuaYoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback based on API docs!

backend/btrixcloud/subs.py Show resolved Hide resolved
backend/btrixcloud/subs.py Outdated Show resolved Hide resolved
status: str
planId: str

futureCancelDate: Optional[datetime]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be any date time format, or UTC?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be datetime UTC, believe it will return an ISO string in UTC.

backend/btrixcloud/subs.py Outdated Show resolved Hide resolved
ikreymer pushed a commit that referenced this pull request Jul 11, 2024
Resolves #1875
Follows #1914

### Changes

- When billing is enabled, adds billing tab to org settings that
displays billing information if applicable
- Handles external link to manage plan
- Refactors org quota type to always be present
- Refactors org settings into `TailwindComponent`
- add secret with echo server URL before starting
- update k3d-ci and microk8s ci
@ikreymer ikreymer merged commit 9a67e28 into main Jul 11, 2024
4 checks passed
@ikreymer ikreymer deleted the subs-api branch July 11, 2024 00:41
ikreymer added a commit that referenced this pull request Aug 5, 2024
- Follow-up to #1914, allows SubscriptionUpdate event to also update
quotas.
- Passes current usage info + current billing page URL to portalUrl
request for external app to be able to respond with best portalUrl
- get_origin() moved to utils to be available more generally.
- Updates billing tab to show current plans, switches order of quotas to
list execution time, storage first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Track changes to Organization model
3 participants