Skip to content

Commit

Permalink
feature: Introduce get-tenant-invoice API request to fetch the invo…
Browse files Browse the repository at this point in the history
…ice metadata for a particular tenant and month (#1192)

* feature: Introduce `get-tenant-invoice` API request to fetch the invoice metadata for a particular tenant and month
  • Loading branch information
jshearer authored Sep 20, 2023
1 parent 4e3d0f6 commit fb0e33d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
52 changes: 52 additions & 0 deletions supabase/functions/billing/get_tenant_invoice_data.ts
Original file line number Diff line number Diff line change
@@ -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<ConstructorParameters<typeof Response>> {
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,
}];
}
3 changes: 3 additions & 0 deletions supabase/functions/billing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]";
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
Expand Down Expand Up @@ -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" },
Expand Down

0 comments on commit fb0e33d

Please sign in to comment.