Merge remote-tracking branch 'origin/main' into fix/private-notification-origins

# Conflicts:
#	src/app/api/settings/ntfy/route.ts
#	src/app/api/settings/webhook/route.ts
This commit is contained in:
Marc-André Bombeck committed 2026-09-10 18:07:55 +02:00
commit 71175290af
255 files changed
+12654 -1141

No files matched your search

@@ -0,0 +1,105 @@
/**
* v1.39 (C1) — what `PATCH /api/onboarding/answers` will and will not accept.
*
* The union has an answer arm per step and a skip arm for six of the nine, and
* the split is a decision rather than a shape: `who` is the one required
* question, and `confirm` / `done` are acknowledgements. A skip arm added to
* one of those three would let a client record a pass through a screen that
* does not offer one, so the arms are checked against
* `ONBOARDING_SKIPPABLE_STEP_IDS` rather than against a list written twice.
*
* The other property here is that a skip can never carry an answer. That is
* what makes "skipped, and my areas are …" unrepresentable rather than merely
* discouraged, and it is why the route can trust the parsed body without
* re-checking the combination.
*/
import { describe, expect, it } from "vitest";
import {
ONBOARDING_SKIPPABLE_STEP_IDS,
ONBOARDING_STEP_IDS,
} from "@/lib/onboarding/needs";
import { onboardingAnswerSchema } from "../onboarding-needs";
const SKIPPABLE: readonly string[] = ONBOARDING_SKIPPABLE_STEP_IDS;
describe("onboardingAnswerSchema", () => {
it("accepts a skip for exactly the steps that may be passed", () => {
for (const step of ONBOARDING_STEP_IDS) {
const parsed = onboardingAnswerSchema.safeParse({
step,
status: "skipped",
});
expect(
parsed.success,
`\`${step}\` ${SKIPPABLE.includes(step) ? "should" : "should not"} be skippable`,
).toBe(SKIPPABLE.includes(step));
}
});
it("refuses a skip that carries an answer", () => {
expect(
onboardingAnswerSchema.safeParse({
step: "areas",
status: "skipped",
areas: ["sleep"],
}).success,
).toBe(false);
});
it("refuses an answer for the wrong step", () => {
expect(
onboardingAnswerSchema.safeParse({ step: "areas", medication: "yes" })
.success,
).toBe(false);
});
it("refuses a value outside the closed set", () => {
expect(
onboardingAnswerSchema.safeParse({ step: "visit", visit: "someday" })
.success,
).toBe(false);
expect(
onboardingAnswerSchema.safeParse({ step: "areas", areas: ["astrology"] })
.success,
).toBe(false);
});
it("refuses an unknown key alongside a valid answer", () => {
expect(
onboardingAnswerSchema.safeParse({
step: "who",
recordTarget: "me",
userId: "someone-else",
}).success,
).toBe(false);
});
it("accepts each answered step with its own payload", () => {
const bodies: unknown[] = [
{ step: "who", recordTarget: "both" },
{ step: "areas", areas: ["glucose", "cycle"] },
{ step: "medication", medication: "sometimes" },
{ step: "sources", sources: ["nightscout", "manual"] },
{ step: "visit", visit: "later" },
{ step: "units", units: { glucoseUnit: "mmol/L" } },
{ step: "confirm" },
{
step: "first-result",
firstResult: {
task: "log-reading",
target: "glucose",
completed: true,
},
},
{ step: "done" },
];
expect(bodies.length).toBe(ONBOARDING_STEP_IDS.length);
for (const body of bodies) {
expect(
onboardingAnswerSchema.safeParse(body).success,
JSON.stringify(body),
).toBe(true);
}
});
});
+34
View File
@@ -886,6 +886,33 @@ const healthScoreRecordBackupSchema = z
})
.passthrough();
/**
* v1.39 (C1) — the needs-based setup state. One row per record.
*
* The three answer objects are validated loosely on purpose: the closed
* vocabularies live in `src/lib/onboarding/needs.ts`, and the restore reads
* every one of them back through the same fail-soft parsers the account
* payload uses. Duplicating the enums here would give the file a second,
* quietly divergent opinion about what an answer is, and the failure mode of
* being stricter here is refusing a whole account's restore over one retired
* chip.
*/
const onboardingRecordBackupSchema = z
.object({
id: z.string().min(1).optional(),
needs: z.record(z.string(), z.unknown()).default({}),
steps: z.array(z.record(z.string(), z.unknown())).default([]),
firstResult: z.record(z.string(), z.unknown()).nullable().default(null),
// The once-only latch on the module derivation. Carried rather than
// defaulted: a null here would hand the next confirm permission to
// re-apply the questionnaire over every module decision made since.
modulesDerivedAt: isoDateTime.nullable().default(null),
completedAt: isoDateTime.nullable().default(null),
createdAt: isoDateTime.optional(),
updatedAt: isoDateTime.optional(),
})
.passthrough();
const appSettingsBackupSchema = z
.object({
id: z.string().min(1),
@@ -1668,6 +1695,10 @@ export const backupPayloadSchema = z
// 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([]),
// The needs-based setup answers. Defaulted to null for the same reason as
// the sections above: a file written before the table existed carries no
// key, and a record that never entered the flow has no row.
onboardingRecord: onboardingRecordBackupSchema.nullable().default(null),
// Visits, the address book behind them, and the three link tables.
// Defaulted for the same reason as the sections above: a file written
// before the tables existed carries no key, and an account that has never
@@ -1796,6 +1827,8 @@ export interface BackupSummary {
intradayProfiles: number;
/** Local days whose health score was written down as it was shown. */
healthScoreRecords: number;
/** 1 when the record's setup answers ride the file, 0 otherwise. */
onboardingRecords: number;
/** v1.37.19 (A6-8) — the visit address book. */
practitioners: number;
/** v1.37.19 (A6-8) — doctor visits, planned and past. */
@@ -1884,6 +1917,7 @@ export function summarizeBackup(payload: BackupPayload): BackupSummary {
correlationPatterns: payload.correlationPatterns.length,
intradayProfiles: payload.intradayProfiles.length,
healthScoreRecords: payload.healthScoreRecords.length,
onboardingRecords: payload.onboardingRecord ? 1 : 0,
// v1.37.19 (A6-8) — the sections restored since 08-01 were written and
// restored but absent from this report, so the admin's "what did I just
// restore" answer silently under-counted a file that carried visits or
+119
View File
@@ -0,0 +1,119 @@
/**
* v1.39 (C1) — request schemas for the three needs-onboarding writes.
*
* They live outside the route files for the reason `./onboarding.ts` gives: a
* route module may only export handlers plus the Next.js route config, so the
* OpenAPI registry cannot import a schema declared inside one. Keeping the
* shapes here makes the published contract and the runtime parser the same
* object.
*
* Prisma-free by construction — everything below reads its vocabulary from
* `@/lib/onboarding/needs` and `@/lib/modules/registry`, both pure data.
*/
import { z } from "zod/v4";
import { ONBOARDING_AREA_KEYS } from "@/lib/modules/registry";
import {
ONBOARDING_FIRST_RESULT_TASKS,
ONBOARDING_MEDICATION_ANSWERS,
ONBOARDING_RECORD_TARGETS,
ONBOARDING_SOURCE_KEYS,
ONBOARDING_VISIT_ANSWERS,
} from "@/lib/onboarding/needs";
/**
* One answered step. `status` is optional and only ever `"done"` on this arm —
* a skip is the separate arm below, so a body can never claim to skip a step
* and carry an answer for it at the same time.
*/
function answered<Id extends string, Shape extends z.ZodRawShape>(
id: Id,
shape: Shape,
) {
return z
.object({
step: z.literal(id),
status: z.literal("done").optional(),
...shape,
})
.strict();
}
/** One deliberately passed step. No answer may ride along. */
function skipped<Id extends string>(id: Id) {
return z
.object({ step: z.literal(id), status: z.literal("skipped") })
.strict();
}
/**
* Q6 — the unit answers. Both fields are optional and nullable: the screen
* only appears when glucose or weight is among the areas, and it may set one
* without the other. The route writes the record's real display columns from
* these and records the answer beside them.
*/
export const onboardingUnitsSchema = z
.object({
glucoseUnit: z.enum(["mg/dL", "mmol/L"]).nullable().optional(),
unitPreference: z.enum(["metric", "imperial"]).nullable().optional(),
})
.strict();
/**
* The task the flow ended on. `completed` is what turns the offer into a
* result — the server stamps the instant rather than taking one from the
* client, so a replay cannot backdate it.
*/
export const onboardingFirstResultSchema = z
.object({
task: z.enum(ONBOARDING_FIRST_RESULT_TASKS),
target: z.string().trim().min(1).max(64).nullable().optional(),
completed: z.boolean().optional(),
})
.strict();
/**
* `PATCH /api/onboarding/answers` — one step at a time.
*
* A flat union rather than a discriminated one: each step id appears twice
* (answered and skipped) and `z.discriminatedUnion` needs one option per
* literal. The three steps that are not questions — `who` is required, and
* `confirm` / `done` are acknowledgements — have no skip arm, so a client
* cannot record a pass through a screen that does not offer one.
*/
export const onboardingAnswerSchema = z.union([
answered("who", { recordTarget: z.enum(ONBOARDING_RECORD_TARGETS) }),
answered("areas", {
areas: z
.array(z.enum(ONBOARDING_AREA_KEYS))
.max(ONBOARDING_AREA_KEYS.length),
}),
answered("medication", {
medication: z.enum(ONBOARDING_MEDICATION_ANSWERS),
}),
answered("sources", {
sources: z
.array(z.enum(ONBOARDING_SOURCE_KEYS))
.max(ONBOARDING_SOURCE_KEYS.length),
}),
answered("visit", { visit: z.enum(ONBOARDING_VISIT_ANSWERS) }),
answered("units", { units: onboardingUnitsSchema }),
answered("confirm", {}),
answered("first-result", { firstResult: onboardingFirstResultSchema }),
answered("done", {}),
skipped("areas"),
skipped("medication"),
skipped("sources"),
skipped("visit"),
skipped("units"),
skipped("first-result"),
]);
export type OnboardingAnswerInput = z.infer<typeof onboardingAnswerSchema>;
/**
* `POST /api/onboarding/restart` — no input beyond the session. Strict and
* empty rather than unvalidated, so a client that starts sending a field gets
* a 422 that names it instead of having it silently ignored.
*/
export const onboardingRestartSchema = z.object({}).strict();