Files
HealthLog/src/lib/daily/briefing-today-read.ts
T
Marc-André Bombeck bf3830203f fix(hrv): keep RMSSD on the display surfaces for an account with no SDNN
Fitbit and Google Health HRV is filed as RMSSD since migration 0420, so
an account whose only HRV comes from them lost it wherever a display
surface read SDNN alone. The dashboard HRV tile keeps its RMSSD fallback
with Recovery off while the account has no SDNN, and its toggle stays
offered for either estimator. A briefing HRV signal counts as measured
today when either series has a reading. The doctor report already files
RMSSD as its own ungated leaf; a guard now holds that. Readiness, HRV
balance and stress stay on SDNN; a test pins that an RMSSD-only account
renormalises over the other components.
2026-10-21 14:32:56 +02:00

100 lines
3.5 KiB
TypeScript

/**
* The briefing as every surface serves it: the digest, the dashboard
* snapshot and the `/insights` advisor payload all go through
* `briefingForToday`, so a client never decides for itself whether
* yesterday's text or a raw "+33.72 bpm" may be shown.
*
* The snapshot already carries each type's newest reading and passes it in;
* the advisor route has no snapshot at hand and reads the few reading types
* a signal can rest on here, in one grouped query, and only when the payload
* actually carries such a signal.
*/
import { dailyBriefingSchema, type DailyBriefing } from "@/lib/ai/schema";
import { prisma } from "@/lib/db";
import { readLatestEver } from "@/lib/dashboard/summary-reads";
import type { Locale } from "@/lib/i18n/config";
import { readBriefingGeneratedAt } from "@/lib/insights/briefing-generated-at";
import { userDayKey } from "@/lib/tz/format";
import {
ALL_SIGNAL_READING_TYPES,
SIGNAL_READING_TYPE,
briefingForToday,
} from "@/lib/daily/briefing-today";
import type { MeasurementType } from "@/generated/prisma/client";
/** Whether a generation moment falls on today's calendar day in `timezone`. */
export function generatedOnLocalToday(
generatedAt: string | null,
timezone: string,
now: Date,
): boolean {
return (
generatedAt !== null &&
userDayKey(new Date(generatedAt), timezone) === userDayKey(now, timezone)
);
}
/**
* The newest reading of every type a briefing signal can rest on. A failed
* read answers "no reading" for every type, which drops reading-backed
* signals: the quiet side of the rule, never a stale claim.
*/
export async function readSignalLastSeen(
userId: string,
): Promise<(type: string) => string | null> {
try {
// One index probe per type for its latest live reading
// (`readLatestEver`), rather than a grouped max over every row.
const rows = await readLatestEver(
prisma,
userId,
ALL_SIGNAL_READING_TYPES as MeasurementType[],
);
const byType = new Map(
rows.map((row) => [
String(row.type),
new Date(row.measured_at).toISOString(),
]),
);
return (type) => byType.get(type) ?? null;
} catch {
return () => null;
}
}
/**
* A cached advisor payload with its `dailyBriefing` resolved for today:
* null when the text was generated on an earlier day (or at an unknown
* time), otherwise without signals whose metric was not measured today and
* with every delta at its metric's precision. Anything that is not an
* advisor payload with a valid briefing passes through untouched.
*/
export async function insightsPayloadForToday(
payload: unknown,
ctx: { userId: string; timezone: string; language: Locale; now?: Date },
): Promise<unknown> {
if (payload === null || typeof payload !== "object") return payload;
const record = payload as Record<string, unknown>;
if (record.dailyBriefing == null) return payload;
const parsed = dailyBriefingSchema.safeParse(record.dailyBriefing);
if (!parsed.success) return payload;
const briefing: DailyBriefing = parsed.data;
const now = ctx.now ?? new Date();
const needsReadings = (briefing.signalsOfDay ?? []).some(
(s) => SIGNAL_READING_TYPE[s.sourceMetric] !== undefined,
);
const lastSeenAt = needsReadings
? await readSignalLastSeen(ctx.userId)
: () => null;
return {
...record,
dailyBriefing: briefingForToday(briefing, {
generatedAt: readBriefingGeneratedAt(record),
lastSeenAt,
timezone: ctx.timezone,
todayLocalDate: userDayKey(now, ctx.timezone),
language: ctx.language,
}),
};
}