Fix jwt-token

This commit is contained in:
2025-09-15 21:26:15 +02:00
parent 2afbc76817
commit aa930270d4
2 changed files with 42 additions and 18 deletions

View File

@@ -716,9 +716,16 @@ class AlertService {
*/ */
async checkAlertRules(detection) { async checkAlertRules(detection) {
try { 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({ const rules = await AlertRule.findAll({
where: { where: {
tenant_id: detection.tenant_id, tenant_id: device.tenant_id,
is_active: true is_active: true
} }
}); });

View File

@@ -265,42 +265,59 @@ describe('AlertService', () => {
describe('logAlert', () => { describe('logAlert', () => {
it('should create alert log entry', async () => { it('should create alert log entry', async () => {
const tenant = await createTestTenant(); const tenant = await createTestTenant();
const user = await createTestUser({ tenant_id: tenant.id });
const device = await createTestDevice({ tenant_id: tenant.id }); const device = await createTestDevice({ tenant_id: tenant.id });
const detection = await createTestDetection({ device_id: device.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', rule_name: 'Test Alert',
threat_level: 'high', min_rssi: -80,
message: 'Test alert message' 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).to.exist;
expect(logEntry.device_id).to.equal(device.id); expect(logEntry.alert_rule_id).to.equal(rule.id);
expect(logEntry.rule_name).to.equal('Test Alert'); expect(logEntry.detection_id).to.equal(detection.id);
expect(logEntry.threat_level).to.equal('high'); expect(logEntry.alert_type).to.equal('sms');
expect(logEntry.status).to.equal('sent');
}); });
it('should include detection and threat data in log', async () => { 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({ const detection = await createTestDetection({
device_id: device.id, device_id: device.id,
drone_type: 2, drone_type: 2,
rssi: -50 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', rule_name: 'Critical Alert',
threat_level: 'critical', min_rssi: -80,
message: 'Critical threat detected' 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.alert_rule_id).to.equal(rule.id);
expect(logEntry.rssi).to.equal(-50); expect(logEntry.detection_id).to.equal(detection.id);
expect(logEntry.drone_id).to.equal(detection.drone_id); expect(logEntry.alert_type).to.equal('sms');
expect(logEntry.status).to.equal('sent');
}); });
}); });