mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42: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
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* A booked visit's reminder appears on no preventive-care surface.
|
||||
*
|
||||
* A planned encounter mints one `MeasurementReminder` with `origin:
|
||||
* ENCOUNTER`, because an appointment is a reminder with a date and a second
|
||||
* engine would be a second cron, a second queue and a second set of
|
||||
* preferences. The cost of that decision is this: every READ that means "the
|
||||
* checkups this person keeps" now has to say so, or the checkup list fills up
|
||||
* with appointments.
|
||||
*
|
||||
* The research that proposed the feature called this "a one-line `where`". It
|
||||
* is four, and the fourth is in a different subsystem, which is exactly why it
|
||||
* is worth a guard rather than a comment.
|
||||
*
|
||||
* Two halves, deliberately:
|
||||
*
|
||||
* 1. **By source text** — each of the four read sites contains an origin
|
||||
* exclusion. Whitespace-tolerant, and it ASSERTS A NON-ZERO MATCH COUNT.
|
||||
* A matcher that matches nothing is green for the wrong reason; the
|
||||
* Bearer-scope guard in this directory stayed blind for months because
|
||||
* its regex demanded a literal a two-line call never produced, and it
|
||||
* passed by finding nothing rather than by finding everything.
|
||||
* 2. **By behaviour** — the DTO mapper refuses an `ENCOUNTER` row. Source
|
||||
* text cannot tell a filter that runs from one that is written, and the
|
||||
* refusal is what turns a lost filter into a loud failure instead of an
|
||||
* appointment relabelled as a checkup.
|
||||
*
|
||||
* What this file does NOT prove: that the four queries run. That is what the
|
||||
* mapper's throw is for, and what an integration test against real rows would
|
||||
* add. Written down so the limit is visible rather than assumed away.
|
||||
*/
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { toMeasurementReminderDto } from "@/lib/measurement-reminders/dto";
|
||||
import type { MeasurementReminder } from "@/generated/prisma/client";
|
||||
|
||||
const REPO_ROOT = resolve(__dirname, "../..");
|
||||
|
||||
function read(relativePath: string): string {
|
||||
return readFileSync(resolve(REPO_ROOT, relativePath), "utf8");
|
||||
}
|
||||
|
||||
/**
|
||||
* Every read that answers "what preventive care is due", and what each one
|
||||
* feeds. Named rather than discovered: a discovery sweep would have to guess
|
||||
* which reminder reads are Vorsorge surfaces and which are the engine itself,
|
||||
* and the engine's two reads must NOT carry the exclusion — firing the
|
||||
* appointment and satisfying it is the whole point of the row.
|
||||
*/
|
||||
const VORSORGE_READ_SITES: ReadonlyArray<{ file: string; surface: string }> = [
|
||||
{
|
||||
file: "src/app/api/measurement-reminders/route.ts",
|
||||
surface: "the reminder list the checkups page renders",
|
||||
},
|
||||
{
|
||||
file: "src/lib/daily/load-digest.ts",
|
||||
surface: "the daily digest's due list",
|
||||
},
|
||||
{
|
||||
file: "src/lib/insights/feature-blocks.ts",
|
||||
surface: "the insights preventive-care block",
|
||||
},
|
||||
{
|
||||
file: "src/lib/mcp/tools.ts",
|
||||
surface: "the MCP get_preventive_care tool",
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
* `origin: { not: "ENCOUNTER" }`, tolerant of any formatting Prettier might
|
||||
* choose — line breaks between the key and the object, single or double
|
||||
* quotes, a trailing comma.
|
||||
*/
|
||||
const EXCLUSION = /origin\s*:\s*\{\s*not\s*:\s*["']ENCOUNTER["']\s*,?\s*\}/g;
|
||||
|
||||
function reminderRow(
|
||||
origin: MeasurementReminder["origin"],
|
||||
): MeasurementReminder {
|
||||
const at = new Date("2026-07-01T09:00:00.000Z");
|
||||
return {
|
||||
id: "reminder-1",
|
||||
userId: "user-1",
|
||||
label: "Dr. Example",
|
||||
measurementType: null,
|
||||
intervalDays: null,
|
||||
rrule: null,
|
||||
anchorDate: at,
|
||||
endsOn: null,
|
||||
origin,
|
||||
notifyHour: 9,
|
||||
location: null,
|
||||
nextDueAt: at,
|
||||
lastSatisfiedAt: null,
|
||||
enabled: true,
|
||||
createdAt: at,
|
||||
updatedAt: at,
|
||||
deletedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
describe("appointment reminders are excluded from every Vorsorge read", () => {
|
||||
it("names a read set that is not empty (no vacuous pass)", () => {
|
||||
expect(VORSORGE_READ_SITES.length).toBe(4);
|
||||
});
|
||||
|
||||
for (const { file, surface } of VORSORGE_READ_SITES) {
|
||||
it(`excludes ENCOUNTER rows from ${surface}`, () => {
|
||||
const source = read(file);
|
||||
const matches = source.match(EXCLUSION) ?? [];
|
||||
expect(
|
||||
matches.length,
|
||||
`${file} feeds ${surface} and no longer excludes ENCOUNTER-origin ` +
|
||||
"rows, so a booked appointment will surface there as a checkup",
|
||||
).toBeGreaterThan(0);
|
||||
});
|
||||
}
|
||||
|
||||
it("leaves the engine's own reads alone", () => {
|
||||
// The cron fires these rows and the satisfy primitive resolves them. An
|
||||
// exclusion in either would mean a booked appointment that never nudges,
|
||||
// which is the opposite failure and just as silent.
|
||||
for (const file of [
|
||||
"src/lib/jobs/measurement-reminder.ts",
|
||||
"src/lib/measurement-reminders/satisfy.ts",
|
||||
]) {
|
||||
const matches = read(file).match(EXCLUSION) ?? [];
|
||||
expect(
|
||||
matches.length,
|
||||
`${file} is the engine, not a Vorsorge surface — excluding ENCOUNTER ` +
|
||||
"rows here would stop appointments firing at all",
|
||||
).toBe(0);
|
||||
}
|
||||
});
|
||||
|
||||
it("the Coach dedup read needs nothing, because it is already narrow", () => {
|
||||
// It filters to `origin: "COACH"`, so an ENCOUNTER row cannot reach it.
|
||||
// Asserted rather than assumed: if that narrowing ever widened, this file
|
||||
// is where the consequence should surface.
|
||||
expect(read("src/lib/ai/coach/suggest-gate.ts")).toMatch(
|
||||
/origin\s*:\s*["']COACH["']/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the reminder DTO refuses an appointment row", () => {
|
||||
it("maps a VORSORGE row normally", () => {
|
||||
expect(toMeasurementReminderDto(reminderRow("VORSORGE")).origin).toBe(
|
||||
"VORSORGE",
|
||||
);
|
||||
});
|
||||
|
||||
it("maps a COACH row normally", () => {
|
||||
expect(toMeasurementReminderDto(reminderRow("COACH")).origin).toBe("COACH");
|
||||
});
|
||||
|
||||
it("throws on an ENCOUNTER row rather than relabelling it", () => {
|
||||
// The invariant: every query feeding this mapper excludes them, so one
|
||||
// arriving means a read site lost its filter. Failing loudly is the only
|
||||
// honest answer — the alternative is an appointment shown as a checkup.
|
||||
expect(() => toMeasurementReminderDto(reminderRow("ENCOUNTER"))).toThrow(
|
||||
/ENCOUNTER-origin reminder/,
|
||||
);
|
||||
});
|
||||
|
||||
it("names the guard file in the message, so the next reader finds the proof", () => {
|
||||
expect(() => toMeasurementReminderDto(reminderRow("ENCOUNTER"))).toThrow(
|
||||
/encounter-reminder-exclusion/,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -923,6 +923,22 @@ const DELEGABLE_ROUTES: Record<string, DelegableEntry> = {
|
||||
domain: "measurements",
|
||||
why: "One nutrient's daily series over the record, scoped by the resolved user id.",
|
||||
},
|
||||
"app/api/encounters/route.ts": {
|
||||
domain: "profile",
|
||||
why: "The record's visits — what happened at a practice and what is booked. Record content in the same sense as the allergies and family history already under this domain: it names no credential, no integration and no notification channel. Carries the honest cost of that classification, which is that a delegate granted `profile` for allergies also sees the visit list; the consent copy names visits for exactly this reason. The create arm is a delegable write.",
|
||||
},
|
||||
"app/api/encounters/[id]/route.ts": {
|
||||
domain: "profile",
|
||||
why: "One visit of the record, fetch-then-guard against the resolved user. The links it resolves are read through the link service, which narrows both ends to the same resolved id.",
|
||||
},
|
||||
"app/api/practitioners/route.ts": {
|
||||
domain: "profile",
|
||||
why: "The record's own address book of doctors and practices. Record content, not account configuration — it touches no credential, no integration and no notification channel, which is the fence the classification turns on. The create arm is a delegable write.",
|
||||
},
|
||||
"app/api/practitioners/[id]/route.ts": {
|
||||
domain: "profile",
|
||||
why: "One practitioner of the record, fetch-then-guard against the resolved user.",
|
||||
},
|
||||
"app/api/illness/episodes/route.ts": {
|
||||
domain: "illness",
|
||||
why: "The record's illness episodes. Module-gated on the record on both arms, so a delegate gets the surface only where the owner switched it on; the create arm is a delegable write.",
|
||||
@@ -1108,6 +1124,14 @@ const DELEGABLE_ROUTES: Record<string, DelegableEntry> = {
|
||||
domain: "illness",
|
||||
why: "Reopening a deleted episode with its day logs intact. Reached only through this module's MANAGE arm; the argument for admitting it is the manage literal's reason line, and the entry here is what leg (a) freezes and leg (f) checks the section against.",
|
||||
},
|
||||
"app/api/encounters/[id]/restore/route.ts": {
|
||||
domain: "profile",
|
||||
why: "Reopening a deleted visit with its links intact. Reached only through this module's MANAGE arm; the argument for admitting it is the manage literal's reason line, and the entry here is what leg (a) freezes and leg (f) checks the section against.",
|
||||
},
|
||||
"app/api/practitioners/[id]/restore/route.ts": {
|
||||
domain: "profile",
|
||||
why: "Reopening a deleted address-book entry. Reached only through this module's MANAGE arm; the argument for admitting it is the manage literal's reason line, and the entry here is what leg (a) freezes and leg (f) checks the section against.",
|
||||
},
|
||||
"app/api/mood-entries/bulk-delete/route.ts": {
|
||||
domain: "mind",
|
||||
why: "Tombstoning a selection. Reached only through this module's MANAGE arm; the argument for admitting it is the manage literal's reason line, and the entry here is what leg (a) freezes and leg (f) checks the section against.",
|
||||
@@ -1347,6 +1371,10 @@ const DELEGABLE_WRITE_ROUTES: Record<string, string> = {
|
||||
"Adding an analyte to the record's catalogue. A name the record already tracks is the ordinary 409 from the same `(userId, name)` uniqueness the owner would hit themselves.",
|
||||
"app/api/illness/episodes/route.ts":
|
||||
"Opening an illness episode. The module gate runs against the RECORD before the write, so a delegate cannot create an episode inside a record whose owner switched the module off.",
|
||||
"app/api/encounters/route.ts":
|
||||
"Filing a visit, or booking one. Every id the body may carry — the practitioner, the checkup it closes, the three link arrays — is re-narrowed to the resolved record before anything is written, so a delegate cannot attach one record's document to another's visit. Booking mints the appointment reminder under the RECORD, which is correct: it is the owner's appointment and the owner's channels that should be nudged about it.",
|
||||
"app/api/practitioners/route.ts":
|
||||
"Adding a doctor or practice to the record's address book. Nothing is unique across accounts here by design, so a delegate adding a practice the owner already has produces a second row rather than a collision with somebody else's namespace.",
|
||||
"app/api/medications/[id]/side-effects/route.ts":
|
||||
"Recording a side effect. Admitted on one condition, met at the call site: the POST rate bucket keys on the ACTOR, so a delegate burns their own allowance rather than the owner's and cannot collect a fresh one by switching records.",
|
||||
"app/api/medications/route.ts":
|
||||
@@ -1453,6 +1481,26 @@ const DELEGABLE_MANAGE_ROUTES: Record<string, ManageEntry> = {
|
||||
conditions: ["C4"],
|
||||
why: "Editing and deleting an episode; the delete soft-deletes. C4 on the edit.",
|
||||
},
|
||||
"app/api/encounters/[id]/route.ts": {
|
||||
domain: "profile",
|
||||
conditions: ["C4"],
|
||||
why: "Editing and deleting a visit; the delete soft-deletes and retires the visit's own appointment reminder with it, never a checkup the visit closed. C4 on the edit: the date, the status, the kind and the practitioner are filed before and after, and the encrypted reason and outcome deliberately are not.",
|
||||
},
|
||||
"app/api/encounters/[id]/restore/route.ts": {
|
||||
domain: "profile",
|
||||
conditions: [],
|
||||
why: "Reopening a deleted visit with its links intact.",
|
||||
},
|
||||
"app/api/practitioners/[id]/route.ts": {
|
||||
domain: "profile",
|
||||
conditions: ["C4"],
|
||||
why: "Correcting and removing one address-book entry; the delete soft-deletes and leaves every visit that named it, because the reference nulls rather than cascades. C4 on the edit, over the plaintext fields only.",
|
||||
},
|
||||
"app/api/practitioners/[id]/restore/route.ts": {
|
||||
domain: "profile",
|
||||
conditions: [],
|
||||
why: "Reopening a deleted address-book entry. It does not re-attach the visits whose reference was nulled, because which visits meant this practice is not recoverable.",
|
||||
},
|
||||
"app/api/illness/episodes/[id]/resolve/route.ts": {
|
||||
domain: "illness",
|
||||
conditions: [],
|
||||
@@ -1809,7 +1857,7 @@ const ACTOR_ROUTES: Record<string, string> = {
|
||||
* read-only profile summary makes the next explicit admission: 197 → 198.
|
||||
* The two initial Cycle reads follow as separate, scoped admissions: 198 → 200.
|
||||
*/
|
||||
const FROZEN_ENTRY_COUNT = 200;
|
||||
const FROZEN_ENTRY_COUNT = 212;
|
||||
|
||||
/**
|
||||
* The two surfaces that authenticate a Bearer token outside `requireAuth` —
|
||||
@@ -2570,7 +2618,7 @@ describe("(g) the MANAGE route set is frozen", () => {
|
||||
|
||||
it("keeps the admitted mutation inventory complete and discoverable", () => {
|
||||
expect(ADMITTED_MUTATING_HANDLERS.length).toBeGreaterThan(0);
|
||||
expect(ADMITTED_MUTATING_HANDLERS.length).toBe(62);
|
||||
expect(ADMITTED_MUTATING_HANDLERS.length).toBe(70);
|
||||
|
||||
const expected = ADMITTED_MUTATING_HANDLERS.map(
|
||||
({ handlerModule, action, level }) =>
|
||||
|
||||
@@ -26,7 +26,7 @@ describe("sharing and handler inventories", () => {
|
||||
});
|
||||
|
||||
it("keeps every admitted mutation uniquely addressable and fully controlled", () => {
|
||||
expect(ADMITTED_MUTATING_HANDLERS).toHaveLength(62);
|
||||
expect(ADMITTED_MUTATING_HANDLERS).toHaveLength(70);
|
||||
expect(
|
||||
new Set(
|
||||
ADMITTED_MUTATING_HANDLERS.map(
|
||||
|
||||
Reference in new issue
Block a user