From 71b59e676ccb1dec26d8e45dad979fd89c08140a Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Wed, 24 Sep 2025 04:28:10 +0200 Subject: [PATCH] Fix jwt-token --- server/services/alertService.js | 92 +++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/server/services/alertService.js b/server/services/alertService.js index afa13bf..1e94d14 100644 --- a/server/services/alertService.js +++ b/server/services/alertService.js @@ -523,6 +523,30 @@ class AlertService { } async sendSMSAlert(phoneNumber, message, rule, detection, threatAssessment = null, alertEventId = null) { + // Validate required parameters + if (!rule) { + console.error('❌ Failed to send SMS alert: rule parameter is null'); + // Still try to log the attempt if possible, but handle null rule gracefully + try { + return await AlertLog.create({ + alert_rule_id: null, + detection_id: detection ? detection.id : null, + alert_type: 'sms', + recipient: phoneNumber, + message: message, + status: 'failed', + sent_at: new Date(), + external_id: null, + priority: 'medium', + error_message: 'Alert rule is null - cannot send SMS', + alert_event_id: alertEventId + }); + } catch (logError) { + console.error('❌ Failed to log SMS alert attempt:', logError.message); + return null; + } + } + // Check if Twilio is enabled if (!this.twilioEnabled || !this.twilioClient) { console.log('📱 SMS alert skipped - Twilio not configured'); @@ -531,7 +555,7 @@ class AlertService { return await AlertLog.create({ alert_rule_id: rule.id, - detection_id: detection.id, + detection_id: detection ? detection.id : null, alert_type: 'sms', recipient: phoneNumber, message: message, @@ -569,7 +593,7 @@ class AlertService { return await AlertLog.create({ alert_rule_id: rule.id, - detection_id: detection.id, + detection_id: detection ? detection.id : null, alert_type: 'sms', recipient: phoneNumber, message: message, @@ -597,7 +621,7 @@ class AlertService { return await AlertLog.create({ alert_rule_id: rule.id, - detection_id: detection.id, + detection_id: detection ? detection.id : null, alert_type: 'sms', recipient: phoneNumber, message: message, @@ -612,6 +636,30 @@ class AlertService { } async sendEmailAlert(email, message, rule, detection, threatAssessment = null, alertEventId = null) { + // Validate required parameters + if (!rule) { + console.error('❌ Failed to send email alert: rule parameter is null'); + // Still try to log the attempt if possible, but handle null rule gracefully + try { + return await AlertLog.create({ + alert_rule_id: null, + detection_id: detection ? detection.id : null, + alert_type: 'email', + recipient: email, + message: message, + status: 'failed', + sent_at: new Date(), + external_id: null, + priority: 'medium', + error_message: 'Alert rule is null - cannot send email', + alert_event_id: alertEventId + }); + } catch (logError) { + console.error('❌ Failed to log email alert attempt:', logError.message); + return null; + } + } + try { // Email implementation would go here // For now, just log the alert @@ -624,12 +672,12 @@ class AlertService { timestamp: new Date().toISOString(), simulatedDelivery: true, ruleId: rule.id, - detectionId: detection.id + detectionId: detection ? detection.id : null }; return await AlertLog.create({ alert_rule_id: rule.id, - detection_id: detection.id, + detection_id: detection ? detection.id : null, alert_type: 'email', recipient: email, message: message, @@ -654,7 +702,7 @@ class AlertService { return await AlertLog.create({ alert_rule_id: rule.id, - detection_id: detection.id, + detection_id: detection ? detection.id : null, alert_type: 'email', recipient: email, message: message, @@ -668,10 +716,34 @@ class AlertService { } async sendWebhookAlert(webhookUrl, detection, device, rule, threatAssessment = null, alertEventId = null) { + // Validate required parameters + if (!rule) { + console.error('❌ Failed to send webhook alert: rule parameter is null'); + // Still try to log the attempt if possible, but handle null rule gracefully + try { + return await AlertLog.create({ + alert_rule_id: null, + detection_id: detection ? detection.id : null, + alert_type: 'webhook', + recipient: webhookUrl, + message: 'Webhook alert attempt with null rule', + status: 'failed', + sent_at: new Date(), + external_id: null, + priority: 'medium', + error_message: 'Alert rule is null - cannot send webhook', + alert_event_id: alertEventId + }); + } catch (logError) { + console.error('❌ Failed to log webhook alert attempt:', logError.message); + return null; + } + } + const payload = { event: 'drone_detection', timestamp: new Date().toISOString(), - detection: { + detection: detection ? { id: detection.id, device_id: detection.device_id, drone_id: detection.drone_id, @@ -681,14 +753,14 @@ class AlertService { geo_lat: detection.geo_lat, geo_lon: detection.geo_lon, server_timestamp: detection.server_timestamp - }, - device: { + } : null, + device: device ? { id: device.id, name: device.name, geo_lat: device.geo_lat, geo_lon: device.geo_lon, location_description: device.location_description - }, + } : null, alert_rule: { id: rule.id, name: rule.name,