"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ENGINE_AUDIT_ACTIONS = void 0; exports.create = create; exports.deleteEngineAuditLogsOlderThan = deleteEngineAuditLogsOlderThan; exports.listAuditLogs = listAuditLogs; exports.exportAuditLogsCsv = exportAuditLogsCsv; const connection_1 = require("../db/connection"); /** * Repository for persisting audit log entries to the database. * Records user actions and system events for security and compliance tracking. */ /** * Creates a new audit log entry. * @param data - Audit log fields to persist. * @param data.userId - Optional ID of the user who performed the action. * @param data.action - Action identifier (e.g. login, withdrawal). * @param data.resourceType - Optional type of resource affected. * @param data.resourceId - Optional ID of the resource affected. * @param data.ipAddress - Optional client IP address. * @param data.userAgent - Optional client user-agent string. * @param data.metadata - Optional additional context as key-value pairs. * @returns Resolves when the row has been inserted. */ async function create(data) { await (0, connection_1.query)(`INSERT INTO audit_logs (user_id, action, resource_type, resource_id, ip_address, user_agent, metadata) VALUES ($1, $2, $3, $4, $5, $6, $7)`, [ data.userId ?? null, data.action, data.resourceType ?? null, data.resourceId ?? null, data.ipAddress ?? null, data.userAgent ?? null, data.metadata ? JSON.stringify(data.metadata) : null, ]); } exports.ENGINE_AUDIT_ACTIONS = ['prediction_round_settled']; async function deleteEngineAuditLogsOlderThan(cutoff, batchSize = 5000) { const result = await (0, connection_1.query)(`WITH deleted AS ( DELETE FROM audit_logs WHERE ctid IN ( SELECT ctid FROM audit_logs WHERE action = ANY($1::text[]) AND created_at < $2 LIMIT $3 ) RETURNING 1 ) SELECT COUNT(*)::text AS count FROM deleted`, [exports.ENGINE_AUDIT_ACTIONS, cutoff, Math.min(Math.max(batchSize, 100), 20000)]); return parseInt(result[0]?.count ?? '0', 10); } async function listAuditLogs(filters) { const conditions = []; const params = []; let i = 1; if (filters.action) { conditions.push(`action = $${i++}`); params.push(filters.action); } else if (filters.actions?.length) { conditions.push(`action = ANY($${i++})`); params.push(filters.actions); } else if (filters.excludeActions?.length) { conditions.push(`NOT (action = ANY($${i++}))`); params.push(filters.excludeActions); } if (filters.userId) { conditions.push(`user_id = $${i++}`); params.push(filters.userId); } if (filters.from) { conditions.push(`created_at >= $${i++}`); params.push(filters.from); } if (filters.to) { conditions.push(`created_at <= $${i++}`); params.push(filters.to); } const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : ''; const limit = Math.min(Math.max(filters.limit ?? 50, 1), 200); const offset = Math.max(filters.offset ?? 0, 0); const countRows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count FROM audit_logs ${where}`, params); const total = parseInt(countRows[0]?.count ?? '0', 10); const items = await (0, connection_1.query)(`SELECT id, user_id, action, resource_type, resource_id, ip_address, user_agent, metadata, created_at FROM audit_logs ${where} ORDER BY created_at DESC LIMIT $${i++} OFFSET $${i}`, [...params, limit, offset]); return { items, total }; } async function exportAuditLogsCsv(filters) { const { items } = await listAuditLogs({ ...filters, limit: filters.limit ?? 5000, offset: 0 }); const header = 'id,user_id,action,resource_type,resource_id,ip_address,created_at'; const lines = items.map((r) => { const esc = (v) => { if (v == null) return ''; const s = String(v).replace(/"/g, '""'); return `"${s}"`; }; return [ esc(r.id), esc(r.user_id), esc(r.action), esc(r.resource_type), esc(r.resource_id), esc(r.ip_address), esc(new Date(r.created_at).toISOString()), ].join(','); }); return [header, ...lines].join('\n'); } //# sourceMappingURL=auditLogRepository.js.map