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

82.6% Statements 19/23
66.66% Branches 14/21
100% Functions 1/1
86.36% Lines 19/22

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          1x               3x 3x     3x 3x         3x 3x 3x           3x 3x       3x           3x           3x 1x         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/findings/respond: the party a finding is against
 * states its views (Regulation (EU) 2025/327, Art. 63(2)). Only that party;
 * only while the finding is open. 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 findingId =
    typeof body.findingId === "string" ? body.findingId.trim() : "";
  const views = typeof body.views === "string" ? body.views.trim() : "";
  Iif (!findingId || !views) {
    return NextResponse.json(
      { error: "findingId and the party's views are required (Art. 63(2))" },
      { 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 (f:NonComplianceFinding {findingId: $findingId})
     RETURN f.partyId AS partyId, toUpper(coalesce(f.status, '')) AS status
     LIMIT 1`,
    { findingId },
  );
  Iif (found.length === 0) {
    return NextResponse.json(
      { error: `Finding ${findingId} is not in the graph` },
      { status: 404 },
    );
  }
  if (found[0].partyId !== callerDid && !session.roles.includes("EDC_ADMIN")) {
    return NextResponse.json(
      { error: "Only the party the finding is against states its views" },
      { status: 403 },
    );
  }
  if (found[0].status === "CLOSED") {
    return NextResponse.json(
      { error: "The finding is closed" },
      { status: 409 },
    );
  }
  const respondedAt = new Date();
  await runQuery(
    `MATCH (f:NonComplianceFinding {findingId: $findingId})
     SET f.views       = $views,
         f.respondedAt = datetime($respondedAt),
         f.status      = 'VIEWS_RECEIVED'`,
    { findingId, views, respondedAt: respondedAt.toISOString() },
  );
  return NextResponse.json({
    findingId,
    status: "VIEWS_RECEIVED",
    respondedAt: respondedAt.toISOString(),
    article:
      "Regulation (EU) 2025/327, Art. 63(2): the party's views are on record",
  });
}