All files / src/app/api/compliance/findings route.ts

93.93% Statements 31/33
78.12% Branches 25/32
100% Functions 3/3
96.77% Lines 30/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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                        1x                           4x 4x     3x 3x         3x   4x   4x     4x 4x 1x                 2x 2x       4x 4x 4x 4x       4x                                                                         2x 1x         1x                                       2x 2x 2x   2x   2x       2x                                                       2x 2x 2x                  
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 Finding,
} from "@/lib/supervision";
 
export const dynamic = "force-dynamic";
 
/**
 * Non-compliance findings, Regulation (EU) 2025/327 Art. 63.
 *
 * POST: the access body records that a data user or holder failed to comply
 * (with the permit it concerns, if any), notifies the party and gives it
 * four weeks to state its views (Art. 63(2)). Where a breach of the GDPR is
 * suspected the supervisory authority is informed, and the record says so.
 * GET: the body sees every finding; a party sees the findings against it.
 * Issue #206, M4.
 */
 
export async function POST(req: NextRequest) {
  const auth = await requireAuth(["HDAB_AUTHORITY"]);
  if (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 description =
    typeof body.description === "string" ? body.description.trim() : "";
  const permitId =
    typeof body.permitId === "string" && body.permitId.trim()
      ? body.permitId.trim()
      : null;
  const gdprBreach = body.gdprBreach === true;
  if (!partyDid || !description) {
    return NextResponse.json(
      {
        error:
          "partyDid and a description of the non-compliance are required (Art. 63(1))",
      },
      { status: 400 },
    );
  }
 
  const { session } = auth;
  const foundBy = userToParticipantId(
    session.user.email ?? session.user.name ?? session.user.id,
    session.roles,
  );
  const notifiedAt = new Date();
  const respondBy = weeksFrom(notifiedAt, RESPONSE_WEEKS);
  const stamp = notifiedAt.toISOString().slice(0, 10).replace(/-/g, "");
  const findingId = `finding-${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 (f:NonComplianceFinding {findingId: $findingId})
     SET f.partyId                      = $partyDid,
         f.permitId                     = $permitId,
         f.description                  = $description,
         f.gdprBreach                   = $gdprBreach,
         f.supervisoryAuthorityInformed = $gdprBreach,
         f.status                       = 'OPEN',
         f.notifiedAt                   = datetime($notifiedAt),
         f.respondBy                    = datetime($respondBy),
         f.foundBy                      = $foundBy,
         f.ehdsArticle                  = 'Art. 63'
     MERGE (f)-[:AGAINST]->(party)
     WITH f, party
     OPTIONAL MATCH (hdab:Participant)
       WHERE coalesce(hdab.participantId, hdab.id) = $foundBy
     FOREACH (_ IN CASE WHEN hdab IS NOT NULL THEN [1] ELSE [] END |
       MERGE (hdab)-[:FOUND]->(f))
     WITH f, party
     OPTIONAL MATCH (permit:HDABApproval {approvalId: $permitId})
     FOREACH (_ IN CASE WHEN permit IS NOT NULL THEN [1] ELSE [] END |
       MERGE (f)-[:CONCERNS]->(permit))
     RETURN party.name AS partyName
     LIMIT 1`,
    {
      partyDid,
      findingId,
      permitId,
      description,
      gdprBreach,
      notifiedAt: notifiedAt.toISOString(),
      respondBy: respondBy.toISOString(),
      foundBy,
    },
  );
  if (rows.length === 0) {
    return NextResponse.json(
      { error: `${partyDid} is not a participant in the graph` },
      { status: 404 },
    );
  }
  return NextResponse.json(
    {
      findingId,
      party: partyDid,
      partyName: rows[0].partyName,
      permitId,
      gdprBreach,
      supervisoryAuthorityInformed: gdprBreach,
      status: "OPEN",
      notifiedAt: notifiedAt.toISOString(),
      respondBy: respondBy.toISOString(),
      article: gdprBreach
        ? "Regulation (EU) 2025/327, Art. 63(2): the party states its views within four weeks; the supervisory authority is informed of the suspected GDPR breach"
        : "Regulation (EU) 2025/327, Art. 63(2): the party states its views within four weeks",
    },
    { 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<Finding>(
    `MATCH (f:NonComplianceFinding)
     WHERE $all OR f.partyId = $callerDid
     OPTIONAL MATCH (f)-[:AGAINST]->(party:Participant)
     OPTIONAL MATCH (hdab:Participant)-[:FOUND]->(f)
     RETURN f.findingId                     AS findingId,
            f.partyId                       AS party,
            party.name                      AS partyName,
            party.participantType           AS partyType,
            f.permitId                      AS permitId,
            f.description                   AS description,
            coalesce(f.gdprBreach, false)   AS gdprBreach,
            coalesce(f.supervisoryAuthorityInformed, false) AS supervisoryAuthorityInformed,
            toUpper(coalesce(f.status, '')) AS status,
            toString(f.notifiedAt)          AS notifiedAt,
            toString(f.respondBy)           AS respondBy,
            f.views                         AS views,
            toString(f.respondedAt)         AS respondedAt,
            f.measure                       AS measure,
            f.measureNote                   AS measureNote,
            f.exclusionMonths               AS exclusionMonths,
            f.fineEur                       AS fineEur,
            toString(f.closedAt)            AS closedAt,
            f.foundBy                       AS foundBy,
            hdab.name                       AS accessBody
     ORDER BY f.notifiedAt DESC`,
    { all: isBody, callerDid },
  );
  const now = Date.now();
  return NextResponse.json({
    findings: rows.map((f) => ({
      ...f,
      daysToRespond: f.status === "OPEN" ? daysLeft(f.respondBy, now) : null,
    })),
    scope: isBody ? "all" : "own",
    article:
      "Regulation (EU) 2025/327, Art. 63: findings, four weeks to state views (63(2)), measures (63(3))",
  });
}