Let a person name their own medication categories

A medication can be filed under a category the person named, next to the
built-in ones (#1041). The labels live in medication_category_labels,
encrypted at rest, capped at 20 per account; a medication filed under one
carries its custom:<uuid> key in the category column.

- GET/POST /api/medications/categories (POST idempotent, 30/min per actor,
  admitted at MANAGE like the cycle symptom vocabulary) and owner-only
  PATCH/DELETE /api/medications/categories/{key}. DELETE moves every
  medication filed under the category to OTHER in the same transaction
  and answers movedCount.
- category is widened to a built-in value or custom:<uuid> on the wire,
  the create and update bodies refuse a key that is not the caller's
  (422 medications.category.unknown), and list, detail, the insights
  medication tiles and the MCP medications resource carry categoryLabel.
  A key with no owned label reads as OTHER and is counted on the event.
- The wizard's class step lists the person's categories under the
  built-in rows with an inline add row; the list page gains a category
  filter (session only) and a manage sheet for rename, hide and delete.
- Backup carries customMedicationCategories (decrypted labels) and the
  restore writes them before the medications; wipe plan, encrypted
  column registry, rotation script, sharing guards and OpenAPI follow.
This commit is contained in:
Marc-André Bombeck committed 2026-10-03 21:38:03 +02:00
1 parent 350c646463
commit 4cb8118802
55 files changed
+3201 -187

No files matched your search

+22 -1
View File
@@ -333,6 +333,22 @@ const medicationEfficacyTargetSchema = z
})
.passthrough();
/**
* v1.40 (#1041) — a medication category the account named itself. Carried
* with its decrypted label (the label is re-encrypted on restore under the
* restoring instance's key) and restored BEFORE the medications, whose
* `category` names it by `key`.
*/
const customMedicationCategorySchema = z
.object({
key: z.string().regex(/^custom:[0-9a-f-]{36}$/),
label: z.string().trim().min(1).max(40),
sortOrder: z.number().int().min(0).default(0),
isActive: z.boolean().default(true),
createdAt: isoDateTime.optional(),
})
.passthrough();
const medicationSchema = z
.object({
id: z.string().min(1).optional(),
@@ -361,7 +377,8 @@ const medicationSchema = z
// v1.39.4 — the clinical category. Absent in older files. A string, not
// the enum: a file from a newer server may name a category this one does
// not know, and the restore reads that as OTHER rather than refusing the
// whole file.
// whole file. Since v1.40 it may be `custom:<uuid>`, the key of an entry
// in `customMedicationCategories`.
category: z.string().max(64).optional(),
deliveryForm: z.enum(MedicationDeliveryForm).optional(),
trackInjectionSites: z.boolean().optional(),
@@ -1834,6 +1851,10 @@ export const backupPayloadSchema = z
accountSettings: accountSettingsBackupSchema.nullable().default(null),
measurements: z.array(measurementSchema).default([]),
medications: z.array(medicationSchema).default([]),
// v1.40 — absent in older files, which carry no custom category either.
customMedicationCategories: z
.array(customMedicationCategorySchema)
.default([]),
intakeEvents: z.array(intakeEventSchema).default([]),
moodEntries: z.array(moodEntrySchema).default([]),
// v1.15.0 — cycle-tracking tables. Default to empty arrays / null so a
+59 -2
View File
@@ -63,8 +63,8 @@ export const rxNormCodeField = z
"Optional RxNorm RxCUI (numeric, US identifier, e.g. `2601723`). Secondary coding emitted under `http://www.nlm.nih.gov/research/umls/rxnorm` alongside any ATC code, never instead of the free-text name. NULL clears it; absent leaves it untouched.",
);
/**
* Clinical-category values stored in the `medication_categories` side-
* table (TEXT column). v1.5.4 adds `DIABETES` and `ANTIBIOTIC` so the
* Clinical-category values stored in the `medication_categories` table
* (TEXT column, the `MedicationCategoryAssignment` model). v1.5.4 adds `DIABETES` and `ANTIBIOTIC` so the
* wizard's Step 2 taxonomy can write a first-class bucket for those
* two rows instead of collapsing them into `OTHER`. The column is a
* plain TEXT field — no Prisma enum exists to migrate; the Zod values
@@ -91,6 +91,63 @@ export const MEDICATION_CATEGORY_VALUES = [
export type MedicationCategoryValue =
(typeof MEDICATION_CATEGORY_VALUES)[number];
/**
* v1.40 (#1041) — a category the person named themselves is addressed by the
* key of its `MedicationCategoryLabel` row, `custom:<uuid>`. The `category`
* field on the medication wire is a built-in value or such a key.
*/
export const CUSTOM_MEDICATION_CATEGORY_PREFIX = "custom:";
/** Per-account ceiling on custom medication categories (hidden ones count). */
export const MAX_CUSTOM_MEDICATION_CATEGORIES = 20;
const CUSTOM_MEDICATION_CATEGORY_KEY = /^custom:[0-9a-f-]{36}$/;
/** Is `value` a custom-category key rather than a built-in value? */
export function isCustomMedicationCategoryKey(value: string): boolean {
return value.startsWith(CUSTOM_MEDICATION_CATEGORY_PREFIX);
}
/**
* The `category` a create or update body may carry: a built-in value or a
* well-formed custom key. Ownership of a custom key is checked by the route
* (422 `medications.category.unknown`), not here.
*/
export const medicationCategoryInputSchema = z
.union([
z.enum(MEDICATION_CATEGORY_VALUES),
z.string().regex(CUSTOM_MEDICATION_CATEGORY_KEY, "Unknown category"),
])
.describe(
"A built-in clinical category (BLOOD_PRESSURE, VITAMIN, SUPPLEMENT, PAIN_RELIEF, ALLERGY, DIGESTIVE, THYROID, HORMONE, SKIN, SLEEP_AID, DIABETES, ANTIBIOTIC, MENTAL_HEALTH, OTHER) or `custom:<uuid>`, the key of one of the caller's own categories.",
);
const customCategoryLabelSchema = z
.string()
.trim()
.min(1, "Label must not be empty")
.max(40, "Label must be at most 40 characters");
/** `POST /api/medications/categories` body. */
export const createMedicationCategoryLabelSchema = z.object({
label: customCategoryLabelSchema,
});
/** `PATCH /api/medications/categories/{key}` body: at least one field. */
export const updateMedicationCategoryLabelSchema = z
.object({
label: customCategoryLabelSchema.optional(),
sortOrder: z.number().int().min(0).max(1000).optional(),
isActive: z.boolean().optional(),
})
.refine(
(v) =>
v.label !== undefined ||
v.sortOrder !== undefined ||
v.isActive !== undefined,
"At least one field is required",
);
/**
* v1.4.25 W4d — Prisma-level treatment class. Orthogonal to
* `MEDICATION_CATEGORY_VALUES` (the clinical taxonomy that lives in the
@@ -1,7 +1,7 @@
import { z } from "zod/v4";
import {
MEDICATION_CATEGORY_VALUES,
medicationCategoryInputSchema,
MEDICATION_DELIVERY_FORM_VALUES,
MEDICATION_TREATMENT_CLASS_VALUES,
atcCodeField,
@@ -99,7 +99,7 @@ export const createMedicationSchema = z
.object({
name: z.string().min(1).max(100),
dose: z.string().min(1).max(50),
category: z.enum(MEDICATION_CATEGORY_VALUES).optional(),
category: medicationCategoryInputSchema.optional(),
/** v1.4.25 W4d — treatment-class discriminator (GENERIC | GLP1). */
treatmentClass: z.enum(MEDICATION_TREATMENT_CLASS_VALUES).optional(),
/** v1.4.25 W4d — doses per pen/vial for inventory tracking.
@@ -267,7 +267,7 @@ export const updateMedicationSchema = z
.object({
name: z.string().min(1).max(100).optional(),
dose: z.string().min(1).max(50).optional(),
category: z.enum(MEDICATION_CATEGORY_VALUES).optional(),
category: medicationCategoryInputSchema.optional(),
treatmentClass: z.enum(MEDICATION_TREATMENT_CLASS_VALUES).optional(),
dosesPerUnit: z.number().int().min(1).max(1000).nullable().optional(),
/** v1.16.10 — inventory units one dose consumes. Default 1.