import { newId } from '@rareindex/shared'; import { auditLog } from '@rareindex/database'; import { db } from './db.ts'; /** Audit trail for every flag/exclusion/merge — nothing is ever silently deleted (§116). */ export async function audit(entry: { entityType: string; entityId: string; action: string; reason: string; actor?: string; details?: Record }): Promise { await db().insert(auditLog).values({ id: newId('event'), entityType: entry.entityType, entityId: entry.entityId, action: entry.action, reason: entry.reason, actor: entry.actor ?? 'system', details: entry.details ?? {} }); } export async function auditMany(entries: Array<{ entityType: string; entityId: string; action: string; reason: string; actor?: string; details?: Record }>): Promise { if (entries.length === 0) return; for (let i = 0; i < entries.length; i += 500) { await db() .insert(auditLog) .values(entries.slice(i, i + 500).map((e) => ({ id: newId('event'), entityType: e.entityType, entityId: e.entityId, action: e.action, reason: e.reason, actor: e.actor ?? 'system', details: e.details ?? {} }))); } }