diff --git a/supabase/functions/billing/get_tenant_invoice_data.ts b/supabase/functions/billing/get_tenant_invoice_data.ts new file mode 100644 index 0000000000..e92a603b61 --- /dev/null +++ b/supabase/functions/billing/get_tenant_invoice_data.ts @@ -0,0 +1,52 @@ +import { customerQuery, StripeClient } from "./shared.ts"; + +export interface getTenantPaymentMethodsParams { + tenant: string; + month: string; + type: "Usage" | "Manual"; +} + +const INVOICE_TYPE_KEY = "estuary.dev/invoice_type"; +const BILLING_PERIOD_START_KEY = "estuary.dev/period_start"; + +export async function getTenantInvoice( + req_body: getTenantPaymentMethodsParams, + full_req: Request, +): Promise> { + const customer = (await StripeClient.customers.search({ query: customerQuery(req_body.tenant) })).data[0]; + const parsed_date = new Date(req_body.month); + + const year = new Intl.DateTimeFormat("en", { year: "numeric" }).format(parsed_date); + const month = new Intl.DateTimeFormat("en", { month: "2-digit" }).format(parsed_date); + + // We always start on the first of the month + const metadata_date = `${year}-${month}-01`; + + if (customer) { + const query = + `customer:"${customer.id}" AND metadata["${INVOICE_TYPE_KEY}"]:"${req_body.type}" AND metadata["${BILLING_PERIOD_START_KEY}"]:"${metadata_date}" AND -status:"draft"`; + const resp = await StripeClient.invoices.search({ + query, + }); + + if (resp.data[0]) { + const limited_invoice = { + id: resp.data[0].id, + amount_due: resp.data[0].amount_due, + invoice_pdf: resp.data[0].invoice_pdf, + hosted_invoice_url: resp.data[0].hosted_invoice_url, + status: resp.data[0].status, + }; + + return [JSON.stringify({ invoice: limited_invoice }), { + headers: { "Content-Type": "application/json" }, + status: 200, + }]; + } + } + + return [JSON.stringify({ invoice: null }), { + headers: { "Content-Type": "application/json" }, + status: 200, + }]; +} diff --git a/supabase/functions/billing/index.ts b/supabase/functions/billing/index.ts index 9fa8a0826a..41a3506ff0 100644 --- a/supabase/functions/billing/index.ts +++ b/supabase/functions/billing/index.ts @@ -5,6 +5,7 @@ import { getTenantPaymentMethods } from "./get_tenant_payment_methods.ts"; import { deleteTenantPaymentMethod } from "./delete_tenant_payment_method.ts"; import { setTenantPrimaryPaymentMethod } from "./set_tenant_primary_payment_method.ts"; import { createClient } from "https://esm.sh/@supabase/supabase-js@2.0.5"; +import { getTenantInvoice } from "./get_tenant_invoice_data.ts"; // Now that the supabase CLI supports multiple edge functions, // we should refactor this into individual functions instead @@ -54,6 +55,8 @@ serve(async (req) => { res = await deleteTenantPaymentMethod(request, req); } else if (request.operation === "set-tenant-primary-payment-method") { res = await setTenantPrimaryPaymentMethod(request, req); + } else if (request.operation === "get-tenant-invoice") { + res = await getTenantInvoice(request, req); } else { res = [JSON.stringify({ error: "unknown_operation" }), { headers: { "Content-Type": "application/json" },