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