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

95.55% Statements 43/45
87.17% Branches 68/78
75% Functions 3/4
95.45% Lines 42/44

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274                          1x                                 1x       8x 8x     7x 7x           7x 8x 8x 2x                 5x 8x 1x                   4x         8x 8x 8x 3x         3x 1x             3x               2x         8x   8x               8x       8x   8x       8x 8x       8x 8x 8x         8x 8x 2x                       2x 1x               3x                                                                                                                                                                   3x 1x         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 {
  CRITERIA,
  PURPOSES,
  PUBLISH_WORKING_DAYS,
  addMonths,
  addWorkingDays,
} from "@/lib/permits";
import { estimateFee, type FeeEstimate } from "@/lib/fees";
 
export const dynamic = "force-dynamic";
 
/**
 * POST /api/compliance/permits: the health data access body decides on an
 * access application (Regulation (EU) 2025/327, Art. 68).
 *
 * Approve, and a data permit is issued: the HDABApproval carries the decision
 * date, the Art. 53(1) purpose, the validity period, the conditions and the
 * Art. 68(1)(a) to (h) criteria as assessed, and GRANTS_ACCESS_TO the dataset.
 * Refuse, and the justification is recorded, because Art. 57(1)(j)(iii)
 * publishes refusals with their justification within 30 working days.
 *
 * One decision per application (permit-<applicationId>); deciding again
 * replaces it, which is what a demo needs when the same application is
 * refused and then approved in front of an audience. Issue #206, M2.
 */
 
