const prisma = require('../config/db'); function clientIp(req) { return req?.ip || req?.socket?.remoteAddress || null; } /** Record an admin action. Never throws — logging must not break the action itself. */ async function audit(req, action, target = null, metadata = null) { try { await prisma.auditLog.create({ data: { actorId: req?.admin?.id || null, action, target, metadata: metadata || undefined, ip: clientIp(req) } }); } catch (e) { console.error('[audit] failed:', e?.message); } } module.exports = { audit, clientIp };