feat(export): remember the practice name between doctor reports

The doctor-report contract has always described the practice / clinic name as
a stored preference, and `/api/auth/me` has returned
`User.lastReportPracticeName` since it shipped, but nothing ever wrote the
column. Every report asked for the clinic name again.

The export route now persists the sanitised name after the requested artefact
has actually been produced, so a generation that throws cannot overwrite the
remembered value with a half-typed one. Clearing the field persists null, which
keeps the cover line cleared next time. The export panel seeds its input from
the auth payload during render, gated per user id, so a late resolve never
clobbers a field the user is already typing into, and a finished export
invalidates the cached payload so a remount reads the new value.
This commit is contained in:
Marc-André Bombeck committed 2026-07-25 15:00:27 +02:00
1 parent ddc18bbd11
commit a3f1d780e7
10 files changed
+229

No files matched your search

@@ -249,3 +249,76 @@ describe("POST /api/export/health-record — outputs", () => {
);
});
});
/**
* The practice name is a remembered preference (`User.lastReportPracticeName`),
* so a monthly report does not ask for the clinic name again every time. The
* write happens only once the artefact exists — a generation that throws must
* not overwrite the remembered value with a half-typed one.
*
* Mutation check: drop the `rememberPracticeName` call in the route and the
* first two cases go red; move it above the generation and the
* failed-generation case goes red.
*/
describe("POST /api/export/health-record — remembered practice name", () => {
it("persists the sanitised practice name after a PDF export", async () => {
const { POST } = await import("../route");
const res = await POST(
mkReq({ format: "pdf", practiceName: " Sample Practice " }),
);
expect(res.status).toBe(200);
expect(prisma.user.update).toHaveBeenCalledWith({
where: { id: "user-1" },
data: { lastReportPracticeName: "Sample Practice" },
});
});
it("persists the practice name after a FHIR export", async () => {
const { POST } = await import("../route");
const res = await POST(
mkReq({ format: "fhir", practiceName: "Sample Practice" }),
);
expect(res.status).toBe(200);
expect(prisma.user.update).toHaveBeenCalledWith({
where: { id: "user-1" },
data: { lastReportPracticeName: "Sample Practice" },
});
});
it("persists null when the user cleared the practice name", async () => {
const { POST } = await import("../route");
const res = await POST(mkReq({ format: "pdf" }));
expect(res.status).toBe(200);
expect(prisma.user.update).toHaveBeenCalledWith({
where: { id: "user-1" },
data: { lastReportPracticeName: null },
});
});
it("writes nothing when the selection is rejected", async () => {
const { POST } = await import("../route");
const res = await POST(mkReq({ format: "xml", practiceName: "Sample" }));
expect(res.status).toBe(422);
expect(prisma.user.update).not.toHaveBeenCalled();
});
it("writes nothing when the generation fails", async () => {
vi.mocked(prisma.measurement.findMany).mockRejectedValue(
new Error("read failed"),
);
const { POST } = await import("../route");
const res = await POST(
mkReq({ format: "pdf", practiceName: "Sample Practice" }),
);
expect(res.status).toBe(500);
expect(prisma.user.update).not.toHaveBeenCalled();
});
it("narrows the update to the session user, never the body", async () => {
const { POST } = await import("../route");
await POST(mkReq({ format: "fhir", practiceName: "Sample Practice" }));
expect(prisma.user.update).toHaveBeenCalledWith(
expect.objectContaining({ where: { id: "user-1" } }),
);
});
});
+26
View File
@@ -54,6 +54,28 @@ import { parseLocaleFromAcceptLanguage } from "@/lib/format-locale";
import { locales, type Locale } from "@/lib/i18n/config";
import { resolveUserTimezone } from "@/lib/tz/resolver";
/**
* Remember the practice / clinic name for the next report, so the user does
* not retype it every time (`User.lastReportPracticeName`, the preference the
* doctor-report contract has always promised).
*
* Called only once the requested artefact has actually been produced: a
* generation that throws must not overwrite the remembered name with a
* half-typed one. `null` is a legitimate value — the user cleared the line and
* the next cover should stay cleared. The value is the already-sanitised one
* (`sanitisePracticeName`, 120-char cap), and the update names its single
* field explicitly rather than spreading the parsed selection.
*/
async function rememberPracticeName(
userId: string,
practiceName: string | null,
) {
await prisma.user.update({
where: { id: userId },
data: { lastReportPracticeName: practiceName },
});
}
export const POST = apiHandler(async (request: NextRequest) => {
const { user } = await requireAuth();
annotate({ action: { name: "export.health-record.build" } });
@@ -189,6 +211,7 @@ export const POST = apiHandler(async (request: NextRequest) => {
fhirRecords,
);
const json = JSON.stringify(bundle);
await rememberPracticeName(user.id, practiceName);
annotate({
meta: { format: "fhir", bytes: json.length, days: range.days },
});
@@ -212,6 +235,9 @@ export const POST = apiHandler(async (request: NextRequest) => {
includeCharts,
aiSummary,
});
// Both remaining formats (`pdf`, `package`) carry the rendered PDF, so the
// artefact exists from here on.
await rememberPracticeName(user.id, practiceName);
if (selection.format === "pdf") {
annotate({