feat(api): add emergency profile read and write route

GET/PATCH /api/anamnesis/emergency, wrapped in apiHandler. The PATCH
validates a partial body with per-field length caps, builds the Prisma
data field-by-field from the parsed input (never a spread), narrows the
user from requireAuth rather than the body, encrypts the free text on
write, and audits the change. The GET decrypts fail-soft for the form
prefill. Registers the OpenAPI operations and schema. An integration
test reads the row back to prove the write lands.
This commit is contained in:
Marc-André Bombeck committed 2026-08-10 05:32:59 +02:00
1 parent 772da42348
commit 6770bf3b7f
7 files changed
+788 -3

No files matched your search

+64
View File
@@ -16,6 +16,10 @@ import {
healthProfileFactWriteSchema,
removedHealthProfileFactSchema,
} from "@/lib/validations/health-profile-facts";
import {
emergencyProfileDtoSchema,
emergencyProfileUpdateSchema,
} from "@/lib/validations/emergency-profile";
import {
ACCEPTED_INSIGHTS_TILE_IDS,
INSIGHTS_SECTION_IDS,
@@ -51,6 +55,18 @@ import {
stdResponses,
} from "./shared";
emergencyProfileUpdateSchema.meta({
id: "UpdateEmergencyProfileRequest",
description:
"Partial edit of the emergency (Notfalldaten) profile. An omitted key leaves the column untouched; a `null` enum or an emptied free-text field clears it. `bloodType`, `organDonor` and `advanceDirective` are closed enums; `contacts`, `implants` and `note` are encrypted at rest. Rejects unknown keys.",
});
emergencyProfileDtoSchema.meta({
id: "EmergencyProfile",
description:
"The caller's emergency profile: three closed-enum facts plus three decrypted free-text fields. A free-text field is null when unset; its `*Unreadable` flag is true when ciphertext was present but could not be decrypted (a key-rotation gap, fail-soft rather than 500).",
});
// ── Coach cadence suggestions (v1.18.1) ──────────────────────────────
// The action endpoint behind the one-tap reminder-suggestion card. The
// client sends ONLY the cadence id + the action; the server resolves the
@@ -1360,6 +1376,54 @@ export const coachPaths: NonNullable<ZodOpenApiObject["paths"]> = {
},
},
},
"/api/anamnesis/emergency": {
get: {
tags: ["Insights"],
summary: "Read the emergency profile",
description:
"Returns the caller's emergency (Notfalldaten) profile: blood type, organ-donor and advance-directive declarations, and the decrypted emergency contacts, implants/devices and ICE note. Free text decrypts fail-soft.",
responses: {
"200": {
description: "The emergency profile (fields null when unset).",
content: {
"application/json": {
schema: dataEnvelope(
emergencyProfileDtoSchema,
"GetEmergencyProfileResponse",
),
},
},
},
...stdResponses,
},
},
patch: {
tags: ["Insights"],
summary: "Update the emergency profile",
description:
"Partial edit of the emergency profile on the caller's own record. Free-text fields are AES-256-GCM encrypted before persistence. Audits as `anamnesis.emergency.update`.",
requestBody: {
required: true,
content: {
"application/json": { schema: emergencyProfileUpdateSchema },
},
},
responses: {
"200": {
description: "The updated emergency profile.",
content: {
"application/json": {
schema: dataEnvelope(
emergencyProfileDtoSchema,
"UpdateEmergencyProfileResponse",
),
},
},
},
...stdResponses,
},
},
},
"/api/coach/about-me/questions": {
get: {
tags: ["Insights"],