All files / src/lib/patient own-record.ts

100% Statements 8/8
100% Branches 8/8
100% Functions 3/3
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39              69x                             59x 13x 13x 9x                     5x    
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { ownPatientIdForSession } from "@/lib/overview/patient";
 
/** True for a session that is a patient and nothing broader. */
export function isPatientOnly(roles: readonly string[]): boolean {
  return roles.includes("PATIENT") && !roles.includes("EDC_ADMIN");
}
 
/**
 * A patient reads and changes their own record only (EHDS Art. 3, GDPR
 * Art. 15; #271 for observations, #475 for the rest).
 *
 * Returns a 403 to send back when a PATIENT names a record that is not theirs,
 * or null when the call may go on. Any other role passes: what it may see is
 * decided by the route's own role gate.
 */
export async function refuseForeignRecord(
  roles: readonly string[],
  patientId: string | null,
): Promise<NextResponse | null> {
  if (!isPatientOnly(roles)) return null;
  const own = ownPatientIdForSession(await getServerSession(authOptions));
  if (own && patientId === own) return null;
  return NextResponse.json(
    {
      error: "Forbidden",
      reason: "Art. 3: a patient reads their own record only",
    },
    { status: 403 },
  );
}
 
/** The record a patient-only session owns, or null. */
export async function ownRecordId(): Promise<string | null> {
  return ownPatientIdForSession(await getServerSession(authOptions));
}