-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Introduce
get-tenant-invoice
API request to fetch the invo…
…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
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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" }, | ||
|