diff --git a/server/controllers/notificationController.js b/server/controllers/notificationController.js index df16f7a75..4efcf4988 100755 --- a/server/controllers/notificationController.js +++ b/server/controllers/notificationController.js @@ -85,30 +85,33 @@ class NotificationController { } testNotification = async (req, res, next) => { - console.log("TESTING"); try { const notification = req.body; + let success; if (notification?.type === "email") { - const result = await this.notificationService.sendTestEmail(notification); - return res.success({ - msg: this.stringService.emailSendSuccess, - }); + success = await this.notificationService.sendTestEmail(notification); } if (notification?.type === "webhook") { - const success = + success = await this.notificationService.sendTestWebhookNotification(notification); - if (!success) { - return res.error({ - msg: this.stringService.webhookSendFailed, - status: 400, - }); - } - return res.success({ - msg: this.stringService.webhookSendSuccess, + } + + if (notification?.type === "pager_duty") { + success = + await this.notificationService.sendTestPagerDutyNotification(notification); + } + if (!success) { + return res.error({ + msg: "Sending notification failed", + status: 400, }); } + + return res.success({ + msg: "Notification sent successfully", + }); } catch (error) { next(handleError(error, SERVICE_NAME, "testWebhook")); } diff --git a/server/service/networkService.js b/server/service/networkService.js index 5ceddb8fd..e04d69c41 100755 --- a/server/service/networkService.js +++ b/server/service/networkService.js @@ -463,6 +463,29 @@ class NetworkService { } } + async requestPagerDuty({ message, routingKey, monitorUrl }) { + try { + const response = await this.axios.post(`https://events.pagerduty.com/v2/enqueue`, { + routing_key: routingKey, + event_action: "trigger", + payload: { + summary: message, + severity: "critical", + source: monitorUrl, + timestamp: new Date().toISOString(), + }, + }); + + if (response?.data?.status !== "success") return false; + return true; + } catch (error) { + error.details = error.response?.data; + error.service = this.SERVICE_NAME; + error.method = "requestPagerDuty"; + throw error; + } + } + /** * Gets the status of a job based on its type and returns the appropriate response. * diff --git a/server/service/notificationService.js b/server/service/notificationService.js index 30c8474c1..6e12722c2 100755 --- a/server/service/notificationService.js +++ b/server/service/notificationService.js @@ -211,7 +211,11 @@ class NotificationService { to, subject ); - console.log(messageId); + + if (messageId) { + return true; + } + return false; }; /** @@ -223,6 +227,7 @@ class NotificationService { * @param {string} address - Email address to send the notification to * @returns {Promise} - Indicates email was sent successfully */ + async sendEmail(networkResponse, address) { const { monitor, status, prevStatus } = networkResponse; const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate"; @@ -232,6 +237,16 @@ class NotificationService { return true; } + async sendTestPagerDutyNotification(notification) { + const { routingKey } = notification.config; + const response = await this.networkService.requestPagerDuty({ + message: "This is a test notification", + monitorUrl: "Test notification", + routingKey, + }); + + return response; + } async sendPagerDutyNotification(networkResponse, notification) { const { monitor, status, code } = networkResponse; const { routingKey, platform } = notification.config; @@ -251,7 +266,7 @@ class NotificationService { routingKey, monitorUrl: monitor.url, }); - return response.status; + return response; } catch (error) { this.logger.error({ message: "Failed to send PagerDuty notification",