import { db } from "../db/client.js"; import { auditLog, users } from "../db/schema.js"; type CurrentUser = typeof users.$inferSelect; /** Who an automatic, no-one-clicked-anything entry is attributed to. */ export const SYSTEM_ACTOR_LABEL = "system"; /** Records one audit-log entry. Call this from any route that mutates state or takes an action. Leave `actor` out for something the app did by itself. */ export async function recordAudit(params: { actor?: CurrentUser; category: string; action: string; targetType?: string; targetId?: string | number; detail?: unknown; }) { await db.insert(auditLog).values({ actorUserId: params.actor?.id, actorLabel: params.actor ? (params.actor.name ?? params.actor.email ?? params.actor.oidcSub) : SYSTEM_ACTOR_LABEL, category: params.category, action: params.action, targetType: params.targetType, targetId: params.targetId !== undefined ? String(params.targetId) : undefined, detail: params.detail !== undefined ? JSON.stringify(params.detail) : undefined, }); }