Files
HealthLog/src/app/api/fhir/MedicationAdministration/route.ts
T
Marc-André BombeckandClaude Opus 4.8 57b8fd8ad9 feat(fhir): read-only FHIR R4 REST search routes
GET /api/fhir/{metadata,Patient,Observation,MedicationStatement,
MedicationAdministration} and $everything, returning type:"searchset"
Bundles (total + self/next links, offset paging via _count clamped to 200)
as application/fhir+json, with an OperationOutcome on error. The per-resource
emitters are reused from src/lib/fhir/resources.ts so the document export and
the REST face share one coding source and can never drift. Auth is a narrow
fhir:read scope via requireAuth (cookie sessions also pass); userId is
narrowed from auth. Read-only — no write handlers. Rate-limited per user.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 01:05:06 +02:00

40 lines
1.5 KiB
TypeScript

/**
* GET /api/fhir/MedicationAdministration — FHIR R4 `searchset` of the caller's
* own acted intakes (`completed` taken / `not-done` skip), with the same
* ATC/RxNorm + dose/route/site coding the document export carries.
*
* Read-only. `userId` narrowed from `requireAuth`; shared emitter is the
* single source of the coding. Offset paging via `_count` (≤200) / `_offset`.
*/
import { NextRequest } from "next/server";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { annotate } from "@/lib/logging/context";
import { checkRateLimit } from "@/lib/rate-limit";
import { medicationAdministrationsFromReportData } from "@/lib/fhir/resources";
import {
FHIR_READ_SCOPE,
loadFhirContext,
operationOutcome,
parsePaging,
searchsetResponse,
} from "@/lib/fhir/rest";
export const GET = apiHandler(async (request: NextRequest) => {
const { user } = await requireAuth(FHIR_READ_SCOPE);
annotate({ action: { name: "fhir.medicationadministration.search" } });
const rl = await checkRateLimit(`fhir:${user.id}`, 120, 60 * 60 * 1000);
if (!rl.allowed) {
return operationOutcome(429, "throttled", "Rate limit exceeded");
}
const { count, offset } = parsePaging(request.nextUrl.searchParams);
const { data, germanAtc } = await loadFhirContext(user.id);
const all = medicationAdministrationsFromReportData(data, { germanAtc });
const page = all.slice(offset, offset + count);
annotate({ meta: { total: all.length, count, offset } });
return searchsetResponse(request.nextUrl, page, all.length, count, offset);
});