fix(backend): flatten medication serialization (MedicationResponse)

Backend + frontend. Resolves the integration gap where the medication list/create
responses were deeply nested (fields inside medicationData.data JSON blob,
camelCase) while the frontend expected flat snake_case fields — so the
MedicationManager couldn't display real data.

Backend (models/medication.rs):
* New MedicationData struct: deserializes the camelCase JSON string stored in
  medicationData.data (sideEffects->side_effects, prescribedBy->prescribed_by,
  etc.). Tolerates missing keys via #[default].
* New MedicationResponse: flat snake_case, the wire format. From<Medication>
  deserializes the data blob, maps fields, synthesizes active=true (TODO: real
  active flag), formats timestamps as ISO 8601. Tolerates malformed blobs.
* Fixed MedicationRepository::update: the old dot-notation (medicationData.name)
  wrote into phantom paths because the stored shape is medicationData:{data:
  '<string>'}. Now loads the doc, deserializes the blob, applies overrides,
  re-serializes, and $sets the whole data string. Updates actually persist now.
* 4 new unit tests (MedicationData deser, defaults, MedicationResponse flatten,
  malformed-blob tolerance).

Handlers (handlers/medications.rs): create/list/get/update now return
MedicationResponse instead of the raw Medication model.

Frontend (types/api.ts): Medication interface reconciled to match
MedicationResponse (added id, profile_id, route, reason, side_effects,
prescribed_by/date, notes, tags; relaxed user_id to optional).

Verified: backend cargo fmt/build/clippy 0 warnings, 23 unit tests pass (was 19);
frontend npm build clean, 20 vitest tests pass.
This commit is contained in:
goose 2026-06-28 11:30:01 -03:00
parent 68a50b0457
commit d41256e05b
3 changed files with 288 additions and 45 deletions

View file

@ -111,14 +111,23 @@ export interface PillIdentification {
}
export interface Medication {
id?: string;
medication_id?: string;
user_id: string;
user_id?: string;
profile_id?: string;
name: string;
dosage: string;
frequency: string;
route?: string;
reason?: string;
instructions?: string;
side_effects?: string[];
prescribed_by?: string;
prescribed_date?: string;
start_date?: string;
end_date?: string;
instructions?: string;
notes?: string;
tags?: string[];
active: boolean;
pill_identification?: PillIdentification;
created_at?: string;