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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | 3x 7x 7x 6x 6x 5x 5x 5x 1x 1x 1x 5x 13x 13x 13x 13x 13x 13x 5x 5x 18x 5x 1x 1x | import { NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import { runQuery } from "@/lib/neo4j";
import {
PUBLISH_WORKING_DAYS,
addWorkingDays,
applicationClock,
decisionClock,
parseGraphTime,
} from "@/lib/permits";
import { resultsDeadline, type ResultRow } from "@/lib/results";
export const dynamic = "force-dynamic";
/**
* GET /api/permits: the public register of the health data access body.
* Any signed-in participant. Regulation (EU) 2025/327 wants this public;
* Every API route needs a session since #404 (ADR-044), which puts the register behind a login.
*
* Art. 57(1)(j)(ii) every application received, without undue delay
* Art. 57(1)(j)(iii) permits issued and refusals with their justification,
* within 30 working days of the decision
* Art. 57(1)(j)(iv) measures related to non-compliance (Art. 63), so a
* revoked permit stays listed with the reason
* Art. 58(1)(f) who has been granted access to which datasets, and
* the purposes of the permit
*
* Only what those provisions need leaves this route: the applicant
* organisation, purpose, dataset, dates, decision, conditions and the written
* justification. No officer names and no ethics references. Issue #206, M2.
*/
interface RegisterRow {
applicationId: string;
applicant: string | null;
applicantDid: string | null;
applicantCountry: string | null;
accessBody: string | null;
purpose: string | null;
datasetId: string | null;
datasetTitle: string | null;
submittedAt: string | null;
applicationStatus: string;
permitId: string | null;
decision: string;
decidedAt: string | null;
validUntil: string | null;
conditions: unknown;
justification: string | null;
publishBy: string | null;
revokedAt: string | null;
revocationReason: string | null;
statisticalAlternativeOffered: boolean | null;
complete: boolean | null;
completedAt: string | null;
extendedAt: string | null;
incompleteNoticeAt: string | null;
completeBy: string | null;
feeEur: number | null;
resultsCommunicatedAt: string | null;
resultsCount: number | null;
}
export async function GET() {
const auth = await requireAuth();
if (isAuthError(auth)) return auth;
try {
const rows = await runQuery<RegisterRow>(
`MATCH (app:AccessApplication)
OPTIONAL MATCH (p:Participant)-[:SUBMITTED]->(app)
OPTIONAL MATCH (permit:HDABApproval)-[:APPROVES]->(app)
OPTIONAL MATCH (permit)-[:GRANTS_ACCESS_TO]->(granted:HealthDataset)
OPTIONAL MATCH (requested:HealthDataset)
WHERE coalesce(requested.datasetId, requested.id) = app.datasetId
OPTIONAL MATCH (permit)-[:ISSUED_BY]->(hdab:Participant)
OPTIONAL MATCH (rc:ResultCommunication)-[:RESULT_OF]->(permit)
WITH app, p, permit, granted, requested, hdab,
count(rc) AS resultsCount, min(toString(rc.communicatedAt)) AS resultsCommunicatedAt
RETURN app.applicationId AS applicationId,
p.name AS applicant,
coalesce(p.participantId, p.id) AS applicantDid,
p.country AS applicantCountry,
hdab.name AS accessBody,
coalesce(permit.permittedPurpose, app.requestedPurpose) AS purpose,
coalesce(granted.datasetId, granted.id, app.datasetId) AS datasetId,
coalesce(granted.title, requested.title, granted.name, requested.name) AS datasetTitle,
toString(app.submittedAt) AS submittedAt,
toUpper(coalesce(app.status, '')) AS applicationStatus,
permit.approvalId AS permitId,
toUpper(coalesce(permit.status, '')) AS decision,
toString(coalesce(permit.decidedAt, permit.approvedAt)) AS decidedAt,
toString(permit.validUntil) AS validUntil,
permit.conditions AS conditions,
permit.justification AS justification,
toString(permit.publishBy) AS publishBy,
toString(permit.revokedAt) AS revokedAt,
permit.revocationReason AS revocationReason,
permit.statisticalAlternativeOffered AS statisticalAlternativeOffered,
app.complete AS complete,
toString(app.completedAt) AS completedAt,
toString(app.extendedAt) AS extendedAt,
toString(app.incompleteNoticeAt) AS incompleteNoticeAt,
toString(app.completeBy) AS completeBy,
permit.feeEur AS feeEur,
resultsCommunicatedAt AS resultsCommunicatedAt,
resultsCount AS resultsCount
ORDER BY coalesce(permit.decidedAt, permit.approvedAt, app.submittedAt) DESC`,
);
// Health data requests (Art. 69) are published alongside applications.
const requestRows = await runQuery<{
requestId: string;
applicant: string | null;
applicantDid: string | null;
applicantCountry: string | null;
accessBody: string | null;
purpose: string | null;
datasetId: string | null;
datasetTitle: string | null;
submittedAt: string | null;
status: string;
decidedAt: string | null;
justification: string | null;
publishBy: string | null;
statisticalContent: string | null;
decidedUnder: string | null;
feeEur: number | null;
}>(
`MATCH (r:HealthDataRequest)
OPTIONAL MATCH (p:Participant)-[:SUBMITTED]->(r)
OPTIONAL MATCH (ds:HealthDataset)
WHERE coalesce(ds.datasetId, ds.id) = r.datasetId
OPTIONAL MATCH (hdab:Participant)
WHERE r.decidedBy IS NOT NULL AND coalesce(hdab.participantId, hdab.id) = r.decidedBy
RETURN r.requestId AS requestId,
p.name AS applicant,
coalesce(p.participantId, p.id) AS applicantDid,
p.country AS applicantCountry,
hdab.name AS accessBody,
r.purpose AS purpose,
r.datasetId AS datasetId,
coalesce(ds.title, ds.name) AS datasetTitle,
toString(r.submittedAt) AS submittedAt,
toUpper(coalesce(r.status, '')) AS status,
toString(r.decidedAt) AS decidedAt,
r.justification AS justification,
toString(r.publishBy) AS publishBy,
r.statisticalContent AS statisticalContent,
r.decidedUnder AS decidedUnder,
r.feeEur AS feeEur
ORDER BY coalesce(r.decidedAt, r.submittedAt) DESC`,
);
const now = Date.now();
const requestEntries = requestRows.map((r) => {
const undecided = r.status === "PENDING";
const outcome =
r.status === "ANSWERED" || r.status === "APPROVED"
? "request approved"
: r.status === "REJECTED"
? "refused"
: "pending";
return {
kind: "request" as const,
applicationId: r.requestId,
applicant: r.applicant,
applicantDid: r.applicantDid,
applicantCountry: r.applicantCountry,
accessBody: r.accessBody,
purpose: r.purpose,
datasetId: r.datasetId,
datasetTitle: r.datasetTitle,
submittedAt: r.submittedAt,
outcome,
permitId: null,
decidedAt: r.decidedAt,
validUntil: null,
conditions: r.statisticalContent
? [`Statistic: ${r.statisticalContent}`]
: [],
justification: r.justification,
publishBy: r.publishBy ? r.publishBy.slice(0, 10) : null,
revokedAt: null,
revocationReason: null,
statisticalAlternativeOffered: false,
complete: null,
decidedUnder: r.decidedUnder,
feeEur: r.feeEur,
results: null,
...decisionClock(r.submittedAt, undecided, now),
clockState: undecided ? "running" : "decided",
completeBy: null,
daysToComplete: null,
extended: false,
};
});
const entries = rows.map((r) => {
const decided = ["APPROVED", "REJECTED", "REVOKED"].includes(r.decision);
const undecided =
!decided &&
!["APPROVED", "REJECTED", "REVOKED"].includes(r.applicationStatus);
const decidedAt = parseGraphTime(r.decidedAt);
const publishByMs = r.publishBy
? Date.parse(r.publishBy.slice(0, 10))
: decidedAt !== null
? addWorkingDays(new Date(decidedAt), PUBLISH_WORKING_DAYS).getTime()
: null;
const outcome =
r.decision === "APPROVED"
? "permit issued"
: r.decision === "REJECTED"
? "refused"
: r.decision === "REVOKED"
? "permit revoked"
: "pending";
return {
kind: "application" as const,
applicationId: r.applicationId,
applicant: r.applicant,
applicantDid: r.applicantDid,
applicantCountry: r.applicantCountry,
accessBody: r.accessBody,
purpose: r.purpose,
datasetId: r.datasetId,
datasetTitle: r.datasetTitle,
submittedAt: r.submittedAt,
outcome,
permitId: r.permitId,
decidedAt: r.decidedAt,
validUntil: r.validUntil,
conditions: Array.isArray(r.conditions)
? r.conditions.map(String)
: r.conditions
? [String(r.conditions)]
: [],
justification: r.justification,
publishBy:
publishByMs !== null
? new Date(publishByMs).toISOString().slice(0, 10)
: null,
revokedAt: r.revokedAt,
revocationReason: r.revocationReason,
statisticalAlternativeOffered: r.statisticalAlternativeOffered === true,
complete: r.complete,
decidedUnder: r.decision ? "Art. 68" : null,
feeEur: r.feeEur,
// Art. 61(4): results due 18 months after the end of the processing
results:
r.decision === "APPROVED" || r.decision === "REVOKED"
? {
deadline: resultsDeadline(r.validUntil),
communicatedAt: r.resultsCommunicatedAt,
count: Number(r.resultsCount ?? 0),
onTime:
r.resultsCommunicatedAt && resultsDeadline(r.validUntil)
? (parseGraphTime(r.resultsCommunicatedAt) ?? 0) <=
(parseGraphTime(resultsDeadline(r.validUntil)) ?? 0)
: null,
}
: null,
...applicationClock(r, undecided, now),
};
});
// Measures taken in relation to non-compliance (Art. 57(1)(j)(iv), Art.
// 63(3)): every closed finding with a measure, the party, the date and
// the written reason. Open findings are not published; the party is
// still stating its views (Art. 63(2)).
const measures = await runQuery<{
findingId: string;
party: string | null;
partyDid: string | null;
permitId: string | null;
measure: string;
note: string | null;
exclusionMonths: number | null;
fineEur: number | null;
notifiedAt: string | null;
closedAt: string | null;
accessBody: string | null;
}>(
`MATCH (f:NonComplianceFinding)
WHERE toUpper(coalesce(f.status, '')) = 'CLOSED'
AND f.measure IS NOT NULL AND f.measure <> 'NONE'
OPTIONAL MATCH (f)-[:AGAINST]->(party:Participant)
OPTIONAL MATCH (hdab:Participant)-[:FOUND]->(f)
RETURN f.findingId AS findingId,
party.name AS party,
f.partyId AS partyDid,
f.permitId AS permitId,
f.measure AS measure,
f.measureNote AS note,
f.exclusionMonths AS exclusionMonths,
f.fineEur AS fineEur,
toString(f.notifiedAt) AS notifiedAt,
toString(f.closedAt) AS closedAt,
hdab.name AS accessBody
ORDER BY f.closedAt DESC`,
);
// Results communicated by data users (Art. 61(4), Art. 57(1)(j)(v)).
const results = await runQuery<ResultRow>(
`MATCH (rc:ResultCommunication)
OPTIONAL MATCH (rc)-[:RESULT_OF]->(permit:HDABApproval)
OPTIONAL MATCH (permit)-[:GRANTS_ACCESS_TO]->(ds:HealthDataset)
OPTIONAL MATCH (p:Participant)
WHERE coalesce(p.participantId, p.id) = rc.applicantId
RETURN rc.resultId AS resultId, rc.permitId AS permitId,
rc.applicantId AS applicant, p.name AS applicantName,
coalesce(ds.datasetId, ds.id) AS datasetId,
coalesce(ds.title, ds.name) AS datasetTitle,
rc.kind AS kind, rc.title AS title, rc.summary AS summary,
rc.url AS url, toString(rc.communicatedAt) AS communicatedAt,
toString(rc.deadline) AS deadline, rc.onTime AS onTime
ORDER BY rc.communicatedAt DESC`,
);
const all = [...entries, ...requestEntries].sort((a, b) =>
(b.decidedAt ?? b.submittedAt ?? "").localeCompare(
a.decidedAt ?? a.submittedAt ?? "",
),
);
return NextResponse.json({
generatedAt: new Date(now).toISOString(),
entries: all,
measures,
results,
articles: {
requests: "Art. 69, published under Art. 57(1)(j)(ii) and (iii)",
applications: "Art. 57(1)(j)(ii)",
decisions: "Art. 57(1)(j)(iii), within 30 working days",
revocations: "Art. 57(1)(j)(iv), Art. 63(3)",
measures: "Art. 57(1)(j)(iv), Art. 63(3), Art. 64",
results:
"Art. 57(1)(j)(v), Art. 61(4), within 18 months of the end of processing",
fees: "Art. 62",
trustedHolders: "Art. 72",
naturalPersons: "Art. 58(1)(f)",
},
});
} catch (err) {
console.error("Failed to read the permits register:", err);
return NextResponse.json(
{
error: "The register is unavailable",
detail: err instanceof Error ? err.message : String(err),
},
{ status: 502 },
);
}
}
|