From f46b0768fcf706bd5b8ea3d50ab51ad683256a8e Mon Sep 17 00:00:00 2001 From: Skorpios Date: Sun, 9 Feb 2025 13:58:14 -0800 Subject: [PATCH] Refactored notification schema config object. --- Server/controllers/notificationController.js | 45 ++++++++++---------- Server/db/models/Notification.js | 23 +++------- Server/service/notificationService.js | 19 +++++---- 3 files changed, 37 insertions(+), 50 deletions(-) diff --git a/Server/controllers/notificationController.js b/Server/controllers/notificationController.js index 22c6f473c..dadf86a58 100644 --- a/Server/controllers/notificationController.js +++ b/Server/controllers/notificationController.js @@ -23,34 +23,34 @@ class NotificationController { prevStatus: true, }; - if (type === "telegram") { - if (!config?.botToken || !config?.chatId) { + if (type === "webhook") { + if (!config?.type) { return res.status(400).json({ success: false, - msg: "botToken and chatId are required for Telegram notifications" + msg: "webhook type is required in config" }); } + + if (config.type === "telegram") { + if (!config.botToken || !config.chatId) { + return res.status(400).json({ + success: false, + msg: "botToken and chatId are required for Telegram notifications" + }); + } + } else if (["discord", "slack"].includes(config.type)) { + if (!config.webhookUrl) { + return res.status(400).json({ + success: false, + msg: `webhookUrl is required for ${config.type} notifications` + }); + } + } + await this.notificationService.sendWebhookNotification( - networkResponse, null, type, config.botToken, config.chatId + networkResponse, + config ); - } else if (type === "discord" || type === "slack") { - if (!config?.webhookUrl) { - return res.status(400).json({ - success: false, - msg: `webhookUrl is required for ${type} notifications` - }); - } - await this.notificationService.sendWebhookNotification( - networkResponse, config.webhookUrl, type - ); - } else if (type === "email") { - if (!config?.address) { - return res.status(400).json({ - success: false, - msg: "address is required for email notifications" - }); - } - await this.notificationService.sendEmail(networkResponse, config.address); } res.json({ success: true, msg: "Notification sent successfully" }); @@ -64,7 +64,6 @@ class NotificationController { res.status(500).json({ success: false, msg: "Failed to send notification" }); } } - } export default NotificationController; \ No newline at end of file diff --git a/Server/db/models/Notification.js b/Server/db/models/Notification.js index aebbdd2fb..7d684bbc1 100644 --- a/Server/db/models/Notification.js +++ b/Server/db/models/Notification.js @@ -8,14 +8,14 @@ const NotificationSchema = mongoose.Schema( }, type: { type: String, - enum: ["email", "sms"], + enum: ["email", "sms", "webhook"], }, config: { - webhookUrl: { type: String }, // For Discord & Slack - botToken: { type: String }, // For Telegram - chatId: { type: String }, // For Telegram + type: String, + webhookUrl: String, + botToken: String, + chatId: String }, - address: { type: String, }, @@ -56,19 +56,6 @@ const NotificationSchema = mongoose.Schema( } ); -NotificationSchema.pre("save", function (next) { - if (this.type === "telegram" && (!this.config.botToken || !this.config.chatId)) { - return next(new Error("botToken and chatId are required for Telegram notifications")); - } - if ((this.type === "discord" || this.type === "slack") && !this.config.webhookUrl) { - return next(new Error(`webhookUrl is required for ${this.type} notifications`)); - } - if (this.type === "email" && !this.config.address) { - return next(new Error("address is required for email notifications")); - } - next(); - }); - NotificationSchema.pre("save", function (next) { if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) { this.cpuAlertThreshold = this.alertThreshold; diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index 0f402626e..ebd27c166 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -33,22 +33,23 @@ class NotificationService { return null; } - async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) { + async sendWebhookNotification(networkResponse, config) { const { monitor, status } = networkResponse; - let url = address; + const { type, webhookUrl, botToken, chatId } = config; + let url = webhookUrl; - const message = this.formatNotificationMessage(monitor, status, platform, chatId); + const message = this.formatNotificationMessage(monitor, status, type, chatId); if (!message) { this.logger.warn({ - message: `Unsupported platform: ${platform}`, + message: `Unsupported webhook type: ${type}`, service: this.SERVICE_NAME, method: 'sendWebhookNotification', - platform + type }); return false; } - if (platform === 'telegram') { + if (type === 'telegram') { if (!botToken || !chatId) { return false; } @@ -56,17 +57,17 @@ class NotificationService { } try { - const response = await this.networkService.requestWebhook(platform, url, message); + const response = await this.networkService.requestWebhook(type, url, message); return response.status; } catch (error) { this.logger.error({ - message: `Error sending ${platform} notification`, + message: `Error sending ${type} notification`, service: this.SERVICE_NAME, method: 'sendWebhookNotification', error: error.message, stack: error.stack, url, - platform, + type, requestPayload: message }); return false;