const prisma = require('../config/db'); const { getClientIp } = require('./requestUtils'); // What each DataAccessLog action means, for the admin page. const ACCESS_ACTIONS = { report_pdf: 'Report downloaded (PDF)', report_excel: 'Report downloaded (Excel)', report_email: 'Report emailed', report_whatsapp: 'Report sent by WhatsApp', attendance_export: 'Attendance register exported', certificates_download: 'Certificates downloaded', group_lists: 'Room/group lists opened', form_responses: 'Attendee form answers viewed', consent_list: 'Parental consent list viewed', medical_list: 'Medical list viewed', backup_download: 'Database backup downloaded', impersonation: 'Viewed the site as another user', }; /** * POPIA access log: someone exported or viewed personal information in bulk. Fire-and- * forget like logAdminAction — a logging failure never blocks the export itself. */ async function logDataAccess(req, { action, what, eventId = null, records = null }) { try { await prisma.dataAccessLog.create({ data: { actorId: req.user?.id || null, actorRole: req.user?.role || 'unknown', action, what: String(what || ACCESS_ACTIONS[action] || action).slice(0, 500), eventId: eventId || null, records: Number.isFinite(records) ? records : null, ip: getClientIp(req), }, }); } catch (e) { console.error('Failed to log data access:', e?.message); } } /** Rows in a report payload (main table or layered sections, plus extra tables). */ function reportRowCount(payload) { const p = payload || {}; let n = Array.isArray(p.table?.rows) ? p.table.rows.length : 0; if (Array.isArray(p.layered?.sections)) n += p.layered.sections.reduce((s, sec) => s + (Array.isArray(sec.items) ? sec.items.length : 0), 0); if (Array.isArray(p.extraTables)) n += p.extraTables.reduce((s, t) => s + (Array.isArray(t.rows) ? t.rows.length : 0), 0); return n; } module.exports = { ACCESS_ACTIONS, logDataAccess, reportRowCount };