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

87.87% Statements 29/33
64.7% Branches 22/34
100% Functions 3/3
93.54% Lines 29/31

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                        1x                     2x 2x     2x 2x         2x   2x   2x       2x     2x 1x         1x 1x       2x 2x 2x 2x       2x                                                                       1x           1x                                   1x 1x 1x   1x   1x       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";
import {
  RESPONSE_WEEKS,
  daysLeft,
  slugOf,
  weeksFrom,
  type InformationRequest,
} from "@/lib/supervision";
 
export const dynamic = "force-dynamic";
 
/**
 * Requests for information, Regulation (EU) 2025/327 Art. 57(1)(a)(ii) and
 * Art. 63(1): the access body asks a data user or holder for information
 * on its use of the data; the request and the answer are on record.
 * POST: the body asks (four weeks to answer, as for views under 63(2)).
 * GET: the body sees all; a party sees what was asked of it. Issue #206, M4.
 */
 
export async function POST(req: NextRequest) {
  const auth = await requireAuth(["HDAB_AUTHORITY"]);
  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 partyDid =
    typeof body.partyDid === "string" ? body.partyDid.trim() : "";
  const question =
    typeof body.question === "string" ? body.question.trim() : "";
  const permitId =
    typeof body.permitId === "string" && body.permitId.trim()
      ? body.permitId.trim()
      : null;
  const findingId =
    typeof body.findingId === "string" && body.findingId.trim()
      ? body.findingId.trim()
      : null;
  if (!partyDid || !question) {
    return NextResponse.json(
      { error: "partyDid and a question are required (Art. 63(1))" },
      { status: 400 },
    );
  }
  const { session } = auth;
  const requestedBy = userToParticipantId(
    session.user.email ?? session.user.name ?? session.user.id,
    session.roles,
  );
  const requestedAt = new Date();
  const answerBy = weeksFrom(requestedAt, RESPONSE_WEEKS);
  const stamp = requestedAt.toISOString().slice(0, 10).replace(/-/g, "");
  const requestId = `info-${slugOf(partyDid)}-${stamp}-${Math.random()
    .toString(36)
    .slice(2, 6)}`;
 
  const rows = await runQuery<{ partyName: string | null }>(
    `MATCH (party:Participant)
     WHERE coalesce(party.participantId, party.id) = $partyDid
     MERGE (i:InformationRequest {requestId: $requestId})
     SET i.partyId     = $partyDid,
         i.permitId    = $permitId,
         i.findingId   = $findingId,
         i.question    = $question,
         i.status      = 'OPEN',
         i.requestedAt = datetime($requestedAt),
         i.answerBy    = datetime($answerBy),
         i.requestedBy = $requestedBy,
         i.ehdsArticle = 'Art. 63(1)'
     MERGE (i)-[:ASKED_OF]->(party)
     WITH i, party
     OPTIONAL MATCH (hdab:Participant)
       WHERE coalesce(hdab.participantId, hdab.id) = $requestedBy
     FOREACH (_ IN CASE WHEN hdab IS NOT NULL THEN [1] ELSE [] END |
       MERGE (hdab)-[:ASKED]->(i))
     WITH i, party
     OPTIONAL MATCH (f:NonComplianceFinding {findingId: $findingId})
     FOREACH (_ IN CASE WHEN f IS NOT NULL THEN [1] ELSE [] END |
       MERGE (i)-[:ABOUT]->(f))
     RETURN party.name AS partyName
     LIMIT 1`,
    {
      partyDid,
      requestId,
      permitId,
      findingId,
      question,
      requestedAt: requestedAt.toISOString(),
      answerBy: answerBy.toISOString(),
      requestedBy,
    },
  );
  Iif (rows.length === 0) {
    return NextResponse.json(
      { error: `${partyDid} is not a participant in the graph` },
      { status: 404 },
    );
  }
  return NextResponse.json(
    {
      requestId,
      party: partyDid,
      partyName: rows[0].partyName,
      permitId,
      findingId,
      status: "OPEN",
      requestedAt: requestedAt.toISOString(),
      answerBy: answerBy.toISOString(),
      article:
        "Regulation (EU) 2025/327, Art. 63(1): request for information; four weeks to answer",
    },
    { status: 201 },
  );
}
 
export async function GET() {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
  const { session } = auth;
  const isBody =
    session.roles.includes("HDAB_AUTHORITY") ||
    session.roles.includes("EDC_ADMIN");
  const callerDid = userToParticipantId(
    session.user.email ?? session.user.name ?? session.user.id,
    session.roles,
  );
  const rows = await runQuery<InformationRequest>(
    `MATCH (i:InformationRequest)
     WHERE $all OR i.partyId = $callerDid
     OPTIONAL MATCH (i)-[:ASKED_OF]->(party:Participant)
     OPTIONAL MATCH (hdab:Participant)-[:ASKED]->(i)
     RETURN i.requestId                     AS requestId,
            i.partyId                       AS party,
            party.name                      AS partyName,
            i.permitId                      AS permitId,
            i.findingId                     AS findingId,
            i.question                      AS question,
            toUpper(coalesce(i.status, '')) AS status,
            toString(i.requestedAt)         AS requestedAt,
            toString(i.answerBy)            AS answerBy,
            i.answer                        AS answer,
            toString(i.answeredAt)          AS answeredAt,
            i.requestedBy                   AS requestedBy,
            hdab.name                       AS accessBody
     ORDER BY i.requestedAt DESC`,
    { all: isBody, callerDid },
  );
  const now = Date.now();
  return NextResponse.json({
    requests: rows.map((r) => ({
      ...r,
      daysToAnswer: r.status === "OPEN" ? daysLeft(r.answerBy, now) : null,
    })),
    scope: isBody ? "all" : "own",
    article: "Regulation (EU) 2025/327, Art. 63(1)",
  });
}