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).
90 lines
2.8 KiB
TypeScript
90 lines
2.8 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 { requireRole } from "@/lib/authz/rbac";
|
|
import { getPool } from "@/lib/db";
|
|
import { writeAudit } from "@/lib/audit";
|
|
import { recordValidationRunSchema } from "@/lib/validation/status.schemas";
|
|
import {
|
|
computeValidationStatus,
|
|
recordValidationRun,
|
|
listValidationRuns,
|
|
} from "@/lib/validation/status";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
// GET /api/validation — this org's validation runs, newest first. Any member
|
|
// (viewer+) may read the compliance history. org-scoped via ctx.org.id.
|
|
export const GET = withOrg(async (req: NextRequest, ctx: Ctx) => {
|
|
try {
|
|
requireRole(ctx, "viewer");
|
|
|
|
const { limit, offset, page } = parsePagination(req);
|
|
const { items, total } = await listValidationRuns(
|
|
getPool(),
|
|
ctx.org.id,
|
|
limit,
|
|
offset
|
|
);
|
|
|
|
return ok(items, { total, page, limit });
|
|
} catch (err: unknown) {
|
|
if (err instanceof Error && "status" in err) {
|
|
return fail(err.message, (err as { status: number }).status);
|
|
}
|
|
return fail("Failed to load validation runs.", 500);
|
|
}
|
|
});
|
|
|
|
// POST /api/validation — compute and record a validation run for the org. The
|
|
// coverage/quality/status are derived deterministically server-side from the
|
|
// reported engines and source reachability; the client cannot set them directly.
|
|
// Recording a compliance artifact is a mutation, so editor+ is required.
|
|
export const POST = withOrg(async (req: NextRequest, ctx: Ctx) => {
|
|
try {
|
|
requireRole(ctx, "editor");
|
|
|
|
const json = await req.json().catch(() => null);
|
|
const parsed = recordValidationRunSchema.safeParse(json);
|
|
if (!parsed.success) {
|
|
return fail(parsed.error.issues[0]?.message ?? "Invalid request body.", 400);
|
|
}
|
|
|
|
const status = computeValidationStatus({
|
|
enginesRun: parsed.data.enginesRun,
|
|
requiredEngines: parsed.data.requiredEngines,
|
|
sourcesReachable: parsed.data.sourcesReachable,
|
|
});
|
|
|
|
const pool = getPool();
|
|
const run = await recordValidationRun(
|
|
pool,
|
|
ctx.org.id,
|
|
parsed.data.subject,
|
|
status,
|
|
parsed.data.enginesRun,
|
|
parsed.data.sourcesReachable
|
|
);
|
|
|
|
await writeAudit(pool, {
|
|
orgId: ctx.org.id,
|
|
userId: ctx.user.id,
|
|
action: "validation_run.recorded",
|
|
entityType: "validation_run",
|
|
entityId: run.id,
|
|
metadata: {
|
|
status: run.status,
|
|
coverage: run.coverage,
|
|
qualityScore: run.qualityScore,
|
|
},
|
|
});
|
|
|
|
return created({ run, status });
|
|
} catch (err: unknown) {
|
|
if (err instanceof Error && "status" in err) {
|
|
return fail(err.message, (err as { status: number }).status);
|
|
}
|
|
return fail("Failed to record validation run.", 500);
|
|
}
|
|
});
|