From 0513e52ebe105f76cacf6f5bdafebb73309ee726 Mon Sep 17 00:00:00 2001 From: Joseph Shearer Date: Tue, 26 Sep 2023 15:13:04 -0400 Subject: [PATCH] Update to combine all final invoices into `invoice_type: final` --- crates/billing-integrations/src/stripe.rs | 24 +++++++++++------------ supabase/migrations/30_manual_billing.sql | 20 +++---------------- supabase/tests/billing.test.sql | 4 ++-- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/crates/billing-integrations/src/stripe.rs b/crates/billing-integrations/src/stripe.rs index f4d34aef93..1f31192df4 100644 --- a/crates/billing-integrations/src/stripe.rs +++ b/crates/billing-integrations/src/stripe.rs @@ -47,14 +47,12 @@ fn parse_date(arg: &str) -> Result { #[derive(Debug, Clone, PartialEq, Eq, Hash, sqlx::Type, Serialize, Deserialize)] #[sqlx(rename_all = "snake_case")] enum InvoiceType { - #[serde(rename = "usage")] - Usage, - #[serde(rename = "manual")] - Manual, - #[serde(rename = "current_month")] - CurrentMonth, + #[serde(rename = "final")] + Final, #[serde(rename = "preview")] Preview, + #[serde(rename = "manual")] + Manual, } #[derive(Serialize, Default, Debug)] @@ -113,10 +111,10 @@ impl Invoice { recreate_finalized: bool, ) -> anyhow::Result<()> { match (&self.invoice_type, &self.extra) { - (InvoiceType::CurrentMonth, _) => { - bail!("Should not create Stripe invoices for dynamic current_month invoices") + (InvoiceType::Preview, _) => { + bail!("Should not create Stripe invoices for preview invoices") } - (InvoiceType::Usage, Some(extra)) => { + (InvoiceType::Final, Some(extra)) => { let unwrapped_extra = extra.clone().0.expect( "This is just a sqlx quirk, if the outer Option is Some then this will be Some", ); @@ -129,7 +127,7 @@ impl Invoice { return Ok(()); } } - (InvoiceType::Usage, None) => { + (InvoiceType::Final, None) => { bail!("Invoice should have extra") } _ => {} @@ -137,7 +135,7 @@ impl Invoice { // An invoice should be generated in Stripe if the tenant is on a paid plan, which means: // * The tenant has a free trial start date - if let InvoiceType::Usage = self.invoice_type { + if let InvoiceType::Final = self.invoice_type { if let None = get_tenant_trial_date(&db_client, self.billed_prefix.to_owned()).await? { tracing::info!("Skipping usage invoice for tenant in free tier"); return Ok(()); @@ -437,9 +435,9 @@ pub async fn do_publish_invoices(cmd: &PublishInvoice) -> anyhow::Result<()> { }); tracing::info!( - "Processing {usage} usage-based bills, and {manual} manually-entered bills.", + "Processing {usage} usage-based invoices, and {manual} manually-entered invoices.", usage = invoice_type_counter - .remove(&InvoiceType::Usage) + .remove(&InvoiceType::Final) .unwrap_or_default(), manual = invoice_type_counter .remove(&InvoiceType::Manual) diff --git a/supabase/migrations/30_manual_billing.sql b/supabase/migrations/30_manual_billing.sql index 1f934e98c1..09a6727b37 100644 --- a/supabase/migrations/30_manual_billing.sql +++ b/supabase/migrations/30_manual_billing.sql @@ -326,16 +326,6 @@ manual_bills as ( from internal.manual_bills inner join authorized_tenants on manual_bills.tenant ^@ authorized_tenants.tenant ), -current_month as ( - select - date_trunc('month', (report->>'billed_month')::timestamptz)::date as date_start, - (date_trunc('month', (report->>'billed_month')::timestamptz) + interval '1 month' - interval '1 day')::date as date_end, - report->>'billed_prefix' as billed_prefix, - coalesce(nullif(report->'line_items', 'null'::jsonb), '[]'::jsonb) as line_items, - coalesce(nullif(report->'subtotal', 'null'::jsonb), to_jsonb(0))::integer as subtotal, - report as extra - from authorized_tenants, internal.billing_report_202308(authorized_tenants.tenant, now()) as report -), generated_prior_months as ( select date_trunc('month', (report->>'billed_month')::timestamptz)::date as date_start, @@ -347,13 +337,13 @@ generated_prior_months as ( from authorized_tenants join generate_series( greatest(date '2023-08-01', date_trunc('month', authorized_tenants.created_at)::date), - date_trunc('month',now()::date - interval '1 month'), + date_trunc('month',now()::date), '1 month' ) as invoice_month on not exists(select 1 from historical_bills where historical_bills.date_start = invoice_month) join internal.billing_report_202308(authorized_tenants.tenant, invoice_month) as report on true ) select - h.date_start, h.date_end, h.billed_prefix, h.line_items, h.subtotal, h.extra, 'usage' as invoice_type + h.date_start, h.date_end, h.billed_prefix, h.line_items, h.subtotal, h.extra, 'final' as invoice_type from historical_bills h union all select @@ -362,11 +352,7 @@ from generated_prior_months p union all select m.date_start, m.date_end, m.billed_prefix, m.line_items, m.subtotal, m.extra, 'manual' as invoice_type -from manual_bills m -union all -select - c.date_start, c.date_end, c.billed_prefix, c.line_items, c.subtotal, c.extra, 'current_month' as invoice_type -from current_month c; +from manual_bills m; grant select on table invoices_ext to authenticated; diff --git a/supabase/tests/billing.test.sql b/supabase/tests/billing.test.sql index 3c62556c48..c55b7e31e1 100644 --- a/supabase/tests/billing.test.sql +++ b/supabase/tests/billing.test.sql @@ -616,7 +616,7 @@ begin 'date_end', (date_trunc('month', now()) + interval '1 month' - interval '1 day')::date, 'subtotal', 0, 'line_items', '[]'::jsonb, - 'invoice_type', 'current_month', + 'invoice_type', 'preview', 'billed_prefix', 'aliceCo/' ), '{ @@ -638,7 +638,7 @@ begin "billed_prefix": "aliceCo/", "date_end": "2022-08-31", "date_start": "2022-08-01", - "invoice_type": "usage", + "invoice_type": "final", "line_items": [ { "count": 1,