collab: Add billing_subscriptions table (#15448)

This PR adds a new `billing_subscriptions` table to the database, as
well as some accompanying models/queries.

In this table we store a minimal amount of data from Stripe:

- The Stripe customer ID
- The Stripe subscription ID
- The status of the Stripe subscription

This should be enough for interactions with the Stripe API (e.g., to
[create a customer portal
session](https://docs.stripe.com/api/customer_portal/sessions/create)),
as well as determine whether a subscription is active (based on the
`status`).

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-29 14:32:13 -04:00 committed by GitHub
parent 0702ed5cd6
commit 085d41b121
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 215 additions and 0 deletions

View file

@ -0,0 +1,58 @@
use crate::db::{BillingSubscriptionId, UserId};
use sea_orm::entity::prelude::*;
/// A billing subscription.
#[derive(Clone, Debug, Default, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "billing_subscriptions")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: BillingSubscriptionId,
pub user_id: UserId,
pub stripe_customer_id: String,
pub stripe_subscription_id: String,
pub stripe_subscription_status: StripeSubscriptionStatus,
pub created_at: DateTime,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id"
)]
User,
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}
/// The status of a Stripe subscription.
///
/// [Stripe docs](https://docs.stripe.com/api/subscriptions/object#subscription_object-status)
#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash)]
#[sea_orm(rs_type = "String", db_type = "String(None)")]
pub enum StripeSubscriptionStatus {
#[default]
#[sea_orm(string_value = "incomplete")]
Incomplete,
#[sea_orm(string_value = "incomplete_expired")]
IncompleteExpired,
#[sea_orm(string_value = "trialing")]
Trialing,
#[sea_orm(string_value = "active")]
Active,
#[sea_orm(string_value = "past_due")]
PastDue,
#[sea_orm(string_value = "canceled")]
Canceled,
#[sea_orm(string_value = "unpaid")]
Unpaid,
#[sea_orm(string_value = "paused")]
Paused,
}

View file

@ -24,6 +24,8 @@ pub struct Model {
pub enum Relation {
#[sea_orm(has_many = "super::access_token::Entity")]
AccessToken,
#[sea_orm(has_many = "super::billing_subscription::Entity")]
BillingSubscription,
#[sea_orm(has_one = "super::room_participant::Entity")]
RoomParticipant,
#[sea_orm(has_many = "super::project::Entity")]