Improve transactionality, align invoice model with frontend
This commit is contained in:
+14
-4
@@ -106,12 +106,22 @@ export type Tenant = {
|
||||
|
||||
export type Invoice = {
|
||||
id: string
|
||||
customer: string
|
||||
status: string
|
||||
amount_due: number
|
||||
currency: string
|
||||
tenant_pubkey: string
|
||||
amount: number
|
||||
period_start: number
|
||||
period_end: number
|
||||
created_at: number
|
||||
paid_at: number | null
|
||||
voided_at: number | null
|
||||
}
|
||||
|
||||
// The backend models an invoice's lifecycle as timestamps rather than a status
|
||||
// field, so derive the display status from them: paid once paid_at is set, void
|
||||
// once voided_at is set, otherwise still open.
|
||||
export function invoiceStatus(invoice: Pick<Invoice, "paid_at" | "voided_at">): "open" | "paid" | "void" {
|
||||
if (invoice.paid_at != null) return "paid"
|
||||
if (invoice.voided_at != null) return "void"
|
||||
return "open"
|
||||
}
|
||||
|
||||
export type Activity = {
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
reactivateRelay,
|
||||
getRelay,
|
||||
getTenant,
|
||||
invoiceStatus,
|
||||
listRelayActivity,
|
||||
listRelays,
|
||||
listTenantInvoices,
|
||||
@@ -141,7 +142,7 @@ export async function tenantNeedsPaymentSetup(): Promise<boolean> {
|
||||
export async function getLatestOpenInvoice(): Promise<Invoice | null> {
|
||||
const invoices = await listTenantInvoices(account()!.pubkey)
|
||||
const open = invoices
|
||||
.filter(inv => inv.status === "open" && inv.amount_due > 0)
|
||||
.filter(inv => invoiceStatus(inv) === "open" && inv.amount > 0)
|
||||
.sort((a, b) => b.period_start - a.period_start)
|
||||
return open[0] ?? null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user