From 64c5aa9bb095749b55672cbb82f1d0b1a7635164 Mon Sep 17 00:00:00 2001 From: Skorpios Date: Sat, 8 Feb 2025 17:07:14 -0800 Subject: [PATCH] Added notifcation controller and route. --- Server/controllers/notificationController.js | 65 ++++++++++++++++++++ Server/routes/notificationRoute.js | 19 ++++++ 2 files changed, 84 insertions(+) create mode 100644 Server/controllers/notificationController.js create mode 100644 Server/routes/notificationRoute.js diff --git a/Server/controllers/notificationController.js b/Server/controllers/notificationController.js new file mode 100644 index 000000000..70b099feb --- /dev/null +++ b/Server/controllers/notificationController.js @@ -0,0 +1,65 @@ +import logger from '../utils/logger.js'; + +class NotificationController { + constructor(notificationService) { + this.notificationService = notificationService; + } + + async triggerNotification(req, res) { + const { monitorId, type, webhookUrl, botToken, chatId } = 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" }, + status: false, + statusChanged: true, + prevStatus: true, + }; + + if (type === "telegram") { + if (!botToken || !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); + } else if (type === "discord" || type === "slack") { + if (!webhookUrl) { + return res.status(400).json({ + success: false, + msg: `webhookUrl is required for ${type} notifications` + }); + } + await this.notificationService.sendWebhookNotification(networkResponse, webhookUrl, type); + } else if (type === "email") { + if (!req.body.address) { + return res.status(400).json({ + success: false, + msg: "address is required for email notifications" + }); + } + await this.notificationService.sendEmail(networkResponse, req.body.address); + } + + res.json({ success: true, msg: "Notification sent successfully" }); + } catch (error) { + logger.error({ + message: error.message, + service: "NotificationController", + method: "triggerNotification", + stack: error.stack, + }); + res.status(500).json({ success: false, msg: "Failed to send notification" }); + } + } +} + +export default NotificationController; \ No newline at end of file diff --git a/Server/routes/notificationRoute.js b/Server/routes/notificationRoute.js new file mode 100644 index 000000000..6c7fc37d7 --- /dev/null +++ b/Server/routes/notificationRoute.js @@ -0,0 +1,19 @@ +import express from 'express'; + +class NotificationRoutes { + constructor(notificationController) { + this.notificationController = notificationController; + this.router = express.Router(); + this.initializeRoutes(); + } + + initializeRoutes() { + this.router.post('/trigger', this.notificationController.triggerNotification.bind(this.notificationController)); + } + + getRouter() { + return this.router; + } +} + +export default NotificationRoutes; \ No newline at end of file