"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listUnprocessedEvents = listUnprocessedEvents; exports.markEventProcessed = markEventProcessed; exports.markEventIgnored = markEventIgnored; exports.listRecentEvents = listRecentEvents; exports.listEventsForExport = listEventsForExport; const connection_1 = require("../db/connection"); async function listUnprocessedEvents(limit = 50) { return (0, connection_1.query)(`SELECT id, table_name, operation, row_id, old_data, new_data, changed_columns, source_hint, severity, processed_at, security_alert_id, created_at FROM db_tamper_events WHERE processed_at IS NULL ORDER BY created_at ASC LIMIT $1`, [limit]); } async function markEventProcessed(eventId, securityAlertId) { await (0, connection_1.query)(`UPDATE db_tamper_events SET processed_at = NOW(), security_alert_id = $2 WHERE id = $1`, [eventId, securityAlertId]); } /** Marks spurious tamper events as processed without raising a security alert. */ async function markEventIgnored(eventId) { await markEventProcessed(eventId, null); } async function listRecentEvents(opts) { const limit = opts.limit ?? 50; const offset = opts.offset ?? 0; const params = [limit, offset]; let where = ''; if (opts.tableName) { where = 'WHERE table_name = $3'; params.push(opts.tableName); } const items = await (0, connection_1.query)(`SELECT id, table_name, operation, row_id, old_data, new_data, changed_columns, source_hint, severity, processed_at, security_alert_id, created_at FROM db_tamper_events ${where} ORDER BY created_at DESC LIMIT $1 OFFSET $2`, params); const countRows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count FROM db_tamper_events ${where}`, opts.tableName ? [opts.tableName] : []); return { items, total: parseInt(countRows[0]?.count ?? '0', 10) }; } async function listEventsForExport(opts) { const limit = Math.min(Math.max(opts?.limit ?? 10000, 1), 50000); const params = []; const conditions = []; if (opts?.tableName) { params.push(opts.tableName); conditions.push(`table_name = $${params.length}`); } if (opts?.from) { params.push(opts.from); conditions.push(`created_at >= $${params.length}`); } if (opts?.to) { params.push(opts.to); conditions.push(`created_at <= $${params.length}`); } params.push(limit); const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : ''; return (0, connection_1.query)(`SELECT id, table_name, operation, row_id, old_data, new_data, changed_columns, source_hint, severity, processed_at, security_alert_id, created_at FROM db_tamper_events ${where} ORDER BY created_at DESC LIMIT $${params.length}`, params); } //# sourceMappingURL=dbTamperRepository.js.map