fix(documents): close v1.25.1 library QA findings (backend)

Default documentDate at store time. A store-only upload no longer lands
with a NULL filing date: when the client omits one, default it to the
upload day so the column is always set on a stored row. The library list
sorts, groups, and range-filters on the same effective date the UI shows,
so undated uploads no longer mis-sort under nulls-last, break date-group
headers, or fall through the range filter. The date stays user-editable
and may be cleared back to NULL via PATCH.

Block re-extraction once any fact is APPROVED. A partially-confirmed
document stays EXTRACTED, which the CONFIRMED guard does not catch;
re-extracting would delete the approved facts (severing the committed
record's provenance) and re-stage them as PENDING, letting a second
approval duplicate the committed lab / condition / medication. The
extract route now refuses with 409 (documents.inbound.alreadyPartlyConfirmed)
when approved facts exist; the user must finish reviewing or discard first.

RFC 5987-encode the original-download filename. The Content-Disposition
now carries both an ASCII filename= fallback and a filename*=UTF-8'' value
so a non-ASCII name survives intact; the stored set is inline-safe by
construction, so the dead attachment branch is removed.

Add a light per-user rate-limit backstop to the original-read, PATCH, and
DELETE routes (generous; self-DoS only — the routes are owner-scoped).

Lean the list counts: tally facts for the whole page in one grouped count
instead of materialising fact-id rows, and exclude REJECTED facts from
factCount. Extend the documentDate index with deleted_at to cover the
list scan. Document the deliberate plaintext-title PHI tradeoff at the
column and the store site.
This commit is contained in:
Marc-André Bombeck committed 2026-06-28 23:48:43 +02:00
1 parent 84543ad540
commit eb1779d5c1
14 files changed
+317 -94

No files matched your search

@@ -15,6 +15,9 @@ vi.mock("@/lib/db", () => ({
create: vi.fn(),
findMany: vi.fn(),
},
extractedFact: {
groupBy: vi.fn(),
},
},
}));
@@ -110,6 +113,7 @@ beforeEach(() => {
vi.clearAllMocks();
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(requireModuleEnabled).mockResolvedValue({ enabled: true } as never);
vi.mocked(prisma.extractedFact.groupBy).mockResolvedValue([] as never);
});
describe("POST /api/documents/inbound (store-only)", () => {
@@ -153,10 +157,17 @@ describe("POST /api/documents/inbound (store-only)", () => {
});
describe("GET /api/documents/inbound (list)", () => {
it("applies q + kind filters and returns a keyset page", async () => {
it("applies q + kind filters and returns a keyset page with grouped counts", async () => {
vi.mocked(prisma.inboundDocument.findMany).mockResolvedValue([
{ ...docRow({ id: "d1" }), _count: { facts: 0 }, facts: [] },
{ ...docRow({ id: "d2" }), _count: { facts: 2 }, facts: [{ id: "f1" }] },
docRow({ id: "d1" }),
docRow({ id: "d2" }),
] as never);
// factCount excludes REJECTED; pendingCount is the PENDING subset. The
// counts come from a single grouped query, not from materialised fact rows.
vi.mocked(prisma.extractedFact.groupBy).mockResolvedValue([
{ documentId: "d2", status: "PENDING", _count: { _all: 1 } },
{ documentId: "d2", status: "APPROVED", _count: { _all: 1 } },
{ documentId: "d2", status: "REJECTED", _count: { _all: 3 } },
] as never);
const res = await get(
@@ -169,6 +180,14 @@ describe("GET /api/documents/inbound (list)", () => {
expect(body.data.documents).toHaveLength(2);
expect(body.data.nextCursor).toBeNull();
// d1 has no facts; d2 has 1 pending + 1 approved + 3 rejected → factCount 2
// (rejected excluded), pendingCount 1.
const [d1, d2] = body.data.documents;
expect(d1.factCount).toBe(0);
expect(d1.pendingCount).toBe(0);
expect(d2.factCount).toBe(2);
expect(d2.pendingCount).toBe(1);
const arg = vi.mocked(prisma.inboundDocument.findMany).mock.calls[0]![0]!;
const where = arg.where!;
expect(where.userId).toBe("user-1");
@@ -178,6 +197,8 @@ describe("GET /api/documents/inbound (list)", () => {
{ title: { contains: "panel", mode: "insensitive" } },
{ filename: { contains: "panel", mode: "insensitive" } },
]);
// No fact rows are materialised in the page query — counts come from groupBy.
expect(arg.include).toBeUndefined();
// limit+1 fetched for the has-more probe.
expect(arg.take).toBe(11);
});