feat(visits): resolve where the window before a booked visit starts

The visit preparation and the doctor report offered for an appointment
started at the last visit of any kind. An internist one week and a
dermatologist the next gave the dermatologist a one-week window of
nothing it is about.

The list read now publishes `preparation` on every booked upcoming
visit: the first day of the window, the rule that chose it and the
earlier visit it is the day of. First match wins over the visits that
took place before it: the same practitioner, else a practitioner of
the same specialty (free text, compared without case or spacing), else
the same kind for routine, preventive and therapy visits, else the last
90 days. One query of past visits per page, independent of the list's
own filters. Null on every other row; absent on the detail and on
write responses.
This commit is contained in:
Marc-André Bombeck committed 2026-10-21 12:32:12 +02:00
1 parent 0dcafeeb70
commit 402cf1a35a
8 files changed
+781 -8

No files matched your search

+20 -8
View File
@@ -35,6 +35,7 @@ import {
} from "@/lib/validations/encounters";
import { encounterKindLabel } from "@/lib/encounters/kind-label";
import { toEncounterDTO, type EncounterListDTO } from "@/lib/encounters/dto";
import { loadVisitPreparations } from "@/lib/encounters/preparation-read";
import {
ENCOUNTER_INCLUDE,
applyEncounterLinks,
@@ -143,12 +144,17 @@ export const GET = apiHandler(async (request: NextRequest) => {
// them makes the first read zero and the second unfile everything on save.
// Three grouped queries for the page, not three per row.
const visible = await actingDomainVisibility(prisma, grantId);
const links = await loadEncounterLinksForMany(
prisma,
user.id,
[...upcoming, ...past].map((row) => row.id),
visible,
);
// The window before each booked visit reads the whole history, not this
// page of it: the visit it starts at may sit outside the caller's filter.
const [links, preparations] = await Promise.all([
loadEncounterLinksForMany(
prisma,
user.id,
[...upcoming, ...past].map((row) => row.id),
visible,
),
loadVisitPreparations(prisma, user.id, upcoming, now),
]);
annotate({
action: { name: "encounter.visit.list", entity_type: "encounter" },
@@ -156,8 +162,14 @@ export const GET = apiHandler(async (request: NextRequest) => {
});
const body: EncounterListDTO = {
upcoming: upcoming.map((row) => toEncounterDTO(row, links.get(row.id))),
past: past.map((row) => toEncounterDTO(row, links.get(row.id))),
upcoming: upcoming.map((row) => ({
...toEncounterDTO(row, links.get(row.id)),
preparation: preparations.get(row.id) ?? null,
})),
past: past.map((row) => ({
...toEncounterDTO(row, links.get(row.id)),
preparation: null,
})),
};
return apiListSuccess(body, combinedCap(upcomingCap, pastCap));
});