119 lines
5.0 KiB
JavaScript
119 lines
5.0 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createAlert = createAlert;
|
|
exports.listAlerts = listAlerts;
|
|
exports.findById = findById;
|
|
exports.updateAlertStatus = updateAlertStatus;
|
|
exports.setAlertEmails = setAlertEmails;
|
|
exports.getAlertEmailsDecrypted = getAlertEmailsDecrypted;
|
|
const connection_1 = require("../db/connection");
|
|
const crypto_1 = require("../utils/crypto");
|
|
const SECURITY_ALERT_EMAILS_KEY = 'security_alert_emails_encrypted';
|
|
async function createAlert(input) {
|
|
const rows = await (0, connection_1.query)(`INSERT INTO security_alerts (user_id, severity, alert_type, title, details_encrypted, metadata)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
RETURNING *`, [
|
|
input.userId ?? null,
|
|
input.severity,
|
|
input.alertType,
|
|
input.title,
|
|
input.details ? (0, crypto_1.encryptSensitive)(input.details) : null,
|
|
input.metadata ? JSON.stringify(input.metadata) : null,
|
|
]);
|
|
return rows[0];
|
|
}
|
|
async function listAlerts(opts) {
|
|
const limit = Math.min(opts.limit ?? 50, 100);
|
|
const offset = Math.max(opts.offset ?? 0, 0);
|
|
const conditions = [];
|
|
const values = [];
|
|
let i = 1;
|
|
if (opts.status) {
|
|
conditions.push(`status = $${i++}`);
|
|
values.push(opts.status);
|
|
}
|
|
if (opts.userId) {
|
|
conditions.push(`user_id = $${i++}`);
|
|
values.push(opts.userId);
|
|
}
|
|
const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
const countRows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count FROM security_alerts ${where}`, values);
|
|
const items = await (0, connection_1.query)(`SELECT * FROM security_alerts ${where} ORDER BY created_at DESC LIMIT $${i++} OFFSET $${i}`, [...values, limit, offset]);
|
|
return { items, total: Number(countRows[0]?.count ?? 0) };
|
|
}
|
|
async function findById(id) {
|
|
const rows = await (0, connection_1.query)('SELECT * FROM security_alerts WHERE id = $1', [id]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function updateAlertStatus(id, status, resolvedBy, resolutionNote) {
|
|
const rows = await (0, connection_1.query)(`UPDATE security_alerts
|
|
SET status = $2::varchar,
|
|
resolved_by = $3,
|
|
resolved_at = CASE
|
|
WHEN $2::varchar IN ('resolved', 'dismissed') THEN NOW()
|
|
ELSE resolved_at
|
|
END,
|
|
resolution_note_encrypted = COALESCE($4, resolution_note_encrypted),
|
|
updated_at = NOW()
|
|
WHERE id = $1
|
|
RETURNING *`, [id, status, resolvedBy, resolutionNote ? (0, crypto_1.encryptSensitive)(resolutionNote) : null]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function setAlertEmails(emails) {
|
|
const normalized = emails.map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
const encrypted = (0, crypto_1.encryptJson)(normalized);
|
|
await (0, connection_1.query)(`INSERT INTO system_settings (key, value, updated_at) VALUES ($1, $2, NOW())
|
|
ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = NOW()`, [SECURITY_ALERT_EMAILS_KEY, encrypted]);
|
|
}
|
|
async function getAlertEmailsDecrypted() {
|
|
const rows = await (0, connection_1.query)('SELECT value FROM system_settings WHERE key = $1', [SECURITY_ALERT_EMAILS_KEY]);
|
|
if (!rows[0]?.value)
|
|
return [];
|
|
const { decryptSensitive } = await Promise.resolve().then(() => __importStar(require('../utils/crypto')));
|
|
const raw = rows[0].value;
|
|
let text = raw;
|
|
if (raw.startsWith('enc:v1:')) {
|
|
text = decryptSensitive(raw);
|
|
}
|
|
try {
|
|
const parsed = JSON.parse(text);
|
|
return Array.isArray(parsed) ? parsed.filter((e) => typeof e === 'string') : [];
|
|
}
|
|
catch {
|
|
return [];
|
|
}
|
|
}
|
|
//# sourceMappingURL=securityAlertRepository.js.map
|