Fix jwt-token
This commit is contained in:
@@ -523,6 +523,30 @@ class AlertService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sendSMSAlert(phoneNumber, message, rule, detection, threatAssessment = null, alertEventId = null) {
|
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
|
// Check if Twilio is enabled
|
||||||
if (!this.twilioEnabled || !this.twilioClient) {
|
if (!this.twilioEnabled || !this.twilioClient) {
|
||||||
console.log('📱 SMS alert skipped - Twilio not configured');
|
console.log('📱 SMS alert skipped - Twilio not configured');
|
||||||
@@ -531,7 +555,7 @@ class AlertService {
|
|||||||
|
|
||||||
return await AlertLog.create({
|
return await AlertLog.create({
|
||||||
alert_rule_id: rule.id,
|
alert_rule_id: rule.id,
|
||||||
detection_id: detection.id,
|
detection_id: detection ? detection.id : null,
|
||||||
alert_type: 'sms',
|
alert_type: 'sms',
|
||||||
recipient: phoneNumber,
|
recipient: phoneNumber,
|
||||||
message: message,
|
message: message,
|
||||||
@@ -569,7 +593,7 @@ class AlertService {
|
|||||||
|
|
||||||
return await AlertLog.create({
|
return await AlertLog.create({
|
||||||
alert_rule_id: rule.id,
|
alert_rule_id: rule.id,
|
||||||
detection_id: detection.id,
|
detection_id: detection ? detection.id : null,
|
||||||
alert_type: 'sms',
|
alert_type: 'sms',
|
||||||
recipient: phoneNumber,
|
recipient: phoneNumber,
|
||||||
message: message,
|
message: message,
|
||||||
@@ -597,7 +621,7 @@ class AlertService {
|
|||||||
|
|
||||||
return await AlertLog.create({
|
return await AlertLog.create({
|
||||||
alert_rule_id: rule.id,
|
alert_rule_id: rule.id,
|
||||||
detection_id: detection.id,
|
detection_id: detection ? detection.id : null,
|
||||||
alert_type: 'sms',
|
alert_type: 'sms',
|
||||||
recipient: phoneNumber,
|
recipient: phoneNumber,
|
||||||
message: message,
|
message: message,
|
||||||
@@ -612,6 +636,30 @@ class AlertService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sendEmailAlert(email, message, rule, detection, threatAssessment = null, alertEventId = null) {
|
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 {
|
try {
|
||||||
// Email implementation would go here
|
// Email implementation would go here
|
||||||
// For now, just log the alert
|
// For now, just log the alert
|
||||||
@@ -624,12 +672,12 @@ class AlertService {
|
|||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
simulatedDelivery: true,
|
simulatedDelivery: true,
|
||||||
ruleId: rule.id,
|
ruleId: rule.id,
|
||||||
detectionId: detection.id
|
detectionId: detection ? detection.id : null
|
||||||
};
|
};
|
||||||
|
|
||||||
return await AlertLog.create({
|
return await AlertLog.create({
|
||||||
alert_rule_id: rule.id,
|
alert_rule_id: rule.id,
|
||||||
detection_id: detection.id,
|
detection_id: detection ? detection.id : null,
|
||||||
alert_type: 'email',
|
alert_type: 'email',
|
||||||
recipient: email,
|
recipient: email,
|
||||||
message: message,
|
message: message,
|
||||||
@@ -654,7 +702,7 @@ class AlertService {
|
|||||||
|
|
||||||
return await AlertLog.create({
|
return await AlertLog.create({
|
||||||
alert_rule_id: rule.id,
|
alert_rule_id: rule.id,
|
||||||
detection_id: detection.id,
|
detection_id: detection ? detection.id : null,
|
||||||
alert_type: 'email',
|
alert_type: 'email',
|
||||||
recipient: email,
|
recipient: email,
|
||||||
message: message,
|
message: message,
|
||||||
@@ -668,10 +716,34 @@ class AlertService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sendWebhookAlert(webhookUrl, detection, device, rule, threatAssessment = null, alertEventId = null) {
|
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 = {
|
const payload = {
|
||||||
event: 'drone_detection',
|
event: 'drone_detection',
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
detection: {
|
detection: detection ? {
|
||||||
id: detection.id,
|
id: detection.id,
|
||||||
device_id: detection.device_id,
|
device_id: detection.device_id,
|
||||||
drone_id: detection.drone_id,
|
drone_id: detection.drone_id,
|
||||||
@@ -681,14 +753,14 @@ class AlertService {
|
|||||||
geo_lat: detection.geo_lat,
|
geo_lat: detection.geo_lat,
|
||||||
geo_lon: detection.geo_lon,
|
geo_lon: detection.geo_lon,
|
||||||
server_timestamp: detection.server_timestamp
|
server_timestamp: detection.server_timestamp
|
||||||
},
|
} : null,
|
||||||
device: {
|
device: device ? {
|
||||||
id: device.id,
|
id: device.id,
|
||||||
name: device.name,
|
name: device.name,
|
||||||
geo_lat: device.geo_lat,
|
geo_lat: device.geo_lat,
|
||||||
geo_lon: device.geo_lon,
|
geo_lon: device.geo_lon,
|
||||||
location_description: device.location_description
|
location_description: device.location_description
|
||||||
},
|
} : null,
|
||||||
alert_rule: {
|
alert_rule: {
|
||||||
id: rule.id,
|
id: rule.id,
|
||||||
name: rule.name,
|
name: rule.name,
|
||||||
|
|||||||
Reference in New Issue
Block a user