Added Multi-Company Support
This commit is contained in:
@@ -78,20 +78,22 @@ function scanAlerts(userId) {
|
||||
|
||||
for (const c of cands) {
|
||||
seen.add(`${c.reference_type}:${c.reference_id}:${c.type}`);
|
||||
// Every candidate derives from an asset; the alert inherits that asset's company for filtering.
|
||||
const entityId = c.asset_id ? (one('SELECT entity_id FROM assets WHERE id = ?', [c.asset_id])?.entity_id ?? null) : null;
|
||||
const existing = one('SELECT * FROM alerts WHERE reference_type = ? AND reference_id = ? AND type = ?', [c.reference_type, c.reference_id, c.type]);
|
||||
if (existing) {
|
||||
if (existing.status === 'dismissed') continue;
|
||||
run(
|
||||
`UPDATE alerts SET asset_id = ?, severity = ?, title = ?, message = ?, due_date = ?,
|
||||
`UPDATE alerts SET asset_id = ?, entity_id = ?, severity = ?, title = ?, message = ?, due_date = ?,
|
||||
status = CASE WHEN status = 'resolved' THEN 'open' ELSE status END, updated_at = ?
|
||||
WHERE id = ?`,
|
||||
[c.asset_id, c.severity, c.title, c.message, c.due_date, ts, existing.id]
|
||||
[c.asset_id, entityId, c.severity, c.title, c.message, c.due_date, ts, existing.id]
|
||||
);
|
||||
} else {
|
||||
const result = run(
|
||||
`INSERT INTO alerts (type, reference_type, reference_id, asset_id, severity, title, message, due_date, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'open', ?, ?)`,
|
||||
[c.type, c.reference_type, c.reference_id, c.asset_id, c.severity, c.title, c.message, c.due_date, ts, ts]
|
||||
`INSERT INTO alerts (type, reference_type, reference_id, asset_id, entity_id, severity, title, message, due_date, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'open', ?, ?)`,
|
||||
[c.type, c.reference_type, c.reference_id, c.asset_id, entityId, c.severity, c.title, c.message, c.due_date, ts, ts]
|
||||
);
|
||||
created.push(one('SELECT * FROM alerts WHERE id = ?', [result.lastInsertRowid]));
|
||||
}
|
||||
@@ -113,14 +115,17 @@ function listAlerts(query = {}) {
|
||||
FROM alerts al
|
||||
LEFT JOIN assets a ON a.id = al.asset_id
|
||||
LEFT JOIN users u ON u.id = al.acknowledged_by
|
||||
WHERE (? IS NULL OR al.status = ?) AND (? IS NULL OR al.type = ?)
|
||||
WHERE (? IS NULL OR al.status = ?) AND (? IS NULL OR al.type = ?) AND (? IS NULL OR al.entity_id = ?)
|
||||
ORDER BY CASE al.severity WHEN 'critical' THEN 0 WHEN 'warning' THEN 1 ELSE 2 END, al.due_date IS NULL, al.due_date`,
|
||||
[query.status || null, query.status || null, query.type || null, query.type || null]
|
||||
[query.status || null, query.status || null, query.type || null, query.type || null, query.entity_id || null, query.entity_id || null]
|
||||
);
|
||||
}
|
||||
|
||||
function summary() {
|
||||
const rows = all("SELECT status, severity, COUNT(*) AS count FROM alerts GROUP BY status, severity");
|
||||
function summary(companyId) {
|
||||
const rows = all(
|
||||
"SELECT status, severity, COUNT(*) AS count FROM alerts WHERE (? IS NULL OR entity_id = ?) GROUP BY status, severity",
|
||||
[companyId || null, companyId || null]
|
||||
);
|
||||
const open = rows.filter((r) => r.status === 'open');
|
||||
return {
|
||||
open: open.reduce((s, r) => s + r.count, 0),
|
||||
|
||||
Reference in New Issue
Block a user