diff --git a/Server/controllers/notificationController.js b/Server/controllers/notificationController.js index 70b099feb..22c6f473c 100644 --- a/Server/controllers/notificationController.js +++ b/Server/controllers/notificationController.js @@ -6,15 +6,15 @@ class NotificationController { } async triggerNotification(req, res) { - const { monitorId, type, webhookUrl, botToken, chatId } = req.body; - + const { monitorId, type, config } = req.body; + if (!monitorId || !type) { return res.status(400).json({ success: false, msg: "monitorId and type are required" }); } - + try { const networkResponse = { monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" }, @@ -22,33 +22,37 @@ class NotificationController { statusChanged: true, prevStatus: true, }; - + if (type === "telegram") { - if (!botToken || !chatId) { + if (!config?.botToken || !config?.chatId) { return res.status(400).json({ success: false, msg: "botToken and chatId are required for Telegram notifications" }); } - await this.notificationService.sendWebhookNotification(networkResponse, null, type, botToken, chatId); + await this.notificationService.sendWebhookNotification( + networkResponse, null, type, config.botToken, config.chatId + ); } else if (type === "discord" || type === "slack") { - if (!webhookUrl) { + if (!config?.webhookUrl) { return res.status(400).json({ success: false, msg: `webhookUrl is required for ${type} notifications` }); } - await this.notificationService.sendWebhookNotification(networkResponse, webhookUrl, type); + await this.notificationService.sendWebhookNotification( + networkResponse, config.webhookUrl, type + ); } else if (type === "email") { - if (!req.body.address) { + if (!config?.address) { return res.status(400).json({ success: false, msg: "address is required for email notifications" }); } - await this.notificationService.sendEmail(networkResponse, req.body.address); + await this.notificationService.sendEmail(networkResponse, config.address); } - + res.json({ success: true, msg: "Notification sent successfully" }); } catch (error) { logger.error({ @@ -60,6 +64,7 @@ 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 7a3d599ff..aebbdd2fb 100644 --- a/Server/db/models/Notification.js +++ b/Server/db/models/Notification.js @@ -8,17 +8,14 @@ const NotificationSchema = mongoose.Schema( }, type: { type: String, - enum: ["email", "sms", "discord", "slack", "telegram"], + enum: ["email", "sms"], }, - webhookUrl: { - type: String, - }, - botToken: { - type: String, - }, - chatId: { - type: String, - }, + config: { + webhookUrl: { type: String }, // For Discord & Slack + botToken: { type: String }, // For Telegram + chatId: { type: String }, // For Telegram + }, + address: { type: String, }, @@ -59,6 +56,19 @@ 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;