mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
Keep an appointment reminder inside the visit that owns it
Review of the visit work found the appointment arm leaking in both directions: rows the visit routes could strand, and a reminder surface that would act on one as though it were a checkup. An edit could orphan a live reminder. Pointing a still-booked visit at the checkup it closed walked the foreign key off the visit's own reminder, and the next reschedule then minted a second one, because the re-anchor path asked the key what the visit owned instead of asking the engine. An orphan fires: the cron reads the reminder table, not the visit list, so it pushed for an appointment nobody could see, edit or cancel, and deleting the visit did not reach it. The edit arm now refuses the contradiction the create arm already refused, ownership is resolved through the relation, deletion retires the row it owns rather than the row it points at, and a sweep at the end of every visit write retires anything left armed that no visit claims -- so the state is unreachable rather than merely unlikely. The by-id reminder routes never excluded appointments, and that was the larger hole. Marking one done committed the satisfy and THEN failed on the mapper, so every appointment push a person acknowledged answered 500 after the write had landed. An edit could give a spent one-shot a cadence and turn it into a permanent nudge that appears in no list, or switch a cancelled one back on. The delete is a hard delete, so somebody holding only the measurements domain could destroy a row belonging to a visit in the health background. All five refuse in the lookup now -- an appointment is not addressable there at all -- and the chat buttons that mark a reminder done or postpone it refuse it the same way. Nor is an appointment satisfied by a lab result: the auto-satisfy matcher used to resolve any free-text reminder from any panel, which would have marked next month's cardiology appointment attended. A visit is a seam between four domains, and names were crossing it. The values never did, but the title of a discharge letter is itself the sensitive part, and a grant that opened only the health background was never consent for it. Link labels and dates now follow the target's own domain; the link is still listed, so a delegate learns the visit produced a lab result without learning which analyte. Closing a checkup follows the same rule: it re-anchors a preventive-care cadence, which is a measurements write, so a health-background delegate files the visit and the closure is reported as skipped rather than performed in silence. Three smaller repairs. A checkup now closes at the visit's own instant rather than at the moment it was typed in, which anchors a backdated entry correctly and makes correcting a mis-filed visit the no-op it should always have been. A window ending before today no longer widens back out to today. And the fallback in a push notification was the raw enum constant, so a lock screen could read a schema value; it reads the record owner's own language now, in all six.
This commit is contained in:
1 parent
d24dfd89a9
commit
339e815527
29 files changed
+1618
-92
No files matched your search
@@ -27,7 +27,10 @@ import {
|
||||
} from "@/lib/api-response";
|
||||
import { encounterLinkSchema } from "@/lib/validations/encounters";
|
||||
import { linkTargets, unlinkTargets } from "@/lib/links";
|
||||
import { loadEncounterLinks } from "@/lib/encounters/service";
|
||||
import {
|
||||
actingDomainVisibility,
|
||||
loadEncounterLinks,
|
||||
} from "@/lib/encounters/service";
|
||||
|
||||
type RouteParams = { params: Promise<{ id: string }> };
|
||||
|
||||
@@ -78,7 +81,11 @@ async function handle(
|
||||
},
|
||||
});
|
||||
|
||||
const links = await loadEncounterLinks(prisma, user.id, id);
|
||||
// Bare `requireAuth()` resolves the caller's OWN record — this route is
|
||||
// not delegable — so there is no narrowed grant to honour and every
|
||||
// domain is the caller's own.
|
||||
const visible = await actingDomainVisibility(prisma, null);
|
||||
const links = await loadEncounterLinks(prisma, user.id, id, visible);
|
||||
return apiSuccess({ links, unknown: result.unknownTargetIds });
|
||||
}
|
||||
|
||||
|
||||
@@ -17,11 +17,13 @@ import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { apiSuccess, apiError, getClientIp } from "@/lib/api-response";
|
||||
import { encounterKindLabel } from "@/lib/encounters/kind-label";
|
||||
import { toEncounterDTO } from "@/lib/encounters/dto";
|
||||
import {
|
||||
ENCOUNTER_INCLUDE,
|
||||
actingDomainVisibility,
|
||||
loadEncounterLinks,
|
||||
resolveTimezone,
|
||||
resolveOwnerNotificationContext,
|
||||
shouldHaveReminder,
|
||||
} from "@/lib/encounters/service";
|
||||
import { reanchorAppointmentReminder } from "@/lib/encounters/appointment-reminder";
|
||||
@@ -30,7 +32,7 @@ type RouteParams = { params: Promise<{ id: string }> };
|
||||
|
||||
export const POST = apiHandler(
|
||||
async (request: NextRequest, { params }: RouteParams) => {
|
||||
const { user } = await requireRecordAuth("manage", "profile");
|
||||
const { user, grantId } = await requireRecordAuth("manage", "profile");
|
||||
|
||||
const { id } = await params;
|
||||
const existing = await prisma.encounter.findUnique({
|
||||
@@ -42,7 +44,9 @@ export const POST = apiHandler(
|
||||
}
|
||||
|
||||
if (existing.deletedAt !== null) {
|
||||
const timezone = await resolveTimezone(user.id);
|
||||
const { timezone, locale } = await resolveOwnerNotificationContext(
|
||||
user.id,
|
||||
);
|
||||
const now = new Date();
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.encounter.update({
|
||||
@@ -66,7 +70,7 @@ export const POST = apiHandler(
|
||||
occurredAt: existing.occurredAt,
|
||||
practitionerName: existing.practitioner?.name ?? null,
|
||||
practitionerLocation: existing.practitioner?.location ?? null,
|
||||
kindLabel: existing.kind,
|
||||
kindLabel: encounterKindLabel(existing.kind, locale),
|
||||
},
|
||||
timezone,
|
||||
);
|
||||
@@ -79,7 +83,8 @@ export const POST = apiHandler(
|
||||
where: { id },
|
||||
include: ENCOUNTER_INCLUDE,
|
||||
});
|
||||
const links = await loadEncounterLinks(prisma, user.id, id);
|
||||
const visible = await actingDomainVisibility(prisma, grantId);
|
||||
const links = await loadEncounterLinks(prisma, user.id, id, visible);
|
||||
|
||||
await auditLog("encounter.visit.restore", {
|
||||
userId: user.id,
|
||||
|
||||
@@ -30,25 +30,35 @@ import {
|
||||
} from "@/lib/api-response";
|
||||
import { encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import { encounterUpdateSchema } from "@/lib/validations/encounters";
|
||||
import { encounterKindLabel } from "@/lib/encounters/kind-label";
|
||||
import { toEncounterDTO } from "@/lib/encounters/dto";
|
||||
import {
|
||||
ENCOUNTER_INCLUDE,
|
||||
actingDomainVisibility,
|
||||
applyEncounterLinks,
|
||||
closeCheckupForVisit,
|
||||
findOwnAppointmentReminderId,
|
||||
loadEncounterLinks,
|
||||
resolveClosableReminder,
|
||||
resolveOwnedPractitioner,
|
||||
resolveTimezone,
|
||||
resolveOwnerNotificationContext,
|
||||
retireAppointmentReminderForVisit,
|
||||
retireOrphanedAppointmentReminders,
|
||||
shouldHaveReminder,
|
||||
syncAppointmentReminder,
|
||||
} from "@/lib/encounters/service";
|
||||
import {
|
||||
recordUnknownKeys,
|
||||
summarizeRestoreSkips,
|
||||
type RestoreSkipLog,
|
||||
} from "@/lib/export/restore-skips";
|
||||
import type { Prisma } from "@/generated/prisma/client";
|
||||
|
||||
type RouteParams = { params: Promise<{ id: string }> };
|
||||
|
||||
export const GET = apiHandler(
|
||||
async (_request: NextRequest, { params }: RouteParams) => {
|
||||
const { user } = await requireRecordAuth("read", "profile");
|
||||
const { user, grantId } = await requireRecordAuth("read", "profile");
|
||||
|
||||
const { id } = await params;
|
||||
const row = await prisma.encounter.findUnique({
|
||||
@@ -59,7 +69,8 @@ export const GET = apiHandler(
|
||||
return apiError("Visit not found", 404);
|
||||
}
|
||||
|
||||
const links = await loadEncounterLinks(prisma, user.id, id);
|
||||
const visible = await actingDomainVisibility(prisma, grantId);
|
||||
const links = await loadEncounterLinks(prisma, user.id, id, visible);
|
||||
|
||||
annotate({
|
||||
action: {
|
||||
@@ -75,7 +86,7 @@ export const GET = apiHandler(
|
||||
|
||||
export const PATCH = apiHandler(
|
||||
async (request: NextRequest, { params }: RouteParams) => {
|
||||
const { user } = await requireRecordAuth("manage", "profile");
|
||||
const { user, grantId } = await requireRecordAuth("manage", "profile");
|
||||
|
||||
const { id } = await params;
|
||||
const existing = await prisma.encounter.findUnique({ where: { id } });
|
||||
@@ -100,7 +111,7 @@ export const PATCH = apiHandler(
|
||||
}
|
||||
|
||||
const entry = parsed.data;
|
||||
const timezone = await resolveTimezone(user.id);
|
||||
const { timezone, locale } = await resolveOwnerNotificationContext(user.id);
|
||||
const now = new Date();
|
||||
|
||||
// The state the visit will be in once this edit lands. Both the reminder
|
||||
@@ -112,7 +123,10 @@ export const PATCH = apiHandler(
|
||||
: existing.occurredAt;
|
||||
const nextStatus = entry.status ?? existing.status;
|
||||
|
||||
const skips: RestoreSkipLog = [];
|
||||
|
||||
const outcome = await prisma.$transaction(async (tx) => {
|
||||
const visible = await actingDomainVisibility(tx, grantId);
|
||||
let practitionerId = existing.practitionerId;
|
||||
let practitionerName: string | null = null;
|
||||
let practitionerLocation: string | null = null;
|
||||
@@ -161,22 +175,24 @@ export const PATCH = apiHandler(
|
||||
: { disconnect: true };
|
||||
}
|
||||
|
||||
// Whether the visit's own appointment reminder — the ENCOUNTER-origin
|
||||
// row this code minted — is the thing `reminderId` currently holds. A
|
||||
// checkup the visit closes lives in the same column and must not be
|
||||
// re-anchored onto the appointment's date.
|
||||
const currentReminder = existing.reminderId
|
||||
? await tx.measurementReminder.findUnique({
|
||||
where: { id: existing.reminderId },
|
||||
select: { id: true, origin: true },
|
||||
})
|
||||
: null;
|
||||
const ownAppointmentReminderId =
|
||||
currentReminder?.origin === "ENCOUNTER" ? currentReminder.id : null;
|
||||
// The appointment reminder this visit OWNS, resolved through the engine
|
||||
// table rather than by reading the visit's own foreign key. Reading the
|
||||
// key answers a different question — what the visit points at — and the
|
||||
// two stop agreeing the moment the key is re-pointed at a checkup.
|
||||
const ownAppointmentReminderId = await findOwnAppointmentReminderId(
|
||||
tx,
|
||||
user.id,
|
||||
id,
|
||||
);
|
||||
|
||||
let reminderId = existing.reminderId;
|
||||
|
||||
if (shouldHaveReminder(nextStatus, nextOccurredAt, now)) {
|
||||
// The same contradiction the create arm refuses, and it has to be
|
||||
// refused here too: a body that books a visit AND names the checkup it
|
||||
// closed would walk the foreign key off the visit's own reminder,
|
||||
// leaving it live, armed and unreachable.
|
||||
if (entry.reminderId) return "contradictory-reminder" as const;
|
||||
reminderId = await syncAppointmentReminder(
|
||||
tx,
|
||||
{
|
||||
@@ -184,7 +200,7 @@ export const PATCH = apiHandler(
|
||||
occurredAt: nextOccurredAt,
|
||||
practitionerName,
|
||||
practitionerLocation,
|
||||
kindLabel: entry.kind ?? existing.kind,
|
||||
kindLabel: encounterKindLabel(entry.kind ?? existing.kind, locale),
|
||||
status: nextStatus,
|
||||
existingReminderId: ownAppointmentReminderId,
|
||||
},
|
||||
@@ -200,7 +216,7 @@ export const PATCH = apiHandler(
|
||||
occurredAt: nextOccurredAt,
|
||||
practitionerName,
|
||||
practitionerLocation,
|
||||
kindLabel: entry.kind ?? existing.kind,
|
||||
kindLabel: encounterKindLabel(entry.kind ?? existing.kind, locale),
|
||||
status: nextStatus,
|
||||
existingReminderId: ownAppointmentReminderId,
|
||||
},
|
||||
@@ -246,12 +262,44 @@ export const PATCH = apiHandler(
|
||||
nextStatus === "DONE" && existing.status !== "DONE";
|
||||
if (closesCheckupNow && reminderId) {
|
||||
const checkup = await resolveClosableReminder(tx, user.id, reminderId);
|
||||
if (checkup) await closeCheckupForVisit(tx, checkup, timezone, now);
|
||||
if (checkup) {
|
||||
// Same domain fence as the create arm: closing a checkup is a
|
||||
// measurements write, and a health-background delegate may edit the
|
||||
// visit without being able to perform it.
|
||||
if (visible("measurements")) {
|
||||
await closeCheckupForVisit(
|
||||
tx,
|
||||
checkup,
|
||||
timezone,
|
||||
nextOccurredAt,
|
||||
now,
|
||||
);
|
||||
} else {
|
||||
recordUnknownKeys(
|
||||
skips,
|
||||
"checkupClosure",
|
||||
[checkup.id],
|
||||
[checkup.id],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing this account owns may be left armed for a visit that does not
|
||||
// point at it. Runs after the write so the row just minted, whose key is
|
||||
// set one statement earlier, is never mistaken for a stray.
|
||||
await retireOrphanedAppointmentReminders(tx, user.id, now);
|
||||
|
||||
return { updated };
|
||||
});
|
||||
|
||||
if (outcome === "contradictory-reminder") {
|
||||
return apiError(
|
||||
"A booked visit cannot also close a checkup — it has not happened yet",
|
||||
422,
|
||||
{ errorCode: "encounter.reminder-conflict" },
|
||||
);
|
||||
}
|
||||
if (outcome === "unknown-practitioner") {
|
||||
return apiError("Practitioner not found", 404, {
|
||||
errorCode: "encounter.practitioner-not-found",
|
||||
@@ -267,7 +315,8 @@ export const PATCH = apiHandler(
|
||||
where: { id },
|
||||
include: ENCOUNTER_INCLUDE,
|
||||
});
|
||||
const links = await loadEncounterLinks(prisma, user.id, id);
|
||||
const visible = await actingDomainVisibility(prisma, grantId);
|
||||
const links = await loadEncounterLinks(prisma, user.id, id, visible);
|
||||
|
||||
await auditLog("encounter.visit.update", {
|
||||
userId: user.id,
|
||||
@@ -302,7 +351,7 @@ export const PATCH = apiHandler(
|
||||
meta: { status: row.status },
|
||||
});
|
||||
|
||||
return apiSuccess(toEncounterDTO(row, links));
|
||||
return apiSuccess(toEncounterDTO(row, links, summarizeRestoreSkips(skips)));
|
||||
},
|
||||
);
|
||||
|
||||
@@ -320,21 +369,16 @@ export const DELETE = apiHandler(
|
||||
if (existing.deletedAt === null) {
|
||||
const at = new Date();
|
||||
await prisma.$transaction(async (tx) => {
|
||||
// Only the visit's OWN appointment reminder goes with it, and it is
|
||||
// found by the relation rather than by the foreign key: a visit whose
|
||||
// key points at a checkup still owns its appointment row, and reading
|
||||
// the key would leave that row armed for a visit that no longer exists.
|
||||
// Retired BEFORE the tombstone, while the relation still resolves.
|
||||
await retireAppointmentReminderForVisit(tx, user.id, id, at);
|
||||
await tx.encounter.update({ where: { id }, data: { deletedAt: at } });
|
||||
// Only the visit's OWN appointment reminder goes with it. A checkup the
|
||||
// visit closed belongs to the person's Vorsorge list and outlives it.
|
||||
if (existing.reminderId) {
|
||||
const reminder = await tx.measurementReminder.findUnique({
|
||||
where: { id: existing.reminderId },
|
||||
select: { id: true, origin: true },
|
||||
});
|
||||
if (reminder?.origin === "ENCOUNTER") {
|
||||
await tx.measurementReminder.update({
|
||||
where: { id: reminder.id },
|
||||
data: { deletedAt: at, enabled: false, nextDueAt: null },
|
||||
});
|
||||
}
|
||||
}
|
||||
// A checkup the visit closed belongs to the person's preventive-care
|
||||
// list and outlives it; the sweep below only reaps appointment rows.
|
||||
await retireOrphanedAppointmentReminders(tx, user.id, at);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -30,17 +30,24 @@ import {
|
||||
encounterCreateSchema,
|
||||
encounterListQuerySchema,
|
||||
} from "@/lib/validations/encounters";
|
||||
import { encounterKindLabel } from "@/lib/encounters/kind-label";
|
||||
import { toEncounterDTO, type EncounterListDTO } from "@/lib/encounters/dto";
|
||||
import {
|
||||
ENCOUNTER_INCLUDE,
|
||||
actingDomainVisibility,
|
||||
applyEncounterLinks,
|
||||
closeCheckupForVisit,
|
||||
resolveClosableReminder,
|
||||
resolveOwnedPractitioner,
|
||||
resolveTimezone,
|
||||
resolveOwnerNotificationContext,
|
||||
shouldHaveReminder,
|
||||
syncAppointmentReminder,
|
||||
} from "@/lib/encounters/service";
|
||||
import {
|
||||
recordUnknownKeys,
|
||||
summarizeRestoreSkips,
|
||||
type RestoreSkipLog,
|
||||
} from "@/lib/export/restore-skips";
|
||||
|
||||
export const GET = apiHandler(async (request: NextRequest) => {
|
||||
const { user } = await requireRecordAuth("read", "profile");
|
||||
@@ -78,19 +85,33 @@ export const GET = apiHandler(async (request: NextRequest) => {
|
||||
: {}),
|
||||
};
|
||||
|
||||
// The past arm's own upper bound is `now`, and the caller may have asked for
|
||||
// an earlier one. Both have to hold, so they COMBINE to the tighter of the
|
||||
// two rather than one overwriting the other — spreading the caller's
|
||||
// `occurredAt` and then restating `lte` silently widened every window that
|
||||
// ended before today back out to "everything up to now".
|
||||
const requestedTo = query.to ? new Date(query.to) : null;
|
||||
const pastCeiling =
|
||||
requestedTo && requestedTo.getTime() < now.getTime() ? requestedTo : now;
|
||||
|
||||
// Two queries rather than one sorted list. Upcoming reads soonest-first and
|
||||
// past reads newest-first, which is the opposite direction; resolving both
|
||||
// here means the client renders two lists instead of splitting and
|
||||
// re-sorting one, and the ordering lives in one place.
|
||||
const [upcoming, past] = await Promise.all([
|
||||
prisma.encounter.findMany({
|
||||
// `gt` and `gte` are different keys, so the caller's lower bound and this
|
||||
// arm's floor both survive the spread; only the `lte` pair collided.
|
||||
where: { ...where, occurredAt: { ...where.occurredAt, gt: now } },
|
||||
orderBy: { occurredAt: "asc" },
|
||||
take,
|
||||
include: ENCOUNTER_INCLUDE,
|
||||
}),
|
||||
prisma.encounter.findMany({
|
||||
where: { ...where, occurredAt: { ...where.occurredAt, lte: now } },
|
||||
where: {
|
||||
...where,
|
||||
occurredAt: { ...where.occurredAt, lte: pastCeiling },
|
||||
},
|
||||
orderBy: { occurredAt: "desc" },
|
||||
take,
|
||||
include: ENCOUNTER_INCLUDE,
|
||||
@@ -113,7 +134,7 @@ export const GET = apiHandler(async (request: NextRequest) => {
|
||||
export const POST = apiHandler(withIdempotency<[NextRequest]>(postEncounter));
|
||||
|
||||
async function postEncounter(request: NextRequest): Promise<Response> {
|
||||
const { user } = await requireRecordAuth("write", "profile");
|
||||
const { user, grantId } = await requireRecordAuth("write", "profile");
|
||||
|
||||
const { data: rawBody, error: jsonError } = await safeJson(request, {
|
||||
maxBytes: 32 * 1024,
|
||||
@@ -133,10 +154,13 @@ async function postEncounter(request: NextRequest): Promise<Response> {
|
||||
|
||||
const entry = parsed.data;
|
||||
const occurredAt = new Date(entry.occurredAt);
|
||||
const timezone = await resolveTimezone(user.id);
|
||||
const { timezone, locale } = await resolveOwnerNotificationContext(user.id);
|
||||
const now = new Date();
|
||||
|
||||
const skips: RestoreSkipLog = [];
|
||||
|
||||
const created = await prisma.$transaction(async (tx) => {
|
||||
const visible = await actingDomainVisibility(tx, grantId);
|
||||
// A practitioner the caller does not own is a refusal rather than a
|
||||
// silently dropped field: the person named a practice and would otherwise
|
||||
// see the visit save without it.
|
||||
@@ -183,7 +207,7 @@ async function postEncounter(request: NextRequest): Promise<Response> {
|
||||
occurredAt,
|
||||
practitionerName: practitioner?.name ?? null,
|
||||
practitionerLocation: practitioner?.location ?? null,
|
||||
kindLabel: entry.kind,
|
||||
kindLabel: encounterKindLabel(entry.kind, locale),
|
||||
status: entry.status,
|
||||
existingReminderId: null,
|
||||
},
|
||||
@@ -198,10 +222,24 @@ async function postEncounter(request: NextRequest): Promise<Response> {
|
||||
);
|
||||
if (!checkup) return "unknown-reminder" as const;
|
||||
reminderId = checkup.id;
|
||||
// Filing a visit as done against a due checkup closes it. Forward-only
|
||||
// and idempotent, so re-filing the same visit changes nothing.
|
||||
// Filing a visit as done against a due checkup closes it — but closing
|
||||
// one re-anchors a preventive-care cadence, which is a write in the
|
||||
// measurements domain. A delegate holding only the health background may
|
||||
// file the visit and may not perform that side effect, so the visit
|
||||
// saves and the closure is reported as skipped rather than done in
|
||||
// silence: somebody who believes a checkup was closed will not look at
|
||||
// it again.
|
||||
if (entry.status === "DONE") {
|
||||
await closeCheckupForVisit(tx, checkup, timezone, now);
|
||||
if (visible("measurements")) {
|
||||
await closeCheckupForVisit(tx, checkup, timezone, occurredAt, now);
|
||||
} else {
|
||||
recordUnknownKeys(
|
||||
skips,
|
||||
"checkupClosure",
|
||||
[checkup.id],
|
||||
[checkup.id],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,5 +289,8 @@ async function postEncounter(request: NextRequest): Promise<Response> {
|
||||
meta: { status: created.status, kind: created.kind },
|
||||
});
|
||||
|
||||
return apiSuccess(toEncounterDTO(created), 201);
|
||||
return apiSuccess(
|
||||
toEncounterDTO(created, undefined, summarizeRestoreSkips(skips)),
|
||||
201,
|
||||
);
|
||||
}
|
||||
@@ -41,8 +41,13 @@ export const POST = apiHandler(
|
||||
const { user } = await requireRecordAuth("manage", "measurements");
|
||||
const { id } = await params;
|
||||
|
||||
// An appointment reminder is not addressable here. It belongs to a visit,
|
||||
// is managed through the visit routes, and sits in a different sharing
|
||||
// domain — so this family treats one as not found rather than acting on
|
||||
// it. Filtering in the lookup rather than after it is what keeps a write
|
||||
// from committing before the refusal.
|
||||
const existing = await prisma.measurementReminder.findFirst({
|
||||
where: { id, deletedAt: null },
|
||||
where: { id, deletedAt: null, origin: { not: "ENCOUNTER" } },
|
||||
});
|
||||
if (!existing || existing.userId !== user.id) {
|
||||
return apiError("Measurement reminder not found", 404);
|
||||
|
||||
@@ -48,8 +48,13 @@ export const GET = apiHandler(
|
||||
const { user } = await requireRecordAuth("read", "measurements");
|
||||
const { id } = await params;
|
||||
|
||||
// An appointment reminder is not addressable here. It belongs to a visit,
|
||||
// is managed through the visit routes, and sits in a different sharing
|
||||
// domain — so this family treats one as not found rather than acting on
|
||||
// it. Filtering in the lookup rather than after it is what keeps a write
|
||||
// from committing before the refusal.
|
||||
const reminder = await prisma.measurementReminder.findFirst({
|
||||
where: { id, deletedAt: null },
|
||||
where: { id, deletedAt: null, origin: { not: "ENCOUNTER" } },
|
||||
});
|
||||
if (!reminder || reminder.userId !== user.id) {
|
||||
return apiError("Measurement reminder not found", 404);
|
||||
@@ -71,8 +76,13 @@ export const PATCH = apiHandler(
|
||||
const { user } = await requireRecordAuth("manage", "measurements");
|
||||
const { id } = await params;
|
||||
|
||||
// An appointment reminder is not addressable here. It belongs to a visit,
|
||||
// is managed through the visit routes, and sits in a different sharing
|
||||
// domain — so this family treats one as not found rather than acting on
|
||||
// it. Filtering in the lookup rather than after it is what keeps a write
|
||||
// from committing before the refusal.
|
||||
const existing = await prisma.measurementReminder.findFirst({
|
||||
where: { id, deletedAt: null },
|
||||
where: { id, deletedAt: null, origin: { not: "ENCOUNTER" } },
|
||||
});
|
||||
if (!existing || existing.userId !== user.id) {
|
||||
return apiError("Measurement reminder not found", 404);
|
||||
@@ -234,8 +244,15 @@ export const DELETE = apiHandler(
|
||||
const { user } = await requireRecordAuth("manage", "measurements");
|
||||
const { id } = await params;
|
||||
|
||||
const existing = await prisma.measurementReminder.findUnique({
|
||||
where: { id },
|
||||
// An appointment reminder is not addressable here. It belongs to a visit,
|
||||
// is managed through the visit routes, and sits in a different sharing
|
||||
// domain — so this family treats one as not found rather than acting on
|
||||
// it. Filtering in the lookup rather than after it is what keeps a write
|
||||
// from committing before the refusal.
|
||||
// `findFirst` rather than `findUnique`: the origin is not part of any
|
||||
// unique key, so the predicate cannot ride a unique lookup.
|
||||
const existing = await prisma.measurementReminder.findFirst({
|
||||
where: { id, origin: { not: "ENCOUNTER" } },
|
||||
});
|
||||
if (!existing || existing.userId !== user.id) {
|
||||
return apiError("Measurement reminder not found", 404);
|
||||
|
||||
@@ -27,8 +27,13 @@ export const POST = apiHandler(
|
||||
const { user } = await requireRecordAuth("manage", "measurements");
|
||||
const { id } = await params;
|
||||
|
||||
// An appointment reminder is not addressable here. It belongs to a visit,
|
||||
// is managed through the visit routes, and sits in a different sharing
|
||||
// domain — so this family treats one as not found rather than acting on
|
||||
// it. Filtering in the lookup rather than after it is what keeps a write
|
||||
// from committing before the refusal.
|
||||
const existing = await prisma.measurementReminder.findFirst({
|
||||
where: { id, deletedAt: null },
|
||||
where: { id, deletedAt: null, origin: { not: "ENCOUNTER" } },
|
||||
});
|
||||
if (!existing || existing.userId !== user.id) {
|
||||
return apiError("Measurement reminder not found", 404);
|
||||
|
||||
Reference in new issue
Block a user