import { prisma } from "./prisma"; import type { Session } from "next-auth"; interface AuditOptions { action: string; targetId?: string; familyId?: string; detail?: string; } export async function logAudit(session: Session | null, opts: AuditOptions) { if (!session?.user?.id) return; try { await prisma.auditLog.create({ data: { userId: session.user.id, userName: session.user.name ?? "—", action: opts.action, targetId: opts.targetId ?? null, familyId: opts.familyId ?? null, detail: opts.detail ?? null, }, }); } catch { // audit failure must never break the main request } }