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 | 1x 14x 14x 14x 3x 11x 1x 10x 4x 4x 2x 2x 2x 2x 5x 5x 4x 4x 4x 2x 2x 2x 5x 5x 4x 4x 4x 4x 2x 2x 2x 1x 1x | import { getServerSession } from "next-auth/next";
import { NextResponse } from "next/server";
import { runQuery } from "@/lib/neo4j";
import { authOptions } from "@/lib/auth";
export const dynamic = "force-dynamic";
async function requirePatient(): Promise<NextResponse | null> {
const session = await getServerSession(authOptions);
const roles = (session as { roles?: string[] } | null)?.roles ?? [];
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
if (!roles.includes("PATIENT") && !roles.includes("EDC_ADMIN")) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
return null;
}
/**
* GET /api/patient/research?patientId=<id>
*
* EHDS Art. 10 — Research program discovery and consent management.
* Returns available research programs (DataProduct nodes with research purpose)
* and existing patient consents.
*
* POST /api/patient/research
* Body: { patientId, studyId, purpose }
* Creates a PatientConsent node (EHDS Art. 10 — explicit consent).
*/
export async function GET(req: Request) {
const authError = await requirePatient();
if (authError) return authError;
const { searchParams } = new URL(req.url);
const patientId = searchParams.get("patientId");
const [programs, consents] = await Promise.all([
// Available research programs
runQuery<{
studyId: string;
studyName: string;
institution: string;
purpose: string;
description: string;
dataNeeded: string;
status: string;
}>(
`MATCH (dp:DataProduct)
OPTIONAL MATCH (dp)-[:OFFERED_BY|:OFFERED]->(p:Participant)
RETURN coalesce(dp.productId, elementId(dp)) AS studyId,
coalesce(dp.name, dp.title, 'Research Study') AS studyName,
coalesce(p.name, 'Research Institution') AS institution,
coalesce(dp.purpose, 'RESEARCH') AS purpose,
coalesce(dp.description, 'Health research study') AS description,
coalesce(dp.dataNeeded, 'FHIR conditions, medications') AS dataNeeded,
coalesce(dp.status, 'active') AS status
ORDER BY dp.name
LIMIT 10`,
),
// Patient's existing consents
patientId
? runQuery<{
consentId: string;
studyId: string;
grantedAt: string;
revoked: boolean;
purpose: string;
}>(
`MATCH (pc:PatientConsent {patientId: $patientId})
RETURN pc.consentId AS consentId,
pc.studyId AS studyId,
toString(pc.grantedAt) AS grantedAt,
pc.revoked AS revoked,
coalesce(pc.purpose, 'RESEARCH') AS purpose
ORDER BY pc.grantedAt DESC`,
{ patientId },
)
: Promise.resolve([]),
]);
return NextResponse.json({
programs,
consents,
ehdsArticle: "EHDS Art. 10 — Consent for secondary use of health data",
});
}
export async function POST(req: Request) {
const authError = await requirePatient();
if (authError) return authError;
const body = (await req.json()) as {
patientId: string;
studyId: string;
purpose?: string;
};
const { patientId, studyId, purpose = "RESEARCH" } = body;
if (!patientId || !studyId) {
return NextResponse.json(
{ error: "patientId and studyId are required" },
{ status: 400 },
);
}
const result = await runQuery<{ consentId: string }>(
`MATCH (dp:DataProduct)
WHERE coalesce(dp.productId, elementId(dp)) = $studyId
MERGE (pc:PatientConsent {
patientId: $patientId,
studyId: $studyId
})
ON CREATE SET
pc.consentId = randomUUID(),
pc.purpose = $purpose,
pc.grantedAt = datetime(),
pc.revoked = false
WITH pc, dp
MERGE (pc)-[:FOR_STUDY]->(dp)
WITH pc
OPTIONAL MATCH (p:Patient)
WHERE coalesce(p.id, p.resourceId, elementId(p)) = $patientId
FOREACH (_ IN CASE WHEN p IS NOT NULL THEN [1] ELSE [] END |
MERGE (p)-[:HAS_CONSENT]->(pc)
)
RETURN pc.consentId AS consentId`,
{ patientId, studyId, purpose },
);
return NextResponse.json({
consentId: result[0]?.consentId,
patientId,
studyId,
purpose,
grantedAt: new Date().toISOString(),
message:
"EHR donation registered. Your pseudonymised data will be used in this study.",
ehdsArticle: "EHDS Art. 10",
});
}
export async function DELETE(req: Request) {
const authError = await requirePatient();
if (authError) return authError;
const { searchParams } = new URL(req.url);
const consentId = searchParams.get("consentId");
const patientId = searchParams.get("patientId");
if (!consentId || !patientId) {
return NextResponse.json(
{ error: "consentId and patientId are required" },
{ status: 400 },
);
}
const result = await runQuery<{ consentId: string }>(
`MATCH (pc:PatientConsent {consentId: $consentId, patientId: $patientId})
SET pc.revoked = true, pc.revokedAt = datetime()
RETURN pc.consentId AS consentId`,
{ consentId, patientId },
);
if (result.length === 0) {
return NextResponse.json({ error: "Consent not found" }, { status: 404 });
}
return NextResponse.json({
revoked: true,
consentId,
revokedAt: new Date().toISOString(),
gdprArticle: "GDPR Art. 17 — Right to erasure / restriction of processing",
});
}
|