Merge remote-tracking branch 'origin/feat/documents-procedure-filter' into release/v1.40.2

This commit is contained in:
Marc-André Bombeck committed 2026-10-05 07:17:17 +02:00
commit 03c43ef70a
24 files changed
+1308 -54

No files matched your search

+47
View File
@@ -7,10 +7,13 @@
*/
import type { Prisma } from "@/generated/prisma/client";
import { prisma } from "@/lib/db";
import { decryptField } from "@/lib/encounters/dto";
import { MAX_PROCEDURES } from "@/lib/encounters/procedures";
import { listTargetsBySource, replaceTargets } from "@/lib/links";
import type {
DocumentConditionLinkDto,
DocumentEncounterLinkDto,
DocumentLinkedProcedureDto,
DocumentVaccinationLinkDto,
} from "@/lib/validations/inbound-documents";
import { resolveCatalogEntry } from "@/lib/vaccinations/vaccine-catalog";
@@ -77,6 +80,50 @@ export async function loadDocumentEncounterLinks(
return map;
}
/**
* The record's procedures and surgeries that hold at least one LIVE document
* link, newest first — the vault filter bar's procedure choices.
*
* Every live PROCEDURE visit counts whatever its status: a pre-op letter filed
* against a booked operation belongs to it as much as the discharge letter
* belongs to one that happened. Tombstoned visits and tombstoned documents
* drop out, the same honest default `loadDocumentEncounterLinks` uses. The
* reason and body site are ciphertext at rest and decrypt through the visit
* DTO's fail-soft path, so one row with a key gap reads as unnamed rather than
* failing the usage read. Only the label's columns are selected. Bounded like the procedure history itself.
*/
export async function loadLinkedProcedures(
userId: string,
): Promise<DocumentLinkedProcedureDto[]> {
const rows = await prisma.encounter.findMany({
where: {
userId,
deletedAt: null,
kind: "PROCEDURE",
documentLinks: { some: { userId, document: { deletedAt: null } } },
},
orderBy: [{ occurredAt: "desc" }, { id: "asc" }],
take: MAX_PROCEDURES,
// Only what the label needs: no outcome, no practitioner contact fields.
select: {
id: true,
occurredAt: true,
reasonEncrypted: true,
bodySiteEncrypted: true,
laterality: true,
practitioner: { select: { name: true } },
},
});
return rows.map((row) => ({
encounterId: row.id,
occurredAt: row.occurredAt.toISOString(),
reason: decryptField(row.reasonEncrypted, "reason"),
bodySite: decryptField(row.bodySiteEncrypted, "bodySite"),
laterality: row.laterality,
practitionerName: row.practitioner?.name ?? null,
}));
}
/**
* Narrow a client-sent visit-id list to the caller's LIVE visits.
*
+1 -1
View File
@@ -97,7 +97,7 @@ export interface EncounterDTO {
updatedAt: string;
}
function decryptField(
export function decryptField(
value: Uint8Array | null,
field: "reason" | "outcome" | "bodySite",
): string | null {
+15 -2
View File
@@ -379,7 +379,8 @@ export const inboundDocumentPaths: NonNullable<ZodOpenApiObject["paths"]> = {
in: "query",
required: false,
schema: { type: "string" },
description: "Only documents linked to this visit.",
description:
"Only documents linked to this visit. The procedure filter sends a `linkedProcedures` id from the usage read; an id that is not one of the caller's visits matches nothing.",
},
{
name: "year",
@@ -665,7 +666,7 @@ export const inboundDocumentPaths: NonNullable<ZodOpenApiObject["paths"]> = {
tags: ["Documents"],
summary: "Storage usage + effective limits",
description:
"The caller's vault usage and effective limits: `usedBytes` (every non-purged row — tombstones inside the 30-day undo grace still count), `quotaBytes` (per-user override ?? instance default), `maxFileBytes` (admin-tunable per-file cap), `acceptedExtensions` for picker `accept` lists, and `linkedEpisodes` (episodes carrying at least one live document link — the condition-filter chips). Read this before offering an upload.",
"The caller's vault usage and effective limits: `usedBytes` (every non-purged row — tombstones inside the 30-day undo grace still count), `quotaBytes` (per-user override ?? instance default), `maxFileBytes` (admin-tunable per-file cap), `acceptedExtensions` for picker `accept` lists, `linkedEpisodes` (episodes carrying at least one live document link — the condition-filter chips), and `linkedProcedures` (visits of kind PROCEDURE carrying at least one live document link, newest first — the procedure-filter choices; pass one's `encounterId` to the list's `encounterId` filter). `linkedProcedures` is empty when a sharing grant does not cover the visits section. Read this before offering an upload.",
responses: {
...recordRefusal(),
"200": {
@@ -685,6 +686,18 @@ export const inboundDocumentPaths: NonNullable<ZodOpenApiObject["paths"]> = {
name: z.string(),
}),
),
linkedProcedures: z.array(
z.object({
encounterId: z.string(),
occurredAt: z.iso.datetime({ offset: true }),
reason: z.string().nullable(),
bodySite: z.string().nullable(),
laterality: z
.enum(["LEFT", "RIGHT", "BOTH"])
.nullable(),
practitionerName: z.string().nullable(),
}),
),
assistAvailable: z
.boolean()
.describe(
+23
View File
@@ -349,6 +349,21 @@ export interface DocumentConditionLinkDto {
name: string;
}
/**
* One procedure the vault can be filtered by. The visit's own words, decrypted
* server-side; the client composes the label the visits page shows (the
* reason, else the practice, else the kind) in the reader's locale.
*/
export interface DocumentLinkedProcedureDto {
encounterId: string;
/** ISO-8601 instant of the procedure. */
occurredAt: string;
reason: string | null;
bodySite: string | null;
laterality: "LEFT" | "RIGHT" | "BOTH" | null;
practitionerName: string | null;
}
/**
* One visit link on a document DTO — "this letter belongs to that appointment".
*
@@ -958,6 +973,14 @@ export interface DocumentUsageDto {
* every linked document sits pages deep in the timeline.
*/
linkedEpisodes: DocumentConditionLinkDto[];
/**
* Procedures and surgeries (visits of kind PROCEDURE) carrying at least one
* LIVE document link — the filter bar's procedure choices, sourced
* server-side for the same reason as `linkedEpisodes`. Empty when the
* caller's grant does not cover the visits section, because the label is
* the visit's own text.
*/
linkedProcedures: DocumentLinkedProcedureDto[];
/**
* Whether the AI "Suggest details" action can run for this caller — true when
* a provider (vision, or text + local OCR) is configured. The UI hides the