2026-01-29 15:10:29 -07:00
|
|
|
// Audit Log Helper
|
|
|
|
|
// Fire-and-forget insert - never blocks the response
|
2026-05-06 11:44:17 -06:00
|
|
|
const pool = require('../db');
|
2026-01-29 15:10:29 -07:00
|
|
|
|
2026-05-06 11:44:17 -06:00
|
|
|
function logAudit({ userId, username, action, entityType, entityId, details, ipAddress }) {
|
2026-01-29 15:10:29 -07:00
|
|
|
const detailsStr = details && typeof details === 'object'
|
|
|
|
|
? JSON.stringify(details)
|
|
|
|
|
: details || null;
|
|
|
|
|
|
2026-05-06 11:44:17 -06:00
|
|
|
pool.query(
|
2026-01-29 15:10:29 -07:00
|
|
|
`INSERT INTO audit_logs (user_id, username, action, entity_type, entity_id, details, ip_address)
|
2026-05-06 11:44:17 -06:00
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
|
|
|
|
[userId || null, username || 'unknown', action, entityType, entityId || null, detailsStr, ipAddress || null]
|
|
|
|
|
).catch((err) => {
|
|
|
|
|
console.error('Audit log error:', err.message);
|
|
|
|
|
});
|
2026-01-29 15:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = logAudit;
|