billing_customers.rs

 1use super::*;
 2
 3#[derive(Debug)]
 4pub struct CreateBillingCustomerParams {
 5    pub user_id: UserId,
 6    pub stripe_customer_id: String,
 7}
 8
 9#[derive(Debug, Default)]
10pub struct UpdateBillingCustomerParams {
11    pub user_id: ActiveValue<UserId>,
12    pub stripe_customer_id: ActiveValue<String>,
13    pub has_overdue_invoices: ActiveValue<bool>,
14    pub trial_started_at: ActiveValue<Option<DateTime>>,
15}
16
17impl Database {
18    /// Creates a new billing customer.
19    pub async fn create_billing_customer(
20        &self,
21        params: &CreateBillingCustomerParams,
22    ) -> Result<billing_customer::Model> {
23        self.transaction(|tx| async move {
24            let customer = billing_customer::Entity::insert(billing_customer::ActiveModel {
25                user_id: ActiveValue::set(params.user_id),
26                stripe_customer_id: ActiveValue::set(params.stripe_customer_id.clone()),
27                ..Default::default()
28            })
29            .exec_with_returning(&*tx)
30            .await?;
31
32            Ok(customer)
33        })
34        .await
35    }
36
37    /// Updates the specified billing customer.
38    pub async fn update_billing_customer(
39        &self,
40        id: BillingCustomerId,
41        params: &UpdateBillingCustomerParams,
42    ) -> Result<()> {
43        self.transaction(|tx| async move {
44            billing_customer::Entity::update(billing_customer::ActiveModel {
45                id: ActiveValue::set(id),
46                user_id: params.user_id.clone(),
47                stripe_customer_id: params.stripe_customer_id.clone(),
48                has_overdue_invoices: params.has_overdue_invoices.clone(),
49                trial_started_at: params.trial_started_at.clone(),
50                created_at: ActiveValue::not_set(),
51            })
52            .exec(&*tx)
53            .await?;
54
55            Ok(())
56        })
57        .await
58    }
59
60    /// Returns the billing customer for the user with the specified ID.
61    pub async fn get_billing_customer_by_user_id(
62        &self,
63        user_id: UserId,
64    ) -> Result<Option<billing_customer::Model>> {
65        self.transaction(|tx| async move {
66            Ok(billing_customer::Entity::find()
67                .filter(billing_customer::Column::UserId.eq(user_id))
68                .one(&*tx)
69                .await?)
70        })
71        .await
72    }
73
74    /// Returns the billing customer for the user with the specified Stripe customer ID.
75    pub async fn get_billing_customer_by_stripe_customer_id(
76        &self,
77        stripe_customer_id: &str,
78    ) -> Result<Option<billing_customer::Model>> {
79        self.transaction(|tx| async move {
80            Ok(billing_customer::Entity::find()
81                .filter(billing_customer::Column::StripeCustomerId.eq(stripe_customer_id))
82                .one(&*tx)
83                .await?)
84        })
85        .await
86    }
87}