mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
Add the visit and address-book API, and the appointment arm
Nine visit routes and five practitioner routes, all classified under the existing profile domain and each reviewed into the frozen sharing literal with the sentence that argues for it. An address book of a person's own doctors is record content, not account configuration: it names no credential, no integration and no notification channel, which is the fence that classification turns on. The eight sharing domains are unchanged. Link and unlink stay bare, matching document link and unlink, and the pair is revisited as one decision later. Nothing is a required field. A visit saves with a date and nothing else. Every id a body may carry is re-narrowed to the caller before anything is written, and an unknown link id never blocks a save. A booked visit mints exactly one reminder on the existing engine with the new origin, and the appointment half is the part worth reading twice. Both cadence columns stay null, which is the schema's one-shot, so the first due instant is stamped directly and the never-nag property is structural rather than guarded: after it fires, the cadence helper has no occurrence to return. Rescheduling re-anchors that row and never mints a second, which the tests assert by counting rows rather than by checking the date moved. Filing a visit as done closes the checkup it was filed against, on the transition rather than on every later edit. Writing the test first found that: satisfying is forward-only, so re-saving a completed visit was pushing the checkup's next due date out again as if the person had been a second time. An appointment must appear on no preventive-care surface, and that is four separate reads, one of them in the assistant tooling. Each carries the exclusion, a guard proves all four by source text with a non-zero match count, and the reminder mapper refuses such a row outright — so a read site that loses its filter fails loudly instead of showing an appointment as a checkup. The published origin enum is untouched.
This commit is contained in:
1 parent
ba21c3a9bc
commit
f158494def
30 files changed
+2743
-6
No files matched your search
+64
@@ -120,6 +120,22 @@ export const ADMITTED_MUTATING_HANDLERS = [
|
||||
"write",
|
||||
"create",
|
||||
),
|
||||
handler(
|
||||
"app/api/encounters/route.ts",
|
||||
"/api/encounters",
|
||||
"POST",
|
||||
"profile",
|
||||
"write",
|
||||
"create",
|
||||
),
|
||||
handler(
|
||||
"app/api/practitioners/route.ts",
|
||||
"/api/practitioners",
|
||||
"POST",
|
||||
"profile",
|
||||
"write",
|
||||
"create",
|
||||
),
|
||||
handler(
|
||||
"app/api/medications/[id]/side-effects/route.ts",
|
||||
"/api/medications/[id]/side-effects",
|
||||
@@ -336,6 +352,54 @@ export const ADMITTED_MUTATING_HANDLERS = [
|
||||
"manage",
|
||||
"restore",
|
||||
),
|
||||
handler(
|
||||
"app/api/encounters/[id]/route.ts",
|
||||
"/api/encounters/[id]",
|
||||
"PATCH",
|
||||
"profile",
|
||||
"manage",
|
||||
"update",
|
||||
),
|
||||
handler(
|
||||
"app/api/encounters/[id]/route.ts",
|
||||
"/api/encounters/[id]",
|
||||
"DELETE",
|
||||
"profile",
|
||||
"manage",
|
||||
"delete",
|
||||
),
|
||||
handler(
|
||||
"app/api/encounters/[id]/restore/route.ts",
|
||||
"/api/encounters/[id]/restore",
|
||||
"POST",
|
||||
"profile",
|
||||
"manage",
|
||||
"restore",
|
||||
),
|
||||
handler(
|
||||
"app/api/practitioners/[id]/route.ts",
|
||||
"/api/practitioners/[id]",
|
||||
"PATCH",
|
||||
"profile",
|
||||
"manage",
|
||||
"update",
|
||||
),
|
||||
handler(
|
||||
"app/api/practitioners/[id]/route.ts",
|
||||
"/api/practitioners/[id]",
|
||||
"DELETE",
|
||||
"profile",
|
||||
"manage",
|
||||
"delete",
|
||||
),
|
||||
handler(
|
||||
"app/api/practitioners/[id]/restore/route.ts",
|
||||
"/api/practitioners/[id]/restore",
|
||||
"POST",
|
||||
"profile",
|
||||
"manage",
|
||||
"restore",
|
||||
),
|
||||
handler(
|
||||
"app/api/illness/episodes/[id]/day-logs/route.ts",
|
||||
"/api/illness/episodes/[id]/day-logs",
|
||||
|
||||
@@ -0,0 +1,356 @@
|
||||
/**
|
||||
* The appointment arm against real Postgres, through the real routes.
|
||||
*
|
||||
* Every property here is about ROWS — how many exist, which instant they carry,
|
||||
* whether a second call wrote anything — and none of them can be answered by a
|
||||
* mocked client. The one that matters most is a count: a reschedule must leave
|
||||
* the reminder count at one, and a service that minted a second on every edit
|
||||
* would pass any assertion phrased as "the reminder moved".
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
process.env.ENCRYPTION_KEY ??=
|
||||
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
|
||||
import { POST as createEncounter } from "@/app/api/encounters/route";
|
||||
import {
|
||||
PATCH as patchEncounter,
|
||||
DELETE as deleteEncounter,
|
||||
} from "@/app/api/encounters/[id]/route";
|
||||
import { GET as listReminders } from "@/app/api/measurement-reminders/route";
|
||||
|
||||
import { cookieJar, headerJar } from "./mock-next-headers";
|
||||
import { getPrismaClient, truncateAllTables } from "./setup";
|
||||
|
||||
vi.mock("next/headers", async () => {
|
||||
const { cookieJar, headerJar } = await import("./mock-next-headers");
|
||||
return {
|
||||
headers: vi.fn(async () => ({
|
||||
get: (name: string) => headerJar.get(name.toLowerCase()) ?? null,
|
||||
})),
|
||||
cookies: vi.fn(async () => ({
|
||||
get: (name: string) => {
|
||||
const value = cookieJar.get(name);
|
||||
return value ? { name, value } : undefined;
|
||||
},
|
||||
set: (name: string, value: string) => cookieJar.set(name, value),
|
||||
delete: (name: string) => cookieJar.delete(name),
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/lib/db-compat", () => ({
|
||||
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
const OWNER_ID = "appointment-owner";
|
||||
|
||||
/** Far enough out that the appointment is still in the future mid-run. */
|
||||
function daysFromNow(days: number): Date {
|
||||
return new Date(Date.now() + days * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
async function seedSession() {
|
||||
const prisma = getPrismaClient();
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: OWNER_ID,
|
||||
username: "appointment-owner",
|
||||
email: "appointment-owner@example.test",
|
||||
timezone: "Europe/Berlin",
|
||||
},
|
||||
});
|
||||
const session = await prisma.session.create({
|
||||
data: {
|
||||
userId: OWNER_ID,
|
||||
expiresAt: daysFromNow(7),
|
||||
mfaVerifiedAt: new Date(),
|
||||
},
|
||||
});
|
||||
cookieJar.set("healthlog_session", session.id);
|
||||
}
|
||||
|
||||
function post(body: unknown): Promise<Response> {
|
||||
return createEncounter(
|
||||
new Request("http://localhost/api/encounters", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
}) as never,
|
||||
);
|
||||
}
|
||||
|
||||
function patch(id: string, body: unknown): Promise<Response> {
|
||||
return patchEncounter(
|
||||
new Request(`http://localhost/api/encounters/${id}`, {
|
||||
method: "PATCH",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
}) as never,
|
||||
{ params: Promise.resolve({ id }) } as never,
|
||||
);
|
||||
}
|
||||
|
||||
async function body(res: Response) {
|
||||
return (await res.json()) as { data: Record<string, unknown> };
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await truncateAllTables(getPrismaClient());
|
||||
cookieJar.clear();
|
||||
headerJar.clear();
|
||||
await seedSession();
|
||||
});
|
||||
|
||||
describe("booking a visit", () => {
|
||||
it("mints exactly one reminder, anchored on the appointment", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const occurredAt = daysFromNow(14);
|
||||
|
||||
const res = await post({
|
||||
occurredAt: occurredAt.toISOString(),
|
||||
status: "PLANNED",
|
||||
kind: "SPECIALIST",
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
|
||||
const reminders = await prisma.measurementReminder.findMany({
|
||||
where: { userId: OWNER_ID },
|
||||
});
|
||||
expect(reminders).toHaveLength(1);
|
||||
expect(reminders[0]!.origin).toBe("ENCOUNTER");
|
||||
// Both cadence columns NULL is the schema's one-shot: it fires once and
|
||||
// the engine can never compute a second occurrence for it.
|
||||
expect(reminders[0]!.intervalDays).toBeNull();
|
||||
expect(reminders[0]!.rrule).toBeNull();
|
||||
expect(reminders[0]!.nextDueAt?.toISOString()).toBe(
|
||||
occurredAt.toISOString(),
|
||||
);
|
||||
|
||||
const created = (await body(res)).data;
|
||||
expect(created.reminderNextDueAt).toBe(occurredAt.toISOString());
|
||||
});
|
||||
|
||||
it("does not mint one for a visit that already happened", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const res = await post({
|
||||
occurredAt: daysFromNow(-3).toISOString(),
|
||||
status: "DONE",
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
expect(await prisma.measurementReminder.count()).toBe(0);
|
||||
});
|
||||
|
||||
it("saves with a date and nothing else", async () => {
|
||||
// The product constraint, asserted rather than described: no practitioner,
|
||||
// no kind, no reason, no link, and the visit still files.
|
||||
const res = await post({ occurredAt: daysFromNow(-1).toISOString() });
|
||||
expect(res.status).toBe(201);
|
||||
const created = (await body(res)).data;
|
||||
expect(created.practitioner).toBeNull();
|
||||
expect(created.reason).toBeNull();
|
||||
expect(created.status).toBe("DONE");
|
||||
});
|
||||
});
|
||||
|
||||
describe("rescheduling", () => {
|
||||
it("moves the same row and never mints a second", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const created = (
|
||||
await body(
|
||||
await post({
|
||||
occurredAt: daysFromNow(14).toISOString(),
|
||||
status: "PLANNED",
|
||||
}),
|
||||
)
|
||||
).data;
|
||||
|
||||
const moved = daysFromNow(21);
|
||||
const res = await patch(created.id as string, {
|
||||
occurredAt: moved.toISOString(),
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const reminders = await prisma.measurementReminder.findMany({
|
||||
where: { userId: OWNER_ID },
|
||||
});
|
||||
// The count is the assertion. "The reminder moved" would also be true of a
|
||||
// service that left the old row behind and minted a fresh one.
|
||||
expect(reminders).toHaveLength(1);
|
||||
expect(reminders[0]!.nextDueAt?.toISOString()).toBe(moved.toISOString());
|
||||
});
|
||||
|
||||
it("stops nudging once the visit is cancelled, without deleting the row", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const created = (
|
||||
await body(
|
||||
await post({
|
||||
occurredAt: daysFromNow(14).toISOString(),
|
||||
status: "PLANNED",
|
||||
}),
|
||||
)
|
||||
).data;
|
||||
|
||||
await patch(created.id as string, { status: "CANCELLED" });
|
||||
|
||||
const reminders = await prisma.measurementReminder.findMany({
|
||||
where: { userId: OWNER_ID },
|
||||
});
|
||||
expect(reminders).toHaveLength(1);
|
||||
expect(reminders[0]!.enabled).toBe(false);
|
||||
expect(reminders[0]!.nextDueAt).toBeNull();
|
||||
});
|
||||
|
||||
it("retires the reminder when the visit is deleted", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const created = (
|
||||
await body(
|
||||
await post({
|
||||
occurredAt: daysFromNow(14).toISOString(),
|
||||
status: "PLANNED",
|
||||
}),
|
||||
)
|
||||
).data;
|
||||
|
||||
const res = await deleteEncounter(
|
||||
new Request(`http://localhost/api/encounters/${created.id}`, {
|
||||
method: "DELETE",
|
||||
}) as never,
|
||||
{ params: Promise.resolve({ id: created.id as string }) } as never,
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const reminder = await prisma.measurementReminder.findFirstOrThrow({
|
||||
where: { userId: OWNER_ID },
|
||||
});
|
||||
expect(reminder.deletedAt).not.toBeNull();
|
||||
expect(reminder.nextDueAt).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("closing a checkup by filing a visit", () => {
|
||||
async function seedCheckup() {
|
||||
return getPrismaClient().measurementReminder.create({
|
||||
data: {
|
||||
userId: OWNER_ID,
|
||||
label: "Annual blood panel",
|
||||
intervalDays: 365,
|
||||
anchorDate: daysFromNow(-370),
|
||||
notifyHour: 9,
|
||||
nextDueAt: daysFromNow(-5),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
it("advances the checkup when the visit is filed as done", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const checkup = await seedCheckup();
|
||||
|
||||
const res = await post({
|
||||
occurredAt: daysFromNow(-1).toISOString(),
|
||||
status: "DONE",
|
||||
reminderId: checkup.id,
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
|
||||
const after = await prisma.measurementReminder.findUniqueOrThrow({
|
||||
where: { id: checkup.id },
|
||||
});
|
||||
expect(after.lastSatisfiedAt).not.toBeNull();
|
||||
expect(after.nextDueAt!.getTime()).toBeGreaterThan(Date.now());
|
||||
});
|
||||
|
||||
it("is idempotent — filing it done twice changes nothing and still succeeds", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const checkup = await seedCheckup();
|
||||
const created = (
|
||||
await body(
|
||||
await post({
|
||||
occurredAt: daysFromNow(-1).toISOString(),
|
||||
status: "DONE",
|
||||
reminderId: checkup.id,
|
||||
}),
|
||||
)
|
||||
).data;
|
||||
|
||||
const first = await prisma.measurementReminder.findUniqueOrThrow({
|
||||
where: { id: checkup.id },
|
||||
});
|
||||
|
||||
const res = await patch(created.id as string, { status: "DONE" });
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const second = await prisma.measurementReminder.findUniqueOrThrow({
|
||||
where: { id: checkup.id },
|
||||
});
|
||||
expect(second.lastSatisfiedAt?.toISOString()).toBe(
|
||||
first.lastSatisfiedAt?.toISOString(),
|
||||
);
|
||||
expect(second.nextDueAt?.toISOString()).toBe(
|
||||
first.nextDueAt?.toISOString(),
|
||||
);
|
||||
});
|
||||
|
||||
it("refuses a planned visit that also claims to close a checkup", async () => {
|
||||
const checkup = await seedCheckup();
|
||||
const res = await post({
|
||||
occurredAt: daysFromNow(14).toISOString(),
|
||||
status: "PLANNED",
|
||||
reminderId: checkup.id,
|
||||
});
|
||||
// A visit that has not happened cannot have closed anything.
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
|
||||
it("refuses an appointment reminder as the checkup to close", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
await post({
|
||||
occurredAt: daysFromNow(14).toISOString(),
|
||||
status: "PLANNED",
|
||||
});
|
||||
const appointment = await prisma.measurementReminder.findFirstOrThrow({
|
||||
where: { userId: OWNER_ID, origin: "ENCOUNTER" },
|
||||
});
|
||||
|
||||
const res = await post({
|
||||
occurredAt: daysFromNow(-1).toISOString(),
|
||||
status: "DONE",
|
||||
reminderId: appointment.id,
|
||||
});
|
||||
// Accepting it would let one visit re-point another visit's reminder.
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the Vorsorge list", () => {
|
||||
it("does not show a booked appointment as a checkup", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
await prisma.measurementReminder.create({
|
||||
data: {
|
||||
userId: OWNER_ID,
|
||||
label: "Annual blood panel",
|
||||
intervalDays: 365,
|
||||
notifyHour: 9,
|
||||
nextDueAt: daysFromNow(30),
|
||||
},
|
||||
});
|
||||
await post({
|
||||
occurredAt: daysFromNow(14).toISOString(),
|
||||
status: "PLANNED",
|
||||
});
|
||||
|
||||
// Two rows exist on the engine; one of them is an appointment.
|
||||
expect(await prisma.measurementReminder.count()).toBe(2);
|
||||
|
||||
const res = await listReminders();
|
||||
expect(res.status).toBe(200);
|
||||
const listed = (await res.json()) as {
|
||||
data: Array<{ label: string; origin: string }>;
|
||||
};
|
||||
// Exactly the checkup. Had the exclusion been missing, the mapper would
|
||||
// have thrown and this would be a 500 rather than a wrong list — which is
|
||||
// the point of the refusal.
|
||||
expect(listed.data).toHaveLength(1);
|
||||
expect(listed.data[0]!.label).toBe("Annual blood panel");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user