1use crate::db::billing_subscription::StripeSubscriptionStatus;
2
3use super::*;
4
5#[derive(Debug)]
6pub struct CreateBillingSubscriptionParams {
7 pub billing_customer_id: BillingCustomerId,
8 pub stripe_subscription_id: String,
9 pub stripe_subscription_status: StripeSubscriptionStatus,
10}
11
12#[derive(Debug, Default)]
13pub struct UpdateBillingSubscriptionParams {
14 pub billing_customer_id: ActiveValue<BillingCustomerId>,
15 pub stripe_subscription_id: ActiveValue<String>,
16 pub stripe_subscription_status: ActiveValue<StripeSubscriptionStatus>,
17}
18
19impl Database {
20 /// Creates a new billing subscription.
21 pub async fn create_billing_subscription(
22 &self,
23 params: &CreateBillingSubscriptionParams,
24 ) -> Result<()> {
25 self.transaction(|tx| async move {
26 billing_subscription::Entity::insert(billing_subscription::ActiveModel {
27 billing_customer_id: ActiveValue::set(params.billing_customer_id),
28 stripe_subscription_id: ActiveValue::set(params.stripe_subscription_id.clone()),
29 stripe_subscription_status: ActiveValue::set(params.stripe_subscription_status),
30 ..Default::default()
31 })
32 .exec_without_returning(&*tx)
33 .await?;
34
35 Ok(())
36 })
37 .await
38 }
39
40 /// Updates the specified billing subscription.
41 pub async fn update_billing_subscription(
42 &self,
43 id: BillingSubscriptionId,
44 params: &UpdateBillingSubscriptionParams,
45 ) -> Result<()> {
46 self.transaction(|tx| async move {
47 billing_subscription::Entity::update(billing_subscription::ActiveModel {
48 id: ActiveValue::set(id),
49 billing_customer_id: params.billing_customer_id.clone(),
50 stripe_subscription_id: params.stripe_subscription_id.clone(),
51 stripe_subscription_status: params.stripe_subscription_status.clone(),
52 ..Default::default()
53 })
54 .exec(&*tx)
55 .await?;
56
57 Ok(())
58 })
59 .await
60 }
61
62 /// Returns the billing subscription with the specified ID.
63 pub async fn get_billing_subscription_by_id(
64 &self,
65 id: BillingSubscriptionId,
66 ) -> Result<Option<billing_subscription::Model>> {
67 self.transaction(|tx| async move {
68 Ok(billing_subscription::Entity::find_by_id(id)
69 .one(&*tx)
70 .await?)
71 })
72 .await
73 }
74
75 /// Returns the billing subscription with the specified Stripe subscription ID.
76 pub async fn get_billing_subscription_by_stripe_subscription_id(
77 &self,
78 stripe_subscription_id: &str,
79 ) -> Result<Option<billing_subscription::Model>> {
80 self.transaction(|tx| async move {
81 Ok(billing_subscription::Entity::find()
82 .filter(
83 billing_subscription::Column::StripeSubscriptionId.eq(stripe_subscription_id),
84 )
85 .one(&*tx)
86 .await?)
87 })
88 .await
89 }
90
91 /// Returns all of the billing subscriptions for the user with the specified ID.
92 ///
93 /// Note that this returns the subscriptions regardless of their status.
94 /// If you're wanting to check if a use has an active billing subscription,
95 /// use `get_active_billing_subscriptions` instead.
96 pub async fn get_billing_subscriptions(
97 &self,
98 user_id: UserId,
99 ) -> Result<Vec<billing_subscription::Model>> {
100 self.transaction(|tx| async move {
101 let subscriptions = billing_subscription::Entity::find()
102 .inner_join(billing_customer::Entity)
103 .filter(billing_customer::Column::UserId.eq(user_id))
104 .order_by_asc(billing_subscription::Column::Id)
105 .all(&*tx)
106 .await?;
107
108 Ok(subscriptions)
109 })
110 .await
111 }
112
113 /// Returns all of the active billing subscriptions for the user with the specified ID.
114 pub async fn get_active_billing_subscriptions(
115 &self,
116 user_id: UserId,
117 ) -> Result<Vec<billing_subscription::Model>> {
118 self.transaction(|tx| async move {
119 let subscriptions = billing_subscription::Entity::find()
120 .inner_join(billing_customer::Entity)
121 .filter(
122 billing_customer::Column::UserId.eq(user_id).and(
123 billing_subscription::Column::StripeSubscriptionStatus
124 .eq(StripeSubscriptionStatus::Active),
125 ),
126 )
127 .order_by_asc(billing_subscription::Column::Id)
128 .all(&*tx)
129 .await?;
130
131 Ok(subscriptions)
132 })
133 .await
134 }
135}