Carry the recorded score days through wipe, backup and restore

A new user-scoped table is outside all three by default, and the two
structural guards said so the moment the model landed: the wipe plan and
the backup verdict list both named it as unclassified. It now joins the
wipe list, rides the backup payload as its own spread section, and comes
back through the restore route.

The classification guards only prove a helper exists in a declared file, so
they would pass an unreferenced restore helper. The integration round trip
goes through the real admin restore route instead, with the table emptied
first so a restore that carries nothing cannot pass by leaving what was
already there.
This commit is contained in:
Marc-André Bombeck committed 2026-07-31 15:13:47 +02:00
1 parent d89b90da4b
commit ab8e9a164d
8 files changed
+692 -2

No files matched your search

@@ -40,6 +40,7 @@ import { recomputeUserRollups } from "@/lib/rollups/measurement-rollups";
import { restoreCycleData } from "@/lib/cycle/backup";
import { restoreProfileData } from "@/lib/export/profile-backup";
import { restoreIntradayProfileData } from "@/lib/export/intraday-profile-backup";
import { restoreHealthScoreData } from "@/lib/export/health-score-backup";
import { invalidateUserData } from "@/lib/cache/invalidate";
export const dynamic = "force-dynamic";
@@ -71,6 +72,7 @@ interface RestoreResponse {
customMetrics: number;
correlationPatterns: number;
intradayProfiles: number;
healthScoreRecords: number;
};
}
@@ -764,6 +766,14 @@ const handler = apiHandler(
payload,
);
// The score as it was shown on each local day. Both ends live in
// `src/lib/export/health-score-backup.ts`, same as the two above.
const healthScoreCleared = await restoreHealthScoreData(
tx,
ownerId,
payload,
);
const biomarkerByName = new Map<string, string>();
const restoredBiomarkerIds = new Set<string>();
for (const biomarker of payload.biomarkers) {
@@ -1128,6 +1138,7 @@ const handler = apiHandler(
customMetrics: profileCleared.customMetrics,
correlationPatterns: profileCleared.correlationPatterns,
intradayProfiles: intradayCleared.intradayProfiles,
healthScoreRecords: healthScoreCleared.healthScoreRecords,
};
},
{
@@ -324,7 +324,9 @@ describe("writing the row", () => {
* not a comment.
*/
function delegate(count: number) {
const createMany = vi.fn(async () => ({ count }));
const createMany = vi.fn(async (_args: { skipDuplicates?: boolean }) => ({
count,
}));
return {
db: { healthScoreRecord: { createMany } } as never,
createMany,
+6
View File
@@ -54,6 +54,12 @@ export const WIPE_MODELS = [
// record and nothing else holds it, so a wipe that leaves it behind leaves
// behind an account's activity pattern.
"IntradayCumulativeProfile",
// The health score as it was shown, one row per local day. Not a cache of
// the live computation — nothing can rebuild a past day once the readings
// behind it have moved — so leaving it behind leaves behind a record of how
// the account was doing, day by day, after a confirmation that said
// otherwise.
"HealthScoreRecord",
"MeasurementReminder",
// ── Medication ──────────────────────────────────────────────────────────
+9
View File
@@ -103,6 +103,12 @@ export const BACKED_UP_MODELS = [
"CustomMetric",
"CustomMetricEntry",
"CorrelationPattern",
// Reads like a derived tier and is not one. The live computation always
// answers for today from today's rows, so once the readings behind a past
// day have been corrected, re-synced or simply grown, nothing can reproduce
// what that day said. The row is the only copy of the number the account
// actually saw, which is exactly the test for BACKED_UP rather than DERIVED.
"HealthScoreRecord",
// ── Activity and nutrition ────────────────────────────────────────────────
"Workout",
@@ -157,6 +163,7 @@ export const BACKUP_WRITER_FILES: readonly string[] = [
"src/lib/export/records-backup.ts",
"src/lib/export/profile-backup.ts",
"src/lib/export/intraday-profile-backup.ts",
"src/lib/export/health-score-backup.ts",
"src/lib/cycle/backup.ts",
];
@@ -164,6 +171,7 @@ export const BACKUP_RESTORE_FILES: readonly string[] = [
"src/app/api/admin/backups/[id]/restore/route.ts",
"src/lib/export/profile-backup.ts",
"src/lib/export/intraday-profile-backup.ts",
"src/lib/export/health-score-backup.ts",
"src/lib/cycle/backup.ts",
];
@@ -206,6 +214,7 @@ export const TWO_ENDED_MODELS: readonly string[] = [
"CustomMetric",
"CustomMetricEntry",
"CorrelationPattern",
"HealthScoreRecord",
"Workout",
"NutrientIntakeDay",
"InboundDocument",
+19 -1
View File
@@ -31,6 +31,12 @@ import {
type ProfileBackupCounts,
type ProfileBackupSection,
} from "@/lib/export/profile-backup";
import {
buildHealthScoreBackupSection,
countHealthScoreBackupSection,
type HealthScoreBackupCounts,
type HealthScoreBackupSection,
} from "@/lib/export/health-score-backup";
import {
buildIntradayProfileBackupSection,
countIntradayProfileBackupSection,
@@ -42,7 +48,8 @@ export interface FullBackupCounts
extends
RecordsBackupCounts,
ProfileBackupCounts,
IntradayProfileBackupCounts {
IntradayProfileBackupCounts,
HealthScoreBackupCounts {
measurements: number;
medications: number;
intakeEvents: number;
@@ -117,6 +124,7 @@ export async function buildFullBackupPayload(
records,
profile,
intradayProfiles,
healthScoreRecords,
nutrientDays,
] = await Promise.all([
disasterRecovery
@@ -225,6 +233,13 @@ export async function buildFullBackupPayload(
buildIntradayProfileBackupSection(prisma, userId, {
purpose: disasterRecovery ? "disaster-recovery" : "portable-export",
}),
// The score as it was SHOWN on each local day. Reads like a derived tier
// and is not one: today's number always comes from today's rows, so once
// the readings behind a past day have moved, nothing can recompute what
// that day said. The row is the only copy.
buildHealthScoreBackupSection(prisma, userId, {
purpose: disasterRecovery ? "disaster-recovery" : "portable-export",
}),
// Nutrient day totals were absent from every export path, which
// contradicted the schema's own reason for denormalising the unit column
// ("rows stay self-describing in exports even if the catalog ever drifts").
@@ -250,6 +265,7 @@ export async function buildFullBackupPayload(
const recordsSection: RecordsBackupSection = records;
const profileSection: ProfileBackupSection = profile;
const intradaySection: IntradayProfileBackupSection = intradayProfiles;
const healthScoreSection: HealthScoreBackupSection = healthScoreRecords;
const payload = {
schemaVersion: BACKUP_SCHEMA_VERSION,
@@ -396,6 +412,7 @@ export async function buildFullBackupPayload(
...recordsSection,
...profileSection,
...intradaySection,
...healthScoreSection,
nutrientDays: nutrientDays.map((n) => ({
day: n.day,
nutrient: n.nutrient,
@@ -418,6 +435,7 @@ export async function buildFullBackupPayload(
...countRecordsBackupSection(records),
...countProfileBackupSection(profile),
...countIntradayProfileBackupSection(intradayProfiles),
...countHealthScoreBackupSection(healthScoreRecords),
},
};
}
+206
View File
@@ -0,0 +1,206 @@
/**
* The stored health score days, with both backup ends in one file.
*
* `HealthScoreRecord` looks derived and is not. The live computation answers
* for today from today's rows, so a day whose readings have since been
* corrected, re-synced or simply extended cannot be recomputed as it stood;
* the row is the only copy of the number the account actually saw. Classifying
* it `DERIVED` and leaving it out would produce a restore that comes back with
* a blank history and reports success, which is the shape of defect the
* nutrient day totals, the custom mood tags and the custom cycle symptoms each
* shipped in turn.
*
* The builder and the restore live together deliberately, the way
* `intraday-profile-backup.ts` and `src/lib/cycle/backup.ts` do: a reader
* asking "is this carried at both ends?" answers it in one file, and a reader
* who greps only the restore ROUTE gets a false negative, because the route
* delegates.
*
* Nothing here is encrypted, so there is no portable/disaster-recovery split
* on the values. The only difference between the two purposes is identity: a
* canonical disaster-recovery payload carries the row id and its timestamps so
* the row comes back as itself, a portable export carries the day alone.
*/
import type { Prisma, PrismaClient } from "@/generated/prisma/client";
import type { ScoreBand } from "@/lib/analytics/score/types";
export interface HealthScoreBackupOptions {
purpose?: "portable-export" | "disaster-recovery";
}
/** One local day's score, exactly as it was shown. */
export interface HealthScoreRecordBackupEntry {
/** Present in canonical DR payloads so the row keeps a stable identity. */
id?: string;
/** Local calendar day (`YYYY-MM-DD`) in `timezone`. */
dayKey: string;
/**
* The zone the day was cut on. Carried for the same reason the intraday
* curves carry theirs: an account that moved zones has days of different
* lengths behind it, and a restore that guessed would make them look
* comparable.
*/
timezone: string;
composite: number;
band: ScoreBand;
scoreVersion: number;
composition: string[];
/** Each counted pillar's own score, keyed by pillar id. */
pillarScores: Record<string, number>;
inputFingerprint: string;
configVersion: number | null;
configChangedAt: string | null;
computedAt: string;
createdAt?: string;
}
export interface HealthScoreBackupSection {
healthScoreRecords: HealthScoreRecordBackupEntry[];
}
export interface HealthScoreBackupCounts {
healthScoreRecords: number;
}
/**
* Read the stored score days as JSON-safe entries.
*
* `pillarScores` arrives as an opaque `JsonValue`; it is narrowed here rather
* than cast, so a row holding something other than a flat id-to-number map
* reaches the payload as an empty map instead of as a shape the restore's
* schema would reject at the worst possible moment.
*/
function readPillarScores(value: Prisma.JsonValue): Record<string, number> {
if (typeof value !== "object" || value === null || Array.isArray(value)) {
return {};
}
const out: Record<string, number> = {};
for (const [key, entry] of Object.entries(value)) {
if (typeof entry === "number") out[key] = entry;
}
return out;
}
/**
* Build the stored-score slice of a user's full backup.
*
* Takes the delegate it uses rather than a whole `PrismaClient`, matching the
* other section builders, so the route's global client and the worker's local
* one share one read.
*/
export async function buildHealthScoreBackupSection(
prisma: Pick<PrismaClient, "healthScoreRecord">,
userId: string,
options: HealthScoreBackupOptions = {},
): Promise<HealthScoreBackupSection> {
const disasterRecovery = options.purpose === "disaster-recovery";
// The table carries no tombstone — a day is written once and never deleted
// short of a wipe — so both purposes read the same set.
const rows = await prisma.healthScoreRecord.findMany({
where: { userId },
orderBy: { dayKey: "desc" },
});
return {
healthScoreRecords: rows.map((row) => ({
...(disasterRecovery
? { id: row.id, createdAt: row.createdAt.toISOString() }
: {}),
dayKey: row.dayKey,
timezone: row.timezone,
composite: row.composite,
band: row.band as ScoreBand,
scoreVersion: row.scoreVersion,
composition: row.composition,
pillarScores: readPillarScores(row.pillarScores),
inputFingerprint: row.inputFingerprint,
configVersion: row.configVersion,
configChangedAt: row.configChangedAt?.toISOString() ?? null,
computedAt: row.computedAt.toISOString(),
})),
};
}
/** Row counts for the audit trail, mirroring the other section counters. */
export function countHealthScoreBackupSection(
section: HealthScoreBackupSection,
): HealthScoreBackupCounts {
return { healthScoreRecords: section.healthScoreRecords.length };
}
/** Counts the score-record restore wiped, for the audit trail. */
export interface HealthScoreRestoreCleared {
healthScoreRecords: number;
}
/**
* The slice of a parsed backup this restore consumes.
*
* Required rather than optional: `BackupPayload` defaults it, so every caller
* already satisfies this, and a caller that stops satisfying it fails to
* compile instead of passing `undefined` into a loop that iterates zero times
* and reports success.
*/
export interface HealthScoreRestoreInput {
healthScoreRecords: HealthScoreRecordBackupEntry[];
}
/**
* Re-create the account's stored score days.
*
* Delete-then-recreate inside the caller's transaction, matching every other
* section. Note the asymmetry with the live write path next door: THAT one
* yields on conflict, because a second computation of a day it already holds
* must change nothing. This one does not, because the file is the authority
* here and a day it carries twice is a corrupt file, not a race. Two entries
* claiming the same local day therefore throw with a sentence rather than
* letting `skipDuplicates` pick a winner and report success.
*/
export async function restoreHealthScoreData(
tx: Prisma.TransactionClient,
ownerId: string,
payload: HealthScoreRestoreInput,
): Promise<HealthScoreRestoreCleared> {
const cleared = await tx.healthScoreRecord.deleteMany({
where: { userId: ownerId },
});
const seen = new Set<string>();
for (const record of payload.healthScoreRecords) {
if (seen.has(record.dayKey)) {
throw new Error(
`Duplicate health score record for ${record.dayKey}. One account has ` +
"at most one score per local day, and picking a winner here would " +
"silently discard the other reading of that day.",
);
}
seen.add(record.dayKey);
}
if (payload.healthScoreRecords.length > 0) {
await tx.healthScoreRecord.createMany({
data: payload.healthScoreRecords.map((record) => ({
...(record.id ? { id: record.id } : {}),
userId: ownerId,
dayKey: record.dayKey,
timezone: record.timezone,
composite: record.composite,
band: record.band,
scoreVersion: record.scoreVersion,
composition: record.composition,
pillarScores: record.pillarScores as Prisma.InputJsonValue,
inputFingerprint: record.inputFingerprint,
configVersion: record.configVersion,
configChangedAt: record.configChangedAt
? new Date(record.configChangedAt)
: null,
computedAt: new Date(record.computedAt),
...(record.createdAt ? { createdAt: new Date(record.createdAt) } : {}),
})),
});
}
return { healthScoreRecords: cleared.count };
}
+31
View File
@@ -577,6 +577,30 @@ const intradayProfileBackupSchema = z
})
.passthrough();
const healthScoreRecordBackupSchema = z
.object({
id: z.string().min(1).optional(),
dayKey: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
// The zone the day was cut on. An account that moved zones has days of
// different lengths behind it, and a restore that guessed would make them
// look comparable.
timezone: z.string().min(1),
composite: z.number().int().min(0).max(100),
// The same closed set the column's CHECK constraint carries. A band the
// schema let through and the database refused would fail the whole
// restore transaction on one bad row.
band: z.enum(["green", "yellow", "red"]),
scoreVersion: z.number().int(),
composition: z.array(z.string().min(1)).min(1),
pillarScores: z.record(z.string(), z.number()),
inputFingerprint: z.string().regex(/^[0-9a-f]{64}$/),
configVersion: z.number().int().nullable().default(null),
configChangedAt: isoDateTime.nullable().default(null),
computedAt: isoDateTime,
createdAt: isoDateTime.optional(),
})
.passthrough();
const appSettingsBackupSchema = z
.object({
id: z.string().min(1),
@@ -849,6 +873,10 @@ export const backupPayloadSchema = z
// The hourly shape of a cumulative day. Defaulted for the same reason as
// the pair above: a file written before the table existed carries no key.
intradayProfiles: z.array(intradayProfileBackupSchema).default([]),
// The score as it was shown, day by day. Defaulted for the same reason as
// the sections above: a file written before the table existed carries no
// key, and an account whose score never resolved writes [].
healthScoreRecords: z.array(healthScoreRecordBackupSchema).default([]),
manifest: backupManifestSchema.nullable().default(null),
})
.passthrough()
@@ -913,6 +941,8 @@ export interface BackupSummary {
correlationPatterns: number;
/** Stored day curves for the cumulative metrics, across every metric. */
intradayProfiles: number;
/** Local days whose health score was written down as it was shown. */
healthScoreRecords: number;
}
export function summarizeBackup(payload: BackupPayload): BackupSummary {
@@ -947,6 +977,7 @@ export function summarizeBackup(payload: BackupPayload): BackupSummary {
),
correlationPatterns: payload.correlationPatterns.length,
intradayProfiles: payload.intradayProfiles.length,
healthScoreRecords: payload.healthScoreRecords.length,
};
}