From 40c20c87544a19ed4f6da7c884e201ed8e87db53 Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Wed, 24 Sep 2025 04:32:37 +0200 Subject: [PATCH] Fix jwt-token --- server/services/alertService.js | 2 +- server/services/deviceHealthService.js | 84 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/server/services/alertService.js b/server/services/alertService.js index 1e94d14..cfb612b 100644 --- a/server/services/alertService.js +++ b/server/services/alertService.js @@ -826,7 +826,7 @@ class AlertService { return await AlertLog.create({ alert_rule_id: rule.id, - detection_id: detection.id, + detection_id: detection ? detection.id : null, alert_type: 'webhook', recipient: webhookUrl, message: JSON.stringify(payload), diff --git a/server/services/deviceHealthService.js b/server/services/deviceHealthService.js index 2f4906b..ca7ea7f 100644 --- a/server/services/deviceHealthService.js +++ b/server/services/deviceHealthService.js @@ -246,11 +246,53 @@ class DeviceHealthService { } } catch (channelError) { console.error(`❌ Failed to send ${channel} alert for offline device ${device.id}:`, channelError); + + // Log the failed alert attempt to alert logs for audit purposes + try { + const AlertLog = require('../models/AlertLog'); + await AlertLog.create({ + alert_rule_id: rule ? rule.id : null, + detection_id: null, + alert_type: channel, + recipient: channel === 'sms' ? rule?.sms_phone_number : + channel === 'email' ? (rule?.email || rule?.user?.email) : + channel === 'webhook' ? rule?.webhook_url : 'unknown', + message: `Device offline alert failed for ${device.name}`, + status: 'failed', + sent_at: new Date(), + external_id: null, + priority: rule?.priority || 'medium', + error_message: `Device health service error: ${channelError.message}`, + alert_event_id: null + }); + } catch (logError) { + console.error(`❌ Failed to log alert failure:`, logError.message); + } } } } catch (error) { console.error(`❌ Error triggering offline alert for device ${device.id}:`, error); + + // Log the general failure to alert logs for audit purposes + try { + const AlertLog = require('../models/AlertLog'); + await AlertLog.create({ + alert_rule_id: rule ? rule.id : null, + detection_id: null, + alert_type: 'system', + recipient: 'system', + message: `Failed to process device offline alert for ${device.name}`, + status: 'failed', + sent_at: new Date(), + external_id: null, + priority: 'high', + error_message: `Device health service error: ${error.message}`, + alert_event_id: null + }); + } catch (logError) { + console.error(`❌ Failed to log system alert failure:`, logError.message); + } } } @@ -306,11 +348,53 @@ class DeviceHealthService { } } catch (channelError) { console.error(`❌ Failed to send ${channel} recovery alert for device ${device.id}:`, channelError); + + // Log the failed alert attempt to alert logs for audit purposes + try { + const AlertLog = require('../models/AlertLog'); + await AlertLog.create({ + alert_rule_id: rule ? rule.id : null, + detection_id: null, + alert_type: channel, + recipient: channel === 'sms' ? rule?.sms_phone_number : + channel === 'email' ? (rule?.email || rule?.user?.email) : + channel === 'webhook' ? rule?.webhook_url : 'unknown', + message: `Device recovery alert failed for ${device.name}`, + status: 'failed', + sent_at: new Date(), + external_id: null, + priority: rule?.priority || 'medium', + error_message: `Device health service recovery error: ${channelError.message}`, + alert_event_id: null + }); + } catch (logError) { + console.error(`❌ Failed to log recovery alert failure:`, logError.message); + } } } } catch (error) { console.error(`❌ Error triggering recovery alert for device ${device.id}:`, error); + + // Log the general failure to alert logs for audit purposes + try { + const AlertLog = require('../models/AlertLog'); + await AlertLog.create({ + alert_rule_id: rule ? rule.id : null, + detection_id: null, + alert_type: 'system', + recipient: 'system', + message: `Failed to process device recovery alert for ${device.name}`, + status: 'failed', + sent_at: new Date(), + external_id: null, + priority: 'high', + error_message: `Device health service recovery error: ${error.message}`, + alert_event_id: null + }); + } catch (logError) { + console.error(`❌ Failed to log system recovery alert failure:`, logError.message); + } } }