mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
chore(release): v1.6.0
Medication editor overhaul, route of administration, today-tile read-flip. - Route-of-administration column (ORAL | INJECTION | OTHER) decoupled from treatmentClass; injection-site picker surfaces for any injection. Migration 0088 backfills ORAL via a constant column default. - One-time injection (oneShot + deliveryForm = INJECTION) editor path. - Profile-photo upload card on Settings → Account. - Today-tile read-flip: /api/medications/intake?scope=today and /api/dashboard/summary gate through the canonical recurrence engine, matching the reminder worker for bi-weekly, rolling, RRULE, and one-time cadences. - Measurement note cap 25 → 200; dashboard range colors on semantic tokens; admin sidebar icons + shared-helper consolidation; API-tokens multi-language snippets restored. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d01d1d1dc9
commit
8ee7aaac44
53 files changed
+1049
-1017
No files matched your search
@@ -45,7 +45,6 @@ export const DOSE_UNITS = [
|
||||
"puffs",
|
||||
"sprays",
|
||||
] as const;
|
||||
export type DoseUnit = (typeof DOSE_UNITS)[number];
|
||||
|
||||
/** `CadenceKind` from the wizard types, mirrored as a Zod-friendly enum. */
|
||||
export const CADENCE_KINDS = [
|
||||
|
||||
@@ -13,7 +13,8 @@
|
||||
* new schedule cadence, audit metadata) only have to land once.
|
||||
*
|
||||
* The helper is intentionally side-effect-only:
|
||||
* 1. Projects the active schedules through `expandTodayIntakes`.
|
||||
* 1. Projects the active schedules through the canonical recurrence
|
||||
* engine (`scheduleEmitsInWindow`), minting at `windowStart`.
|
||||
* 2. Reads existing rows in the today-window.
|
||||
* 3. Inserts the missing rows (`skipDuplicates: true` for the
|
||||
* `(userId, medicationId, scheduledFor, source)` unique index).
|
||||
@@ -28,7 +29,12 @@
|
||||
* Returns the counts so the caller can surface them on `annotate(...)`.
|
||||
*/
|
||||
import { prisma } from "@/lib/db";
|
||||
import { expandTodayIntakes } from "@/lib/medication-schedule";
|
||||
import {
|
||||
buildCanonicalSchedule,
|
||||
buildRecurrenceContext,
|
||||
scheduleEmitsInWindow,
|
||||
} from "@/lib/medications/scheduling/worker-helpers";
|
||||
import { localHmAsUtc } from "@/lib/timezone";
|
||||
import { recomputeMedicationComplianceForEvent } from "@/lib/rollups/medication-compliance-rollups";
|
||||
|
||||
export interface ProjectTodayIntakesResult {
|
||||
@@ -48,6 +54,10 @@ export async function projectTodayIntakesAndRecompute(input: {
|
||||
where: { userId, active: true },
|
||||
select: {
|
||||
id: true,
|
||||
startsOn: true,
|
||||
endsOn: true,
|
||||
oneShot: true,
|
||||
createdAt: true,
|
||||
schedules: {
|
||||
select: {
|
||||
id: true,
|
||||
@@ -55,16 +65,61 @@ export async function projectTodayIntakesAndRecompute(input: {
|
||||
windowStart: true,
|
||||
windowEnd: true,
|
||||
daysOfWeek: true,
|
||||
timesOfDay: true,
|
||||
reminderGraceMinutes: true,
|
||||
rrule: true,
|
||||
rollingIntervalDays: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const projected = expandTodayIntakes(
|
||||
activeMedications.flatMap((m) => m.schedules),
|
||||
new Date(),
|
||||
userTz,
|
||||
);
|
||||
// v1.6.0 read-flip — gate every "does this schedule emit today?"
|
||||
// decision through the canonical recurrence engine (the same path the
|
||||
// reminder worker uses). The legacy `expandTodayIntakes` walker read
|
||||
// only `daysOfWeek` + `windowStart` and silently skipped
|
||||
// `intervalWeeks > 1`, rolling, RRULE, and one-shot cadences — so the
|
||||
// dashboard / intake today-tile diverged from what the worker minted
|
||||
// for bi-weekly (GLP-1), rolling, and RRULE-only schedules. The
|
||||
// `scheduledFor` instant stays anchored to `windowStart` so it remains
|
||||
// byte-identical to the worker's RED-phase row and dedupes against the
|
||||
// `@@unique([userId, medicationId, scheduledFor, source])` index.
|
||||
const now = new Date();
|
||||
const projected: Array<{ medicationId: string; scheduledFor: Date }> = [];
|
||||
|
||||
for (const med of activeMedications) {
|
||||
if (med.schedules.length === 0) continue;
|
||||
|
||||
// Rolling cadence anchors off the last logged intake. One findFirst
|
||||
// per medication, scoped to `takenAt IS NOT NULL` — byte-identical to
|
||||
// the reminder worker's baseline (`reminder-worker.ts`), so projector
|
||||
// and worker resolve the same next-due instant and never mint
|
||||
// divergent `(med, scheduledFor)` rows.
|
||||
let lastIntakeAt: Date | null = null;
|
||||
if (med.schedules.some((s) => s.rollingIntervalDays !== null)) {
|
||||
const lastIntake = await prisma.medicationIntakeEvent.findFirst({
|
||||
where: { userId, medicationId: med.id, takenAt: { not: null } },
|
||||
orderBy: { takenAt: "desc" },
|
||||
select: { takenAt: true },
|
||||
});
|
||||
lastIntakeAt = lastIntake?.takenAt ?? null;
|
||||
}
|
||||
|
||||
const ctx = buildRecurrenceContext({ medication: med, userTz, lastIntakeAt });
|
||||
|
||||
for (const schedule of med.schedules) {
|
||||
const canonical = buildCanonicalSchedule(schedule);
|
||||
if (!scheduleEmitsInWindow(canonical, ctx, todayStart, todayEnd)) {
|
||||
continue;
|
||||
}
|
||||
const [h, m] = schedule.windowStart.split(":").map(Number);
|
||||
if (!Number.isFinite(h) || !Number.isFinite(m)) continue;
|
||||
projected.push({
|
||||
medicationId: schedule.medicationId,
|
||||
scheduledFor: localHmAsUtc(now, userTz, h, m),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (projected.length === 0) {
|
||||
return { projected: 0, backfilled: 0 };
|
||||
|
||||
@@ -252,12 +252,20 @@ export function validateMeasurementRange(
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.6.0 — notes cap raised from 25 to 200. A 25-char limit could not
|
||||
* hold a meaningful clinical note ("took after large meal, felt dizzy
|
||||
* standing up"). The DB column is unbounded `String?`; this is the
|
||||
* single source of truth the client char-counters import.
|
||||
*/
|
||||
export const MEASUREMENT_NOTES_MAX_LENGTH = 200;
|
||||
|
||||
export const createMeasurementSchema = z
|
||||
.object({
|
||||
type: measurementTypeEnum,
|
||||
value: z.number(),
|
||||
measuredAt: z.iso.datetime({ offset: true }).transform((s) => new Date(s)),
|
||||
notes: z.string().max(25).optional(),
|
||||
notes: z.string().max(MEASUREMENT_NOTES_MAX_LENGTH).optional(),
|
||||
source: measurementSourceEnum.optional().default("MANUAL"),
|
||||
// Only applies when type === BLOOD_GLUCOSE. Mirrored by a CHECK
|
||||
// constraint in Postgres (see migration 0021).
|
||||
@@ -293,7 +301,10 @@ export const updateMeasurementSchema = z.object({
|
||||
.optional(),
|
||||
notes: z
|
||||
.string()
|
||||
.max(25, "Note cannot exceed 25 characters")
|
||||
.max(
|
||||
MEASUREMENT_NOTES_MAX_LENGTH,
|
||||
`Note cannot exceed ${MEASUREMENT_NOTES_MAX_LENGTH} characters`,
|
||||
)
|
||||
.nullable()
|
||||
.optional(),
|
||||
});
|
||||
|
||||
@@ -38,6 +38,20 @@ export const MEDICATION_TREATMENT_CLASS_VALUES = ["GENERIC", "GLP1"] as const;
|
||||
export type MedicationTreatmentClass =
|
||||
(typeof MEDICATION_TREATMENT_CLASS_VALUES)[number];
|
||||
|
||||
/**
|
||||
* v1.6.0 — route of administration. Decoupled from `treatmentClass`:
|
||||
* the injection-site picker surfaces for any `INJECTION` dose, and a
|
||||
* one-time injection is `oneShot: true` + `deliveryForm: "INJECTION"`.
|
||||
* ORAL is the default the migration backfilled onto every existing row.
|
||||
*/
|
||||
export const MEDICATION_DELIVERY_FORM_VALUES = [
|
||||
"ORAL",
|
||||
"INJECTION",
|
||||
"OTHER",
|
||||
] as const;
|
||||
export type MedicationDeliveryForm =
|
||||
(typeof MEDICATION_DELIVERY_FORM_VALUES)[number];
|
||||
|
||||
/**
|
||||
* v1.5 — RRULE string shape check.
|
||||
*
|
||||
@@ -225,6 +239,8 @@ export const createMedicationSchema = z
|
||||
treatmentClass: z.enum(MEDICATION_TREATMENT_CLASS_VALUES).optional(),
|
||||
/** v1.4.25 W4d — doses per pen/vial for inventory tracking. */
|
||||
dosesPerUnit: z.number().int().min(1).max(100).optional(),
|
||||
/** v1.6.0 — route of administration (ORAL | INJECTION | OTHER). */
|
||||
deliveryForm: z.enum(MEDICATION_DELIVERY_FORM_VALUES).optional(),
|
||||
notificationsEnabled: z.boolean().optional(),
|
||||
...courseWindowFields,
|
||||
schedules: z.array(scheduleSchema).min(1, "Mindestens ein Zeitfenster"),
|
||||
@@ -254,6 +270,8 @@ export const updateMedicationSchema = z
|
||||
category: z.enum(MEDICATION_CATEGORY_VALUES).optional(),
|
||||
treatmentClass: z.enum(MEDICATION_TREATMENT_CLASS_VALUES).optional(),
|
||||
dosesPerUnit: z.number().int().min(1).max(100).nullable().optional(),
|
||||
/** v1.6.0 — route of administration (ORAL | INJECTION | OTHER). */
|
||||
deliveryForm: z.enum(MEDICATION_DELIVERY_FORM_VALUES).optional(),
|
||||
active: z.boolean().optional(),
|
||||
notificationsEnabled: z.boolean().optional(),
|
||||
...courseWindowFields,
|
||||
|
||||
Reference in new issue
Block a user