papertrail/lib/audit.ts
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

34 lines
986 B
TypeScript

import type { Pool } from "pg";
// Append-only audit trail. Modules call writeAudit after a meaningful mutation
// so admins can review who did what. Failures here must never break the caller,
// so writeAudit swallows its own errors (logging via the pool is best-effort).
export interface AuditEntry {
orgId: string;
userId: string | null;
action: string;
entityType: string;
entityId?: string | null;
metadata?: Record<string, unknown>;
}
export async function writeAudit(pool: Pool, entry: AuditEntry): Promise<void> {
try {
await pool.query(
`insert into audit_log (org_id, user_id, action, entity_type, entity_id, metadata)
values ($1, $2, $3, $4, $5, $6)`,
[
entry.orgId,
entry.userId,
entry.action,
entry.entityType,
entry.entityId ?? null,
JSON.stringify(entry.metadata ?? {}),
]
);
} catch {
// Audit logging is best-effort and must not fail the originating request.
}
}