Builds PaperTrail from the base skeleton into a full evidence-verification platform for the "Built with Claude: Life Sciences" hackathon (Builder Track). New named-user features (Build Beyond the Bench): - Lab Notebook Companion — rough bench notes -> structured, searchable, grounded experiment records (lib/labNotebook, /console/lab-notebook, migration 0059) - Clinical Trial Matcher — de-identified patient notes -> ranked eligible ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning (lib/trialMatcher, /console/trial-matcher, migrations 0060) Platform: - Deterministic verification moat (biostats recompute, exact-span grounding) - 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar, PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier) - Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise governance (RBAC, /api/v1 gateway, metering, audit chain, retention) - 17 OSS projects assimilated into native TypeScript on our stack (backend/engines retained + credited in NOTICE) - Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site - Next.js 16 upgrade, benchmark harness (honest smoke-test caveat) Licensing: Apache-2.0 (LICENSE + NOTICE).
77 lines
2.6 KiB
TypeScript
77 lines
2.6 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { withOrg, parsePagination, type Ctx } from "@/lib/api/handler";
|
|
import { ok, created, fail } from "@/lib/api/response";
|
|
import { getPool } from "@/lib/db";
|
|
import { writeAudit } from "@/lib/audit";
|
|
import { requireRole } from "@/lib/authz/rbac";
|
|
import { createEvidenceReportSchema } from "@/lib/evidenceReports/schemas";
|
|
import {
|
|
createReport,
|
|
listReports,
|
|
} from "@/lib/evidenceReports/repository";
|
|
import type { EvidenceReportRecord } from "@/lib/evidenceReports/types";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
// GET /api/evidence-reports — paginated, org-scoped list of persisted evidence
|
|
// reports, newest first. Any authenticated member of the org may read them.
|
|
export const GET = withOrg(async (req: NextRequest, ctx: Ctx) => {
|
|
try {
|
|
const { limit, offset, page } = parsePagination(req);
|
|
const { items, total } = await listReports(getPool(), {
|
|
orgId: ctx.org.id,
|
|
limit,
|
|
offset,
|
|
});
|
|
return ok<EvidenceReportRecord[]>(items, { total, page, limit });
|
|
} catch {
|
|
return fail("Couldn't load evidence reports. Please try again.", 500);
|
|
}
|
|
});
|
|
|
|
// POST /api/evidence-reports — persist a composite evidence report the caller
|
|
// already computed (from the deterministic engine). We validate the body but do
|
|
// NOT recompute the science here. org_id always comes from ctx — never the client.
|
|
export const POST = withOrg(async (req: NextRequest, ctx: Ctx) => {
|
|
try {
|
|
requireRole(ctx, "editor");
|
|
|
|
const body = await req.json().catch(() => null);
|
|
const parsed = createEvidenceReportSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return fail(parsed.error.issues[0]?.message ?? "Invalid evidence report.", 400);
|
|
}
|
|
|
|
const record = await createReport(getPool(), {
|
|
orgId: ctx.org.id,
|
|
createdBy: ctx.user.id,
|
|
projectId: parsed.data.projectId ?? null,
|
|
claim: parsed.data.claim,
|
|
verdict: parsed.data.verdict ?? null,
|
|
certainty: parsed.data.certainty ?? null,
|
|
pooled: parsed.data.pooled ?? null,
|
|
report: parsed.data.report,
|
|
});
|
|
|
|
await writeAudit(getPool(), {
|
|
orgId: ctx.org.id,
|
|
userId: ctx.user.id,
|
|
action: "evidence_report.create",
|
|
entityType: "evidence_report",
|
|
entityId: record.id,
|
|
metadata: {
|
|
verdict: record.verdict,
|
|
certainty: record.certainty,
|
|
project_id: record.projectId,
|
|
},
|
|
});
|
|
|
|
return created(record);
|
|
} catch (err) {
|
|
if (err instanceof Error && "status" in err) {
|
|
return fail(err.message, (err as { status: number }).status);
|
|
}
|
|
return fail("Couldn't save the evidence report. Please try again.", 500);
|
|
}
|
|
});
|