import { auditEvents, type Db, securityEvents, type Tx } from "@autoscholar/db"; import { redact } from "@autoscholar/observability"; export type Actor = { type: "admin" | "worker" | "system"; id?: string | null; ip?: string | null }; export async function audit( db: Db | Tx, actor: Actor, action: string, target?: { type: string; id: string }, detail: Record = {}, ): Promise { await db.insert(auditEvents).values({ actorType: actor.type, actorId: actor.id ?? null, action, targetType: target?.type ?? null, targetId: target?.id ?? null, detail: redact(detail), ip: actor.ip ?? null, }); } export async function securityEvent( db: Db | Tx, kind: string, detail: Record = {}, userId?: string | null, ip?: string | null, ): Promise { await db .insert(securityEvents) .values({ kind, detail: redact(detail), userId: userId ?? null, ip: ip ?? null }); }