Skip to content

Commit

Permalink
collab: Add has_overdue_invoices to billing_customers (#24239)
Browse files Browse the repository at this point in the history
This PR adds a new `has_overdue_invoices` field to the
`billing_customers` table.

This will be used to statefully track whether a customer has overdue
invoices, and also to reset it when the invoices are paid.

We will set this field to `true` when a subscription is canceled with
the reason `payment_failed`.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Feb 4, 2025
1 parent b13498a commit aa3da35
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ CREATE TABLE IF NOT EXISTS billing_customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_id INTEGER NOT NULL REFERENCES users(id),
has_overdue_invoices BOOLEAN NOT NULL DEFAULT FALSE,
stripe_customer_id TEXT NOT NULL
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table billing_customers
add column has_overdue_invoices bool not null default false;
21 changes: 21 additions & 0 deletions crates/collab/src/api/billing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,27 @@ async fn handle_customer_subscription_event(
.await?
.ok_or_else(|| anyhow!("billing customer not found"))?;

let was_canceled_due_to_payment_failure = subscription.status == SubscriptionStatus::Canceled
&& subscription
.cancellation_details
.as_ref()
.and_then(|details| details.reason)
.map_or(false, |reason| {
reason == CancellationDetailsReason::PaymentFailed
});

if was_canceled_due_to_payment_failure {
app.db
.update_billing_customer(
billing_customer.id,
&UpdateBillingCustomerParams {
has_overdue_invoices: ActiveValue::set(true),
..Default::default()
},
)
.await?;
}

if let Some(existing_subscription) = app
.db
.get_billing_subscription_by_stripe_subscription_id(&subscription.id)
Expand Down
2 changes: 2 additions & 0 deletions crates/collab/src/db/queries/billing_customers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct CreateBillingCustomerParams {
pub struct UpdateBillingCustomerParams {
pub user_id: ActiveValue<UserId>,
pub stripe_customer_id: ActiveValue<String>,
pub has_overdue_invoices: ActiveValue<bool>,
}

impl Database {
Expand Down Expand Up @@ -43,6 +44,7 @@ impl Database {
id: ActiveValue::set(id),
user_id: params.user_id.clone(),
stripe_customer_id: params.stripe_customer_id.clone(),
has_overdue_invoices: params.has_overdue_invoices.clone(),
..Default::default()
})
.exec(&*tx)
Expand Down
1 change: 1 addition & 0 deletions crates/collab/src/db/tables/billing_customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Model {
pub id: BillingCustomerId,
pub user_id: UserId,
pub stripe_customer_id: String,
pub has_overdue_invoices: bool,
pub created_at: DateTime,
}

Expand Down

0 comments on commit aa3da35

Please sign in to comment.