From 8c8556314fe274dca067ea5d1e2fffea168dcbee Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Mon, 22 Sep 2025 08:22:17 +0200 Subject: [PATCH] Fix jwt-token --- server/services/alertService.js | 35 +++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/server/services/alertService.js b/server/services/alertService.js index ea87fec..095ff66 100644 --- a/server/services/alertService.js +++ b/server/services/alertService.js @@ -357,7 +357,11 @@ class AlertService { // PRIORITY 9: Cooldown period check if (rule.cooldown_period > 0) { const cooldownStart = new Date(Date.now() - rule.cooldown_period * 1000); - const recentAlert = await AlertLog.findOne({ + + console.log(`🔍 Checking cooldown for rule "${rule.name}" (${rule.cooldown_period}s) - looking for alerts after ${cooldownStart.toISOString()}`); + + // First, get all recent alert logs for this rule that were successfully sent + const recentAlerts = await AlertLog.findAll({ where: { alert_rule_id: rule.id, status: 'sent', @@ -368,18 +372,33 @@ class AlertService { include: [{ model: DroneDetection, as: 'detection', - where: { - device_id: detection.device_id - // Removed drone_id check - cooldown applies per device, not per device+drone combination - // This prevents alert spam when multiple drones are detected by the same device - }, + attributes: ['device_id'], required: false // Allow alerts without detection_id (like device offline alerts) }] }); - if (recentAlert) { - console.log(`❌ Rule "${rule.name}": Still in cooldown period (last alert: ${recentAlert.sent_at})`); + console.log(`🔍 Found ${recentAlerts.length} recent alerts for rule "${rule.name}"`); + + // Check if any recent alert was for the same device + const deviceAlert = recentAlerts.find(alert => { + console.log(`🔍 Checking alert: detection device_id=${alert.detection?.device_id}, alert device_id=${alert.device_id}, target device_id=${detection.device_id}, sent_at=${alert.sent_at}`); + + // For alerts with detection, check device_id match + if (alert.detection && alert.detection.device_id === detection.device_id) { + return true; + } + // For alerts without detection (like device offline), check device_id field directly + if (!alert.detection && alert.device_id === detection.device_id) { + return true; + } return false; + }); + + if (deviceAlert) { + console.log(`❌ Rule "${rule.name}": Still in cooldown period for device ${detection.device_id} (last alert: ${deviceAlert.sent_at})`); + return false; + } else { + console.log(`✅ Rule "${rule.name}": Cooldown period expired or no recent alerts for device ${detection.device_id}`); } }