const prisma = require('../config/db'); const { describeUserAgent } = require('./userAgent'); // Fire-and-forget by design — a logging failure must never break login/password-change, // so this swallows its own errors rather than propagating them to the caller (same // posture as the existing email/WhatsApp notification sends elsewhere in this codebase). async function logSecurityEvent({ userId, type, ip, userAgent }) { try { await prisma.securityEvent.create({ data: { userId: userId || null, type, ip: ip || null, device: describeUserAgent(userAgent), }, }); } catch (e) { console.error('Failed to log security event:', e?.message); } } // Recent activity for a user's Profile & Security page — no raw IP in the response, // just what the mockup shows (what happened, on what device, when). async function getRecentSecurityEvents(userId, limit = 10) { const rows = await prisma.securityEvent.findMany({ where: { userId }, orderBy: { createdAt: 'desc' }, take: limit, select: { id: true, type: true, device: true, createdAt: true }, }); return rows; } module.exports = { logSecurityEvent, getRecentSecurityEvents };