fix(medications): return a medication's schedules in one order everywhere

The list reads ordered schedules by their window and id, but the detail,
create, update, compliance, dose-history, GLP-1 and efficacy reads took
them in whatever order Postgres found them, so two schedules created in
the same millisecond could swap places between two answers. Every read
now uses MEDICATION_SCHEDULE_ORDER, and the guard checks every
schedules include, not only the list reads.
This commit is contained in:
Marc-André Bombeck committed 2026-10-25 19:09:27 +01:00
1 parent a42b29a160
commit 135d5baf28
10 files changed
+56 -36

No files matched your search

@@ -12,6 +12,7 @@ import {
import { cachedSwr, caches, type ServerCache } from "@/lib/cache/server-cache";
import { checkRateLimit } from "@/lib/rate-limit";
import { DEFAULT_TIMEZONE } from "@/lib/tz/format";
import { MEDICATION_SCHEDULE_ORDER } from "@/lib/medications/read-order";
type RouteParams = { params: Promise<{ id: string }> };
@@ -71,7 +72,7 @@ export const GET = apiHandler(
const medication = await prisma.medication.findUnique({
where: { id },
include: {
schedules: true,
schedules: { orderBy: MEDICATION_SCHEDULE_ORDER },
// v1.16.3 — archived schedule eras for era-aware compliance.
scheduleRevisions: { orderBy: { validFrom: "asc" } },
// v1.25 — pause eras so paused days drop out of the denominator.
@@ -48,6 +48,7 @@ import { assertMedicationOwnership } from "@/lib/medications/route-guards";
import { dueSchedules } from "@/lib/medications/intake-tracking";
import { DEFAULT_TIMEZONE } from "@/lib/tz/format";
import { isoDateTime } from "@/lib/validations/datetime";
import { MEDICATION_SCHEDULE_ORDER } from "@/lib/medications/read-order";
type RouteParams = { params: Promise<{ id: string }> };
@@ -132,7 +133,7 @@ export const GET = apiHandler(
const medication = await prisma.medication.findUnique({
where: { id },
include: {
schedules: true,
schedules: { orderBy: MEDICATION_SCHEDULE_ORDER },
// v1.16.3 — archived schedule eras: past days mint against the
// schedule that was live THEN.
scheduleRevisions: { orderBy: { validFrom: "asc" } },
+2 -1
View File
@@ -43,6 +43,7 @@ import {
import { NextRequest } from "next/server";
import { dueSchedules } from "@/lib/medications/intake-tracking";
import { floorWhole } from "@/lib/medications/units-per-dose";
import { MEDICATION_SCHEDULE_ORDER } from "@/lib/medications/read-order";
type RouteParams = { params: Promise<{ id: string }> };
@@ -90,7 +91,7 @@ export const GET = apiHandler(
take: 12,
select: { takenAt: true, injectionSite: true },
},
schedules: true,
schedules: { orderBy: MEDICATION_SCHEDULE_ORDER },
},
});