Files
HealthLog/src/lib/cycle/prediction-refresh.ts
T
Marc-André Bombeck 6ac3a61829 Take the writes off three read routes
A GET must not have side effects — api-handler.ts says so next to
READ_HTTP_METHODS, where an MCP-audience token is admitted on the safe
methods precisely because they are assumed to write nothing. Three routes
broke that: the sync handshake bumped User.lastSyncedAt, the achievements
read inserted unlock rows, and the cycle calendar fired a discarded promise
at the forecast cache so a failed write left no trace anywhere.

Each write moves to the place that causes it.

The sync checkpoint is now stamped by the batch ingest endpoints for
measurements, mood and intakes. The handshake reports it. That also fixes
the watermark: the checkpoint used to advance past a window the client had
not drained, and a client trusting it would skip that window. It can only
trail a real sync now.

The unlock rows move to a nightly achievement-unlock-sweep. The badge grid
never needed them — unlocked comes from the live metrics and the completion
date is derived — so the payload is unchanged; the rows pin that date and
carry it into the backup, and the sweep reaches accounts that never open
the page.

The cycle forecast moves to a nightly cycle-prediction-refresh. The
CyclePrediction row is not a cache of a read: it is the index the reminder
cron scans, so which reminders an account received depended on whether
somebody had opened the calendar. persistPredictionCache no longer
swallows its errors either — the job reports them.
2026-08-02 01:21:03 +02:00

127 lines
3.9 KiB
TypeScript

/**
* Recompute the cached cycle forecast for one account from the persisted
* rows, without a request.
*
* `CyclePrediction` is not a cache of a read — it is the index the
* cycle-reminder cron scans to find who is due a period-soon or fertile-soon
* push. Until v1.35.3 its only writer was `GET /api/cycle/calendar`, which
* meant the reminders an account received depended on whether somebody had
* opened the calendar recently, and the write itself was a discarded promise
* inside a `try {} catch {}` that swallowed the failure. Both ends are fixed
* here: the refresh runs on the schedule that consumes it, and a failed write
* throws so the job reports it.
*
* The reads mirror the calendar route's default window so the forecast the
* cron scans and the forecast the calendar renders come out of the same
* inputs.
*/
import { prisma } from "@/lib/db";
import { predictCycle } from "@/lib/cycle";
import {
toCycleInputs,
toDayLogInputs,
toProfileInput,
} from "@/lib/cycle/engine-adapter";
import { persistPredictionCache } from "@/lib/cycle/prediction-cache";
import { addDays } from "@/lib/cycle/day-math";
import { BBT_WINDOW } from "@/lib/cycle/types";
import { DEFAULT_TIMEZONE, moodDateKey } from "@/lib/mood/date-key";
/** Day-log lookback: the calendar's default past span or the symptothermal
* window, whichever reaches further back. */
const LOOKBACK_DAYS = Math.max(90, BBT_WINDOW);
/** Wrist/skin-temperature lookback feeding the temperature-trend layer. */
const TEMPERATURE_LOOKBACK_DAYS = 90;
export type PredictionRefreshOutcome =
/** A forecast was computed and the cache row reflects it. */
| "persisted"
/** No profile row, so the account has never touched cycle tracking. */
| "no-profile"
/** Prediction is off for this account (raw-chart mode / explicit opt-out),
* or the engine had nothing to forecast from. */
| "no-prediction";
/**
* Recompute and persist the forecast for one account. Throws if the write
* fails — the caller is a job and owns the failure.
*/
export async function refreshPredictionCacheForUser(
userId: string,
now: Date = new Date(),
): Promise<PredictionRefreshOutcome> {
const [profile, user] = await Promise.all([
prisma.cycleProfile.findUnique({ where: { userId } }),
prisma.user.findUnique({
where: { id: userId },
select: { timezone: true },
}),
]);
if (!profile) return "no-profile";
if (!profile.predictionEnabled || profile.rawChartMode) {
return "no-prediction";
}
const tz = user?.timezone ?? DEFAULT_TIMEZONE;
const today = moodDateKey(now, tz);
const [cycles, dayLogs, nightlyTemps] = await Promise.all([
prisma.menstrualCycle.findMany({
where: { userId, deletedAt: null },
orderBy: { startDate: "asc" },
}),
prisma.cycleDayLog.findMany({
where: {
userId,
deletedAt: null,
date: { gte: addDays(today, -LOOKBACK_DAYS) },
},
orderBy: { date: "asc" },
select: {
date: true,
flow: true,
basalBodyTempC: true,
temperatureExcluded: true,
ovulationTest: true,
cervicalMucus: true,
cervixPosition: true,
cervixFirmness: true,
cervixOpening: true,
},
}),
prisma.measurement.findMany({
where: {
userId,
deletedAt: null,
type: "WRIST_TEMPERATURE",
measuredAt: {
gte: new Date(
Date.parse(
`${addDays(today, -TEMPERATURE_LOOKBACK_DAYS)}T00:00:00Z`,
),
),
},
},
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
]);
const nights = nightlyTemps.map((m) => ({
date: moodDateKey(m.measuredAt, tz),
valueC: m.value,
}));
const prediction = predictCycle(
toCycleInputs(cycles),
toDayLogInputs(dayLogs),
toProfileInput(profile),
today,
nights,
);
await persistPredictionCache(userId, prediction, now);
return "persisted";
}