const DECISIONS = ["APPROVED", "REJECTED"] as const;
type Decision = (typeof DECISIONS)[number];
 
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 applicationId =
    typeof body.applicationId === "string" ? body.applicationId.trim() : "";
  const decision = String(body.decision ?? "").toUpperCase() as Decision;
  if (!applicationId || !DECISIONS.includes(decision)) {
    return NextResponse.json(
      {
        error: "applicationId and decision (APPROVED or REJECTED) are required",
      },
      { status: 400 },
    );
  }
 
  const justification =
    typeof body.justification === "string" ? body.justification.trim() : "";
  if (decision === "REJECTED" && !justification) {
    return NextResponse.json(
      {
        error:
          "A refusal needs a written justification: Art. 57(1)(j)(iii) publishes it with the decision",
      },
      { status: 400 },
    );
  }
 
  const purpose =
    typeof body.purpose === "string" &&
    (PURPOSES as readonly string[]).includes(body.purpose)
      ? body.purpose
      : null;
 
  const decidedAt = new Date();
  let validUntil: Date | null = null;
  if (decision === "APPROVED") {
    validUntil =
      typeof body.validUntil === "string" &&
      !Number.isNaN(Date.parse(body.validUntil))
        ? new Date(body.validUntil)
        : addMonths(decidedAt, 12);
    if (validUntil.getTime() <= decidedAt.getTime()) {
      return NextResponse.json(
        { error: "validUntil must lie in the future" },
        { status: 400 },
      );
    }
  }
 
  const conditions: string[] = Array.isArray(body.conditions)
    ? body.conditions
        .map(String)
        .map((s) => s.trim())
        .filter(Boolean)
    : typeof body.conditions === "string"
      ? body.conditions
          .split("\n")
          .map((s) => s.trim())
          .filter(Boolean)
      : [];
 
  const criteria =
    body.criteria && typeof body.criteria === "object"
      ? Object.fromEntries(
          CRITERIA.map((k) => [
            k,
            Boolean((body.criteria as Record<string, unknown>)[k]),
          ]),
        )
      : null;
 
  const riskNote =
    typeof body.riskNote === "string" ? body.riskNote.trim() : "";
  // Art. 68(3): with a refusal the body may offer an anonymised statistical
  // answer instead of the data (a health data request under Art. 69).
  const statisticalAlternative =
    decision === "REJECTED" && body.statisticalAlternative === true;
  const datasetId =
    typeof body.datasetId === "string" && body.datasetId.trim()
      ? body.datasetId.trim()
      : null;
 
  const { session } = auth;
  const deciderDid = userToParticipantId(
    session.user.email ?? session.user.name ?? session.user.id,
    session.roles,
  );
  const officer = session.user.name ?? session.user.email ?? session.user.id;
  const permitId = `permit-${applicationId}`;
  const publishBy = addWorkingDays(decidedAt, PUBLISH_WORKING_DAYS);
 
  // Art. 62: the fee on an issued permit, from what the application says
  // about the applicant, the period and the form of the data; reduced for
  // the categories of Art. 62(3).
  let fee: FeeEstimate | null = null;
  if (decision === "APPROVED") {
    const app = await runQuery<{
      applicantCategory: string | null;
      processingPeriodMonths: number | null;
      identifiability: string | null;
    }>(
      `MATCH (app:AccessApplication {applicationId: $applicationId})
       RETURN app.applicantCategory AS applicantCategory,
              app.processingPeriodMonths AS processingPeriodMonths,
              app.identifiability AS identifiability
       LIMIT 1`,
      { applicationId },
    );
    if (app.length > 0) {
      fee = estimateFee({
        applicantCategory: app[0].applicantCategory,
        processingPeriodMonths: app[0].processingPeriodMonths,
        identifiability: app[0].identifiability,
      });
    }
  }
 
  const rows = await runQuery<{
    applicant: string | null;
    applicantName: string | null;
    datasetId: string | null;
    purpose: string | null;
  }>(
    `MATCH (app:AccessApplication {applicationId: $applicationId})
     OPTIONAL MATCH (applicant:Participant)-[:SUBMITTED]->(app)
     MERGE (permit:HDABApproval {approvalId: $permitId})
     SET permit.status           = $decision,
         permit.name             = $name,
         permit.applicationId    = app.applicationId,
         permit.decidedAt        = datetime($decidedAt),
         permit.approvedAt       = CASE WHEN $decision = 'APPROVED' THEN datetime($decidedAt) ELSE null END,
         permit.validFrom        = CASE WHEN $decision = 'APPROVED' THEN datetime($decidedAt) ELSE null END,
         permit.validUntil       = CASE WHEN $validUntil IS NULL THEN null ELSE datetime($validUntil) END,
         permit.permittedPurpose = CASE WHEN $decision = 'APPROVED' THEN coalesce($purpose, app.requestedPurpose) ELSE 'NONE' END,
         permit.conditions       = $conditions,
         permit.justification    = $justification,
         permit.criteria         = $criteria,
         permit.riskNote         = $riskNote,
         permit.statisticalAlternativeOffered = $statisticalAlternative,
         permit.feeEur           = $feeEur,
         permit.feeBodyEur       = $feeBodyEur,
         permit.feeHolderEur     = $feeHolderEur,
         permit.feeCategory      = $feeCategory,
         permit.feeReduction     = $feeReduction,
         permit.hdabOfficer      = $officer,
         permit.decidedBy        = $deciderDid,
         permit.ehdsArticle      = 'Art. 68',
         permit.publishBy        = date($publishBy),
         app.status              = $decision,
         app.decidedAt           = datetime($decidedAt)
     MERGE (permit)-[:APPROVES]->(app)
     WITH permit, app, applicant
     OPTIONAL MATCH (permit)-[old:GRANTS_ACCESS_TO]->()
     WITH permit, app, applicant, collect(old) AS olds
     FOREACH (o IN olds | DELETE o)
     WITH permit, app, applicant
     OPTIONAL MATCH (hdab:Participant)
       WHERE coalesce(hdab.participantId, hdab.id) = $deciderDid
     FOREACH (_ IN CASE WHEN hdab IS NOT NULL THEN [1] ELSE [] END |
       MERGE (permit)-[:ISSUED_BY]->(hdab)
       MERGE (hdab)-[:REVIEWED]->(app))
     WITH permit, app, applicant
     OPTIONAL MATCH (ds:HealthDataset)
       WHERE coalesce(ds.datasetId, ds.id) = coalesce($datasetId, app.datasetId)
     FOREACH (_ IN CASE WHEN ds IS NOT NULL AND $decision = 'APPROVED' THEN [1] ELSE [] END |
       MERGE (permit)-[:GRANTS_ACCESS_TO]->(ds))
     RETURN coalesce(applicant.participantId, applicant.id) AS applicant,
            applicant.name                                  AS applicantName,
            coalesce(ds.datasetId, ds.id, app.datasetId)    AS datasetId,
            permit.permittedPurpose                         AS purpose
     LIMIT 1`,
    {
      applicationId,
      permitId,
      decision,
      name:
        decision === "APPROVED"
          ? `Data permit for ${applicationId}`
          : `Refusal of ${applicationId}`,
      decidedAt: decidedAt.toISOString(),
      validUntil: validUntil ? validUntil.toISOString() : null,
      purpose,
      conditions,
      justification,
      criteria: criteria ? JSON.stringify(criteria) : null,
      riskNote,
      statisticalAlternative,
      feeEur: fee ? fee.totalEur : null,
      feeBodyEur: fee ? fee.bodyEur : null,
      feeHolderEur: fee ? fee.holderEur : null,
      feeCategory: fee ? fee.category : null,
      feeReduction: fee ? fee.reduction : null,
      officer,
      deciderDid,
      datasetId,
      publishBy: publishBy.toISOString().slice(0, 10),
    },
  );
 
  if (rows.length === 0) {
    return NextResponse.json(
      { error: `Application ${applicationId} is not in the graph` },
      { status: 404 },
    );
  }
  const row = rows[0];
  return NextResponse.json({
    permitId,
    applicationId,
    decision,
    applicant: row.applicant,
    applicantName: row.applicantName,
    datasetId: row.datasetId,
    purpose: row.purpose,
    validUntil: validUntil ? validUntil.toISOString() : null,
    decidedAt: decidedAt.toISOString(),
    decidedBy: deciderDid,
    officer,
    conditions,
    justification,
    criteria,
    statisticalAlternative,
    fee,
    publishBy: publishBy.toISOString().slice(0, 10),
    article:
      decision === "APPROVED"
        ? "Regulation (EU) 2025/327, Art. 68(3): data permit issued"
        : statisticalAlternative
          ? "Regulation (EU) 2025/327, Art. 68(3): application refused; an anonymised statistical answer is offered instead (Art. 69)"
          : "Regulation (EU) 2025/327, Art. 68(3): application refused",
  });
}