From 30d8aadf12af55198679960685130321507af865 Mon Sep 17 00:00:00 2001 From: Lukas <35193662+NixNotCastey@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:24:34 +0200 Subject: [PATCH] Slightly refactor --- server/notification-providers/smtp.js | 33 ++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index 2bbec5848..7def06940 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -1,5 +1,6 @@ const nodemailer = require("nodemailer"); const NotificationProvider = require("./notification-provider"); +const { DOWN, UP } = require("../../src/util"); class SMTP extends NotificationProvider { @@ -28,33 +29,29 @@ class SMTP extends NotificationProvider { // If custom subject is not empty, change subject for notification if (customsubject !== "") { - - // Replace "MACROS" with coresponding variable + + // Replace "MACROS" with coresponding variable let replaceName = new RegExp("{NAME}", "g"); let replaceHostname = new RegExp("{HOSTNAME}", "g"); let replaceStatus = new RegExp("{STATUS}", "g"); - let serviceStatus; + // Lets start with dummy values to simplify code + let monitorName = "Test" + let monitorHostname = "example.com" + let serviceStatus = "⚠️ Test"; if (monitorJSON !== null) { - customsubject = customsubject.replace(replaceName,monitorJSON["name"]); - customsubject = customsubject.replace(replaceHostname,monitorJSON["hostname"]); - } else { - // Insert dummy values during test - customsubject = customsubject.replace(replaceName,"Test"); - customsubject = customsubject.replace(replaceHostname,"example.com"); + monitorName = monitorJSON["name"]; + monitorHostname = monitorJSON["hostname"]; } + if (heartbeatJSON !== null) { - if (heartbeatJSON["status"] === 0) { - serviceStatus = "🔴 Down" - } else { - serviceStatus = "✅ Up" - } - customsubject = customsubject.replace(replaceStatus,serviceStatus); - } else { - // Insert dummy values during test - customsubject = customsubject.replace(replaceStatus,"TEST"); + serviceStatus = heartbeatJSON["status"] == DOWN ? "🔴 Down":"✅ Up"; } + // Break replace to one by line for better readability + customsubject = customsubject.replace(replaceStatus,serviceStatus); + customsubject = customsubject.replace(replaceName,monitorName); + customsubject = customsubject.replace(replaceHostname,monitorHostname); subject = customsubject }