Phase2: Invoicecomputation/AdminpricingUI/Ainvoicemgnt

This commit is contained in:
2026-05-24 14:48:40 +02:00
parent d41f0b6ec9
commit f795b9f0ca

View File

@@ -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<UsageBreakdown> — 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),
};
}