From 11d7dbb06e79e604c900b89a01c44c5ce7b4ad63 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 24 May 2026 14:48:40 +0200 Subject: [PATCH] Phase2: Invoicecomputation/AdminpricingUI/Ainvoicemgnt --- src/lib/billing.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/lib/billing.ts b/src/lib/billing.ts index 6fd122d..8dc4a55 100644 --- a/src/lib/billing.ts +++ b/src/lib/billing.ts @@ -341,15 +341,21 @@ async function collectThreemaUsage( ): Promise<{ inCount: number; outCount: number } | null> { const packages = tenant.spec.packages ?? []; if (!packages.includes("threema")) return null; - const usage = await getThreemaUsage( - tenant.metadata.name, - periodStart, - periodEnd - ).catch(() => null); - if (!usage) return null; + // threema-relay.getUsage takes Date params, not strings, and + // returns a discriminated RelayResult — the + // `ok` discriminant must be checked before reading the totals. + // Period end is exclusive in the relay's API; pass the next-day + // midnight UTC to capture the full last day of the period. + const from = new Date(`${periodStart}T00:00:00Z`); + const to = new Date(`${periodEnd}T00:00:00Z`); + to.setUTCDate(to.getUTCDate() + 1); + const result = await getThreemaUsage(tenant.metadata.name, from, to).catch( + () => null + ); + if (!result || !result.ok) return null; return { - inCount: Number(usage.totals?.in ?? 0), - outCount: Number(usage.totals?.out ?? 0), + inCount: Number(result.totals?.in ?? 0), + outCount: Number(result.totals?.out ?? 0), }; }