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    pub async fn get_billing_customer_by_id(
 61        &self,
 62        id: BillingCustomerId,
 63    ) -> Result<Option<billing_customer::Model>> {
 64        self.transaction(|tx| async move {
 65            Ok(billing_customer::Entity::find()
 66                .filter(billing_customer::Column::Id.eq(id))
 67                .one(&*tx)
 68                .await?)
 69        })
 70        .await
 71    }
 72
 73    /// Returns the billing customer for the user with the specified ID.
 74    pub async fn get_billing_customer_by_user_id(
 75        &self,
 76        user_id: UserId,
 77    ) -> Result<Option<billing_customer::Model>> {
 78        self.transaction(|tx| async move {
 79            Ok(billing_customer::Entity::find()
 80                .filter(billing_customer::Column::UserId.eq(user_id))
 81                .one(&*tx)
 82                .await?)
 83        })
 84        .await
 85    }
 86
 87    /// Returns the billing customer for the user with the specified Stripe customer ID.
 88    pub async fn get_billing_customer_by_stripe_customer_id(
 89        &self,
 90        stripe_customer_id: &str,
 91    ) -> Result<Option<billing_customer::Model>> {
 92        self.transaction(|tx| async move {
 93            Ok(billing_customer::Entity::find()
 94                .filter(billing_customer::Column::StripeCustomerId.eq(stripe_customer_id))
 95                .one(&*tx)
 96                .await?)
 97        })
 98        .await
 99    }
100}