Skip to main content

Function: fetchWeeklyRun()

fetchWeeklyRun(companyId, weekStart): Promise<FacilityRunRow[]>

Defined in: src/server/services/invoice/weeklyRun.ts:36

One row per active facility for the billing week, used to render the Invoicing cockpit grid.

The week is a billing (Sunday-anchored) week: the period runs weekStart through weekStart + 6 days inclusive (Sun–Sat). All numeric aggregation, the lesser-of-logged/contracted billable amount, and the per-facility derived status are computed in SQL by the weekly_billing_run RPC; this function only maps the snake_case RPC rows to camelCase FacilityRunRow. billableAmountCents is integer cents. Pass aggregateKpis(rows) to roll the rows up into cockpit totals.

Parameters

companyId

string

Tenant scope; forwarded to the RPC as p_company_id so only this company's facilities are returned (the signed-in admin's company).

weekStart

CalendarDate

The Sunday that anchors the billing week (a CalendarDate, not a JS Date).

Returns

Promise<FacilityRunRow[]>

One FacilityRunRow per active facility for the week. Resolves to an empty array if the RPC errors or returns no data — it never throws or rejects on a query failure.