EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
audit.js
Go to the documentation of this file.
1// utils/audit.js
2const pool = require('../services/db');
3
4/**
5 * Log impersonation events to the impersonation_audit table
6 * @param {object} params
7 * @param {string} params.impersonatorId - UUID of the impersonator
8 * @param {string} params.impersonatorName - Name of the impersonator
9 * @param {string} params.impersonatedTenantId - UUID of the impersonated tenant
10 * @param {string} params.impersonatedTenantName - Name of the impersonated tenant
11 * @param {string} params.action - 'start', 'stop', 'switch'
12 * @param {object} params.req - Express request object
13 */
14async function logImpersonation({
15 impersonatorId,
16 impersonatorName,
17 impersonatedTenantId,
18 impersonatedTenantName,
19 action,
20 req
21}) {
22 try {
23 await pool.query(
24 `INSERT INTO impersonation_audit
25 (impersonator_id, impersonator_name, impersonated_tenant_id, impersonated_tenant_name, action, ip_address, user_agent)
26 VALUES ($1, $2, $3, $4, $5, $6, $7)`,
27 [
28 impersonatorId,
29 impersonatorName,
30 impersonatedTenantId,
31 impersonatedTenantName,
32 action,
33 req.ip,
34 req.get('User-Agent')
35 ]
36 );
37 } catch (err) {
38 console.error('[Audit] Failed to log impersonation:', err);
39 }
40}
41
42module.exports = { logImpersonation };