All files / src/app/api/compliance/information-requests/answer route.ts

80.95% Statements 17/21
63.15% Branches 12/19
100% Functions 1/1
85% Lines 17/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          1x               2x 2x     2x 2x         2x 2x 2x           2x 2x       2x           2x           2x 1x         1x 1x             1x              
import { NextRequest, NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import { runQuery } from "@/lib/neo4j";
import { userToParticipantId } from "@/lib/odrl-engine";
 
export const dynamic = "force-dynamic";
 
/**
 * POST /api/compliance/information-requests/answer: the party answers the
 * access body's request for information (Regulation (EU) 2025/327, Art.
 * 63(1)). Only that party; the answer is on record. Issue #206, M4.
 */
export async function POST(req: NextRequest) {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  let body: Record<string, unknown>;
  try {
    body = (await req.json()) as Record<string, unknown>;
  } catch {
    return NextResponse.json({ error: "Body must be JSON" }, { status: 400 });
  }
  const requestId =
    typeof body.requestId === "string" ? body.requestId.trim() : "";
  const answer = typeof body.answer === "string" ? body.answer.trim() : "";
  Iif (!requestId || !answer) {
    return NextResponse.json(
      { error: "requestId and an answer are required" },
      { status: 400 },
    );
  }
  const { session } = auth;
  const callerDid = userToParticipantId(
    session.user.email ?? session.user.name ?? session.user.id,
    session.roles,
  );
  const found = await runQuery<{ partyId: string | null; status: string }>(
    `MATCH (i:InformationRequest {requestId: $requestId})
     RETURN i.partyId AS partyId, toUpper(coalesce(i.status, '')) AS status
     LIMIT 1`,
    { requestId },
  );
  Iif (found.length === 0) {
    return NextResponse.json(
      { error: `Information request ${requestId} is not in the graph` },
      { status: 404 },
    );
  }
  if (found[0].partyId !== callerDid && !session.roles.includes("EDC_ADMIN")) {
    return NextResponse.json(
      { error: "Only the party asked answers" },
      { status: 403 },
    );
  }
  const answeredAt = new Date();
  await runQuery(
    `MATCH (i:InformationRequest {requestId: $requestId})
     SET i.answer     = $answer,
         i.answeredAt = datetime($answeredAt),
         i.status     = 'ANSWERED'`,
    { requestId, answer, answeredAt: answeredAt.toISOString() },
  );
  return NextResponse.json({
    requestId,
    status: "ANSWERED",
    answeredAt: answeredAt.toISOString(),
    article: "Regulation (EU) 2025/327, Art. 63(1): answer on record",
  });
}