Files
HealthLog/src/lib/illness/dto.ts
T
Marc-André Bombeck 14f0fc4d4f feat(body-sites): record a body site on conditions and add a body-site view
A condition (illness episode) can carry where on the body it sits, in the
person's own words and encrypted at rest, plus an optional side (LEFT,
RIGHT, BOTH). Same enum, codec and reasoning as the procedure's site.

- migration 0354: two nullable columns on illness_episodes, reusing the
  laterality type from 0349
- create and edit accept bodySite and laterality; on PATCH an absent key
  keeps the stored value, so an edit from the shipped iPhone app, which
  never sends either field, leaves them untouched
- GET /api/body-sites: the sites the record holds across visits and
  conditions, folded after the decrypt and bounded per kind; with site
  (and laterality) the visits and conditions there, one hop to their
  documents, lab results and visits. Read in the profile section;
  conditions only under an illness grant with the module on, and links
  into ungranted sections come back as unlabelled placeholders
- the link service reads document and visit links from the condition's
  end of the two existing tables
- /checkups gains a Body sites tab; the visit and condition forms share
  one body-site field pair that suggests sites already used; a
  condition's page links its site into the view
- the doctor report, the clinician page and the MCP doctor-visit summary
  name the site on conditions; the Coach illness block carries it
  sanitised, and MCP get_illness_recovery returns it fenced
- backup and restore carry both fields in both file kinds; the site is
  registered as ciphertext and rotates with the key
- i18n in all seven locales, OpenAPI regenerated

Refs #1025
2026-09-25 21:55:14 +02:00

122 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Illness row → wire-DTO mappers (v1.18.1).
*
* The canonical `IllnessEpisodeDTO` and `IllnessDayLogDTO` shapes the iOS
* client mirrors (server-authoritative — iOS renders, never recomputes).
* Free-text notes live in `noteEncrypted` (Bytes) and are decrypted
* fail-soft on read (a key-rotation gap on one row reads `null`, never
* 500s the whole page). Symptom links flatten to `[{ key, severity }]`
* using the catalog key; `severity` carries the persisted per-link 0–3
* Jackson/WURSS intensity (NULL = a plain presence link).
*/
import { decryptFromBytes } from "@/lib/ai/coach/bytes-codec";
import { getEvent } from "@/lib/logging/context";
import type {
IllnessEpisode,
IllnessDayLog,
Laterality,
IllnessSymptom,
IllnessSymptomLink,
} from "@/generated/prisma/client";
export interface IllnessSymptomDTO {
key: string;
severity: number | null;
}
export interface IllnessEpisodeDTO {
id: string;
label: string;
type: string;
lifecycle: string;
onsetAt: string;
resolvedAt: string | null;
parentConditionId: string | null;
note: string | null;
/**
* Where on the body, decrypted (v1.39.2). Null when not stated. Additive:
* a client that does not know the field ignores it, and an edit that does
* not send it keeps it.
*/
bodySite: string | null;
/** The side of `bodySite`, or null when not stated. */
laterality: Laterality | null;
createdAt: string;
updatedAt: string;
}
export interface IllnessDayLogDTO {
id: string;
episodeId: string;
date: string;
functionalImpact: number | null;
feverC: number | null;
symptoms: IllnessSymptomDTO[];
note: string | null;
updatedAt: string;
}
/** Decrypt a Bytes note fail-soft (null on missing / undecryptable). */
function decryptNote(
noteEncrypted: Uint8Array | null,
field: "note" | "body site" = "note",
): string | null {
if (!noteEncrypted || noteEncrypted.byteLength === 0) return null;
try {
return decryptFromBytes(noteEncrypted);
} catch (err) {
// Undecryptable note (key gap / corruption): fail soft to null but log it
// (F-CRYPTO-2) so a systemic key gap surfaces instead of reading as blank.
getEvent()?.addWarning(
`illness ${field} decrypt failed: ${
err instanceof Error ? err.message : String(err)
}`,
);
return null;
}
}
export function toIllnessEpisodeDTO(row: IllnessEpisode): IllnessEpisodeDTO {
return {
id: row.id,
label: row.label,
type: row.type,
lifecycle: row.lifecycle,
onsetAt: row.onsetAt.toISOString(),
resolvedAt: row.resolvedAt ? row.resolvedAt.toISOString() : null,
parentConditionId: row.parentConditionId,
note: decryptNote(row.noteEncrypted),
bodySite: decryptNote(row.bodySiteEncrypted, "body site"),
laterality: row.laterality,
createdAt: row.createdAt.toISOString(),
updatedAt: row.updatedAt.toISOString(),
};
}
type DayLogWithLinks = IllnessDayLog & {
symptomLinks?: (IllnessSymptomLink & {
symptom: Pick<IllnessSymptom, "key">;
})[];
};
/** Prisma `include` that flattens the symptom links for the DTO mapper. */
export const dayLogSymptomInclude = {
symptomLinks: { include: { symptom: { select: { key: true } } } },
} as const;
export function toIllnessDayLogDTO(row: DayLogWithLinks): IllnessDayLogDTO {
return {
id: row.id,
episodeId: row.episodeId,
date: row.date,
functionalImpact: row.functionalImpact,
feverC: row.feverC,
symptoms: (row.symptomLinks ?? []).map((l) => ({
key: l.symptom.key,
severity: l.severity ?? null,
})),
note: decryptNote(row.noteEncrypted),
updatedAt: row.updatedAt.toISOString(),
};
}