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

84.61% Statements 11/13
75% Branches 6/8
50% Functions 1/2
100% Lines 9/9

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                1x                           7x 7x 7x         7x 7x 6x       6x 5x            
import { NextRequest, NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import {
  parseTransferRequest,
  requestProgrammeTransfer,
  resolveProgrammePatient,
} from "@/lib/patient/programme-consent";
 
export const dynamic = "force-dynamic";
 
/**
 * POST /api/patient/programmes/transfer  { programmeId, year, patientId? }
 *
 * The person tells the hub to send a program their answer for one year
 * (ADR-080, decision 4, second path; #934, step 4). Only under an open
 * consent: without one, never joined or withdrawn, the answer is 403 and
 * nothing is recorded (GDPR Art. 7(3)). What goes out is the program, the
 * year and goal met, nothing else (decision 2). Step 6 (ADR-087): only for
 * the year the program's rule covers (400) and once it has ended (409); the
 * hub then works out goal met inside the record and records it.
 */
export async function POST(req: NextRequest): Promise<NextResponse> {
  const auth = await requireAuth(["PATIENT", "EDC_ADMIN"]);
  Iif (isAuthError(auth)) return auth;
  const body = (await req.json().catch(() => ({}))) as {
    programmeId?: unknown;
    year?: unknown;
    patientId?: string;
  };
  const parsed = parseTransferRequest(body);
  if (parsed instanceof NextResponse) return parsed;
  const who = await resolveProgrammePatient(
    auth.session.roles,
    body.patientId ?? null,
  );
  if (who instanceof NextResponse) return who;
  return requestProgrammeTransfer(
    who.patientId as string,
    parsed.programmeId,
    parsed.year,
  );
}