All files / src/app/api/patient/programmes/consent route.ts

84% Statements 21/25
77.77% Branches 14/18
66.66% Functions 2/3
95% Lines 19/20

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                  1x                             7x 7x 5x         5x 7x 1x             4x 1x         3x       3x 2x               2x 2x 2x 2x           2x       2x 2x    
import { NextRequest, NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import {
  PROGRAMME_CONSENT_SOURCES,
  joinProgramme,
  resolveProgrammePatient,
  withdrawProgramme,
} from "@/lib/patient/programme-consent";
 
export const dynamic = "force-dynamic";
 
/**
 * Consent to one bonus program (#934, step 4; ADR-080, decision 6).
 *
 * POST   { programmeId, patientId?, source? } → join; idempotent while a
 *        consent is open. source is one of PROGRAMME_CONSENT_SOURCES
 *        (default "patient-portal").
 * DELETE ?programmeId=&patientId=     → withdraw; transfers stop at once
 *
 * A PATIENT acts on their own record; EDC_ADMIN names one. Separate from
 * study consent (/api/patient/research) and from the Art. 71 opt-out
 * (/api/patient/opt-out): joining never enrolls anyone in research.
 */
export async function POST(req: NextRequest): Promise<NextResponse> {
  const auth = await requireAuth(["PATIENT", "EDC_ADMIN"]);
  if (isAuthError(auth)) return auth;
  const body = (await req.json().catch(() => ({}))) as {
    programmeId?: string;
    patientId?: string;
    source?: string;
  };
  const source = body.source ?? "patient-portal";
  if (!(PROGRAMME_CONSENT_SOURCES as readonly string[]).includes(source)) {
    return NextResponse.json(
      {
        error: `source must be one of ${PROGRAMME_CONSENT_SOURCES.join(", ")}`,
      },
      { status: 400 },
    );
  }
  if (!body.programmeId) {
    return NextResponse.json(
      { error: "programmeId is required" },
      { status: 400 },
    );
  }
  const who = await resolveProgrammePatient(
    auth.session.roles,
    body.patientId ?? null,
  );
  if (who instanceof NextResponse) return who;
  return joinProgramme(
    who.patientId as string,
    body.programmeId,
    source as (typeof PROGRAMME_CONSENT_SOURCES)[number],
  );
}
 
export async function DELETE(req: NextRequest): Promise<NextResponse> {
  const auth = await requireAuth(["PATIENT", "EDC_ADMIN"]);
  Iif (isAuthError(auth)) return auth;
  const programmeId = req.nextUrl.searchParams.get("programmeId");
  Iif (!programmeId) {
    return NextResponse.json(
      { error: "programmeId is required" },
      { status: 400 },
    );
  }
  const who = await resolveProgrammePatient(
    auth.session.roles,
    req.nextUrl.searchParams.get("patientId"),
  );
  Iif (who instanceof NextResponse) return who;
  return withdrawProgramme(who.patientId as string, programmeId);
}