feat(nutrients): carry daily targets through backup, wipe and the API contract

Nutrient targets travel in the account-settings slice of the full
backup and are admitted code by code on restore, so an unknown code or
an out-of-range value is dropped and named while the rest come back. A
data wipe clears them. The targets route joins the OpenAPI registry,
and the regenerated contract carries the wider nutrient enums, the new
sources and the target and authority fields. Refs #1402.
This commit is contained in:
Marc-André Bombeck committed 2026-11-17 19:05:31 +01:00
1 parent 7c40ba8202
commit db0e4f9688
18 files changed
+1470 -381

No files matched your search

@@ -124,6 +124,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"reportSelectionJson": {
"roundTrip": "rt-User-reportSelectionJson"
},
@@ -2571,6 +2581,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"reportSelectionJson": {
"roundTrip": "rt-User-reportSelectionJson"
},
@@ -67,6 +67,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"reportSelectionJson": {
"roundTrip": "rt-User-reportSelectionJson"
},
@@ -1737,6 +1747,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"reportSelectionJson": {
"roundTrip": "rt-User-reportSelectionJson"
},
@@ -335,7 +335,7 @@
"symptomDefinitions": 2,
"lifeEvents": 3
},
"accountSettingsApplied": 61,
"accountSettingsApplied": 62,
"skipped": {
"catalogueKeys": [],
"links": 0
@@ -335,7 +335,7 @@
"symptomDefinitions": 0,
"lifeEvents": 0
},
"accountSettingsApplied": 61,
"accountSettingsApplied": 62,
"skipped": {
"catalogueKeys": [],
"links": 0
@@ -2787,6 +2787,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"glucoseUnit": "mmol/L",
"hasDiabetes": true,
"labsLocalOcrEnabled": true,
@@ -3046,6 +3056,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"reportSelectionJson": {
"roundTrip": "rt-User-reportSelectionJson"
},
@@ -381,7 +381,7 @@
"symptomDefinitions": 0,
"lifeEvents": 0
},
"accountSettingsApplied": 61,
"accountSettingsApplied": 62,
"skipped": {
"catalogueKeys": [
{
@@ -2281,6 +2281,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"glucoseUnit": "mmol/L",
"hasDiabetes": true,
"labsLocalOcrEnabled": true,
@@ -2483,6 +2493,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"reportSelectionJson": {
"roundTrip": "rt-User-reportSelectionJson"
},
@@ -909,6 +909,7 @@
"moodTagLayoutJson": null,
"moduleListLayoutJson": null,
"activityGoalsJson": null,
"nutrientTargetsJson": null,
"glucoseUnit": null,
"hasDiabetes": false,
"labsLocalOcrEnabled": false,
@@ -1043,6 +1043,7 @@
"moodTagLayoutJson": null,
"moduleListLayoutJson": null,
"activityGoalsJson": null,
"nutrientTargetsJson": null,
"glucoseUnit": null,
"hasDiabetes": false,
"labsLocalOcrEnabled": false,
@@ -1254,6 +1255,7 @@
"documentsLayoutJson": null,
"moduleListLayoutJson": null,
"activityGoalsJson": null,
"nutrientTargetsJson": null,
"reportSelectionJson": null,
"globalExcludedInjectionSites": [],
"healthKitConfigJson": null,
@@ -2402,6 +2402,16 @@
"stepsPerMonth": 250000,
"workoutsPerWeek": 4
},
"nutrientTargetsJson": {
"sugar": {
"value": 40,
"direction": "limit"
},
"protein": {
"value": 120,
"direction": "target"
}
},
"glucoseUnit": "mmol/L",
"hasDiabetes": true,
"labsLocalOcrEnabled": true,
+5
View File
@@ -1571,6 +1571,11 @@ export const USER_FILL_OVERRIDES: Readonly<Record<string, unknown>> = {
// The restore admits goal by goal against the goals schema, so the generic
// `{ roundTrip }` blob would be refused as an unknown key, not carried.
"User.activityGoalsJson": { workoutsPerWeek: 4, stepsPerMonth: 250_000 },
// Admitted code by code against the target schema, like the goals above.
"User.nutrientTargetsJson": {
protein: { value: 120, direction: "target" },
sugar: { value: 40, direction: "limit" },
},
"User.globalExcludedInjectionSites": ["THIGH_LEFT"],
"User.avatarBytes": () => {
const bytes = new Uint8Array(new ArrayBuffer(AVATAR_PNG.byteLength));
@@ -0,0 +1,130 @@
/**
* `GET` / `PATCH /api/nutrients/targets` and the target the daily series
* carries, against the real database and through the real API kit (#1402).
*/
import { beforeEach, describe, expect, it } from "vitest";
import type { User } from "@/generated/prisma/client";
import { GET, PATCH } from "@/app/api/nutrients/targets/route";
import { GET as GET_DAILY } from "@/app/api/nutrients/daily/route";
import { MEASUREMENTS_WRITE_SCOPE } from "@/lib/measurements/scopes";
import { callRoute, type RouteAuth } from "../../kit/call-route";
import { makeSession, makeToken, makeUser } from "../../kit/factories";
import { getPrismaClient, truncateAllTables } from "../setup";
const prisma = getPrismaClient();
const ROUTE = "/api/nutrients/targets";
type Targets = Record<string, { value: number; direction: string }>;
beforeEach(() => truncateAllTables(prisma));
function nutrientUser(): Promise<User> {
return makeUser({ modulePreferencesJson: { nutrients: true } });
}
function patch(targets: Record<string, unknown>, auth: RouteAuth) {
return callRoute<{ targets: Targets }>(PATCH, {
method: "PATCH",
route: ROUTE,
body: { targets },
auth,
});
}
async function stored(user: Pick<User, "id">) {
const row = await prisma.user.findUnique({
where: { id: user.id },
select: { nutrientTargetsJson: true },
});
return row?.nutrientTargetsJson ?? null;
}
describe("/api/nutrients/targets", () => {
it("sets, merges and clears targets code by code", async () => {
const user = await nutrientUser();
const session = await makeSession(user);
const first = await patch(
{
protein: { value: 120, direction: "target" },
sugar: { value: 40, direction: "limit" },
},
session,
);
expect(first.status).toBe(200);
const second = await patch(
{ sugar: null, energy: { value: 2200, direction: "target" } },
session,
);
expect(second.data.targets).toEqual({
energy: { value: 2200, direction: "target" },
protein: { value: 120, direction: "target" },
});
expect(await stored(user)).toEqual(second.data.targets);
const read = await callRoute<{ targets: Targets }>(GET, {
method: "GET",
route: ROUTE,
auth: session,
});
expect(read.data.targets).toEqual(second.data.targets);
await patch({ energy: null, protein: null }, session);
expect(await stored(user)).toBeNull();
});
it("refuses an unknown code or an out-of-range value with 422 and stores nothing", async () => {
const user = await nutrientUser();
const session = await makeSession(user);
for (const targets of [
{ creatine: { value: 5, direction: "target" } },
{ protein: { value: 5000, direction: "target" } },
]) {
const res = await patch(targets, session);
expect(res.status, JSON.stringify(targets)).toBe(422);
}
expect(await stored(user)).toBeNull();
});
it("refuses with module.disabled while the module is off", async () => {
const user = await makeUser();
const res = await patch(
{ protein: { value: 120, direction: "target" } },
await makeSession(user),
);
expect(res.status).toBe(403);
expect(res.meta?.errorCode).toBe("module.disabled");
});
it("refuses a measurements:write token: a bridge cannot set targets", async () => {
const user = await nutrientUser();
const res = await patch(
{ protein: { value: 120, direction: "target" } },
await makeToken(user, { permissions: [MEASUREMENTS_WRITE_SCOPE] }),
);
expect(res.status).toBe(403);
expect(await stored(user)).toBeNull();
});
it("returns the code's target on the daily series, and null for a code without one", async () => {
const user = await nutrientUser();
const session = await makeSession(user);
await patch({ protein: { value: 120, direction: "target" } }, session);
const daily = (nutrient: string) =>
callRoute<{ personalTarget: unknown }>(GET_DAILY, {
method: "GET",
route: "/api/nutrients/daily",
query: { nutrient, days: 7 },
auth: session,
});
expect((await daily("protein")).data.personalTarget).toEqual({
value: 120,
direction: "target",
});
expect((await daily("water")).data.personalTarget).toBeNull();
});
});
+29 -1
View File
@@ -33,6 +33,7 @@ import {
import { GET as getMe } from "@/app/api/auth/me/route";
import * as profile from "@/app/api/user/profile/route";
import * as nutrientTargets from "@/app/api/nutrients/targets/route";
import * as diabetes from "@/app/api/auth/me/diabetes/route";
import * as autoAiRead from "@/app/api/auth/me/documents-auto-ai-read/route";
import * as labsOcr from "@/app/api/auth/me/labs-local-ocr/route";
@@ -449,12 +450,39 @@ function entries(user: Pick<User, "id">): Entry[] {
handler: modules.PATCH,
method: "PATCH",
route: "/api/auth/me/modules",
body: { mood: false },
// Nutrients on as well: the nutrient-target entry below is behind it.
body: { mood: false, nutrients: true },
},
read: get(modules.GET, "/api/auth/me/modules", "modules.mood"),
expected: false,
me: { pick: (me) => at(me, "modules.mood"), expected: false },
},
{
columns: ["nutrientTargetsJson"],
write: {
handler: nutrientTargets.PATCH,
method: "PATCH",
route: "/api/nutrients/targets",
body: {
targets: {
protein: { value: 120, direction: "target" },
sugar: { value: 40, direction: "limit" },
},
},
},
read: {
call: {
handler: nutrientTargets.GET,
method: "GET",
route: "/api/nutrients/targets",
},
pick: (res) => [
at(res.data, "targets.protein.value"),
at(res.data, "targets.sugar.direction"),
],
},
expected: [120, "limit"],
},
{
columns: ["healthScoreConfigJson"],
write: {