Files
HealthLog/src/lib/life-events/dates.ts
T
Marc-André Bombeck 0924285cd6 feat: serve the day, its index, the timeline and life events
Replace the day, timeline and life-event stubs with their implementations
(#613) and drop the not-implemented helper, its error code and its OpenAPI
response with the last stub.

loadDay reads one local day for every reader: the readings live inside the
record's own day window (never the UTC rollups), through the canonical-source
engine the mood sheet already uses (resolveDayReadings in linked-context.ts),
with the person's usual range over the 30 days before; the record sections
from their own tables, each in the storage form it speaks (an instant in the
local window, a date string with its own zone, a calendar date). A switched-off
module's section is not read; a section the grant does not cover is named
not_shared and not read.

The day, the day index and the new visit preparation (GET /api/day/notable)
are admitted on the readings and narrowed per section through
actingDomainVisibility, so a scoped grant sees exactly its sections. The
timeline and its readiness stay whole-record reads and narrow their lanes the
same way.

Notable days are deterministic keys: a three-month high or low with history
behind it, a first reading, and in the visit preparation a regular reading
that stopped. The timeline lays out spans and points from the owning tables,
standing items without a date, and series by zoom: months from the rollup
tier inside its five-year window and from one cached live query before it.
The readiness inventory rates each lane with one link per gap.

Life events are created, edited and soft-deleted as MANAGE verbs with audit
rows, encrypted at rest, and join the sharing guard and the handler matrix.
get_day reads a day for the Coach and the MCP endpoint without life events,
notes or document names; its step carries the date it read.
2026-10-08 14:31:13 +02:00

22 lines
726 B
TypeScript

/**
* Calendar arithmetic for life-event dates (v1.42, #613). Pure, no zone: a
* life-event date is a calendar date aligned to its precision.
*/
import { shiftDateKey } from "@/lib/tz/format";
/**
* The last calendar day a life-event date covers at its precision: the day
* itself, the last day of its month, or the last day of its year.
*/
export function lastDayCovered(
date: string,
precision: "DAY" | "MONTH" | "YEAR",
): string {
if (precision === "DAY") return date;
if (precision === "YEAR") return `${date.slice(0, 4)}-12-31`;
const [y, m] = date.split("-").map(Number);
const next =
m === 12 ? `${y + 1}-01-01` : `${y}-${String(m + 1).padStart(2, "0")}-01`;
return shiftDateKey(next, -1);
}