/** * The admin audit log (who changed what, WEBSITE-1.0-PLAN ยง4). Logging never breaks the * action it records. */ import type { Request } from 'express'; import type { Ctx } from '../context.js'; export function clientIp(req: Request | undefined): string | null { return req ? (req.ip ?? req.socket?.remoteAddress ?? null) : null; } export async function audit(ctx: Ctx, entry: { actorId: string | null; actorRole: string; action: string; targetType: string; targetId?: string | null; metadata?: object; ip?: string | null }) { try { await ctx.db.auditLog.create({ data: { ...entry, targetId: entry.targetId ?? null, metadata: (entry.metadata ?? undefined) as never, ip: entry.ip ?? null } }); } catch (e) { console.error('[audit]', (e as Error).message); } }