From aa930270d4f7648ff0720d0bb1b6fd8238f4a5c3 Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Mon, 15 Sep 2025 21:26:15 +0200 Subject: [PATCH] Fix jwt-token --- server/services/alertService.js | 9 +++- server/tests/services/alertService.test.js | 51 ++++++++++++++-------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/server/services/alertService.js b/server/services/alertService.js index 3f30ee3..6e43a6b 100644 --- a/server/services/alertService.js +++ b/server/services/alertService.js @@ -716,9 +716,16 @@ class AlertService { */ async checkAlertRules(detection) { try { + // Get the device to determine tenant context + const device = await Device.findByPk(detection.device_id); + if (!device) { + console.log(`Device ${detection.device_id} not found for detection`); + return []; + } + const rules = await AlertRule.findAll({ where: { - tenant_id: detection.tenant_id, + tenant_id: device.tenant_id, is_active: true } }); diff --git a/server/tests/services/alertService.test.js b/server/tests/services/alertService.test.js index 58b34fa..da7f162 100644 --- a/server/tests/services/alertService.test.js +++ b/server/tests/services/alertService.test.js @@ -265,42 +265,59 @@ describe('AlertService', () => { describe('logAlert', () => { it('should create alert log entry', async () => { const tenant = await createTestTenant(); + const user = await createTestUser({ tenant_id: tenant.id }); const device = await createTestDevice({ tenant_id: tenant.id }); const detection = await createTestDetection({ device_id: device.id }); - const alertData = { + // Create a test alert rule + const rule = await models.AlertRule.create({ + user_id: user.id, + tenant_id: tenant.id, rule_name: 'Test Alert', - threat_level: 'high', - message: 'Test alert message' - }; + min_rssi: -80, + drone_type: 2, + alert_channels: ['sms'], + sms_phone_number: '+1234567890', + is_active: true + }); - const logEntry = await alertService.logAlert(detection, alertData); + const logEntry = await alertService.logAlert(rule, detection, 'sms', '+1234567890'); expect(logEntry).to.exist; - expect(logEntry.device_id).to.equal(device.id); - expect(logEntry.rule_name).to.equal('Test Alert'); - expect(logEntry.threat_level).to.equal('high'); + expect(logEntry.alert_rule_id).to.equal(rule.id); + expect(logEntry.detection_id).to.equal(detection.id); + expect(logEntry.alert_type).to.equal('sms'); + expect(logEntry.status).to.equal('sent'); }); it('should include detection and threat data in log', async () => { - const device = await createTestDevice(); + const tenant = await createTestTenant(); + const user = await createTestUser({ tenant_id: tenant.id }); + const device = await createTestDevice({ tenant_id: tenant.id }); const detection = await createTestDetection({ device_id: device.id, drone_type: 2, rssi: -50 }); - const alertData = { + // Create a test alert rule + const rule = await models.AlertRule.create({ + user_id: user.id, + tenant_id: tenant.id, rule_name: 'Critical Alert', - threat_level: 'critical', - message: 'Critical threat detected' - }; + min_rssi: -80, + drone_type: 2, + alert_channels: ['sms'], + sms_phone_number: '+1234567890', + is_active: true + }); - const logEntry = await alertService.logAlert(detection, alertData); + const logEntry = await alertService.logAlert(rule, detection, 'sms', '+1234567890'); - expect(logEntry.drone_type).to.equal(2); - expect(logEntry.rssi).to.equal(-50); - expect(logEntry.drone_id).to.equal(detection.drone_id); + expect(logEntry.alert_rule_id).to.equal(rule.id); + expect(logEntry.detection_id).to.equal(detection.id); + expect(logEntry.alert_type).to.equal('sms'); + expect(logEntry.status).to.equal('sent'); }); });