1use crate::db::{BillingCustomerId, BillingSubscriptionId};
2use sea_orm::entity::prelude::*;
3use serde::Serialize;
4
5/// A billing subscription.
6#[derive(Clone, Debug, Default, PartialEq, Eq, DeriveEntityModel)]
7#[sea_orm(table_name = "billing_subscriptions")]
8pub struct Model {
9 #[sea_orm(primary_key)]
10 pub id: BillingSubscriptionId,
11 pub billing_customer_id: BillingCustomerId,
12 pub stripe_subscription_id: String,
13 pub stripe_subscription_status: StripeSubscriptionStatus,
14 pub stripe_cancel_at: Option<DateTime>,
15 pub stripe_cancellation_reason: Option<StripeCancellationReason>,
16 pub created_at: DateTime,
17}
18
19#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
20pub enum Relation {
21 #[sea_orm(
22 belongs_to = "super::billing_customer::Entity",
23 from = "Column::BillingCustomerId",
24 to = "super::billing_customer::Column::Id"
25 )]
26 BillingCustomer,
27}
28
29impl Related<super::billing_customer::Entity> for Entity {
30 fn to() -> RelationDef {
31 Relation::BillingCustomer.def()
32 }
33}
34
35impl ActiveModelBehavior for ActiveModel {}
36
37/// The status of a Stripe subscription.
38///
39/// [Stripe docs](https://docs.stripe.com/api/subscriptions/object#subscription_object-status)
40#[derive(
41 Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash, Serialize,
42)]
43#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)")]
44#[serde(rename_all = "snake_case")]
45pub enum StripeSubscriptionStatus {
46 #[default]
47 #[sea_orm(string_value = "incomplete")]
48 Incomplete,
49 #[sea_orm(string_value = "incomplete_expired")]
50 IncompleteExpired,
51 #[sea_orm(string_value = "trialing")]
52 Trialing,
53 #[sea_orm(string_value = "active")]
54 Active,
55 #[sea_orm(string_value = "past_due")]
56 PastDue,
57 #[sea_orm(string_value = "canceled")]
58 Canceled,
59 #[sea_orm(string_value = "unpaid")]
60 Unpaid,
61 #[sea_orm(string_value = "paused")]
62 Paused,
63}
64
65impl StripeSubscriptionStatus {
66 pub fn is_cancelable(&self) -> bool {
67 match self {
68 Self::Trialing | Self::Active | Self::PastDue => true,
69 Self::Incomplete
70 | Self::IncompleteExpired
71 | Self::Canceled
72 | Self::Unpaid
73 | Self::Paused => false,
74 }
75 }
76}
77
78/// The cancellation reason for a Stripe subscription.
79///
80/// [Stripe docs](https://docs.stripe.com/api/subscriptions/object#subscription_object-cancellation_details-reason)
81#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Hash, Serialize)]
82#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)")]
83#[serde(rename_all = "snake_case")]
84pub enum StripeCancellationReason {
85 #[sea_orm(string_value = "cancellation_requested")]
86 CancellationRequested,
87 #[sea_orm(string_value = "payment_disputed")]
88 PaymentDisputed,
89 #[sea_orm(string_value = "payment_failed")]
90 PaymentFailed,
91}