From 73e3aaf017febe0cf4313f680bf1926f6587d2df Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:08:11 +0530 Subject: [PATCH 1/8] Initial push for the test email --- server/controllers/monitorController.js | 43 ++++++++++++++++++++++++- server/index.js | 3 +- server/routes/monitorRoute.js | 6 ++++ server/service/emailService.js | 1 + server/templates/testEmailTemplate.mjml | 17 ++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 server/templates/testEmailTemplate.mjml diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index 06c2d583f..3b673a93f 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -27,11 +27,12 @@ const SERVICE_NAME = "monitorController"; import pkg from "papaparse"; class MonitorController { - constructor(db, settingsService, jobQueue, stringService) { + constructor(db, settingsService, jobQueue, stringService, emailService) { this.db = db; this.settingsService = settingsService; this.jobQueue = jobQueue; this.stringService = stringService; + this.emailService = emailService; } /** @@ -636,6 +637,46 @@ class MonitorController { } }; + /** + * Sends a test email to verify email delivery functionality. + * @async + * @param {Object} req - The Express request object. + * @property {Object} req.body - The body of the request. + * @property {string} req.body.to - The email address to send the test email to. + * @param {Object} res - The Express response object. + * @param {function} next - The next middleware function. + * @returns {Object} The response object with a success status and the email delivery message ID. + * @throws {Error} If there is an error while sending the test email. + */ + sendTestEmail = async (req, res, next) => { + try { + const { to } = req.body; + + if (!to || typeof to !== "string") { + return res + .status(400) + .json({ error: "A valid recipient email address is required." }); + } + + const subject = "Test Email from Monitoring System"; + const context = { testName: "Monitoring System" }; + + const messageId = await this.emailService.buildAndSendEmail( + "testEmailTemplate", + context, + to, + subject + ); + + return res.success({ + msg: "Test email sent successfully.", + data: { messageId }, + }); + } catch (error) { + next(handleError(error, SERVICE_NAME, "sendTestEmail")); + } + }; + getMonitorsByTeamId = async (req, res, next) => { try { await getMonitorsByTeamIdParamValidation.validateAsync(req.params); diff --git a/server/index.js b/server/index.js index 520e5cab2..153660de1 100755 --- a/server/index.js +++ b/server/index.js @@ -231,7 +231,8 @@ const startApp = async () => { ServiceRegistry.get(MongoDB.SERVICE_NAME), ServiceRegistry.get(SettingsService.SERVICE_NAME), ServiceRegistry.get(JobQueue.SERVICE_NAME), - ServiceRegistry.get(StringService.SERVICE_NAME) + ServiceRegistry.get(StringService.SERVICE_NAME), + ServiceRegistry.get(EmailService.SERVICE_NAME), ); const settingsController = new SettingsController( diff --git a/server/routes/monitorRoute.js b/server/routes/monitorRoute.js index 4b4331d5b..f9ef460c6 100755 --- a/server/routes/monitorRoute.js +++ b/server/routes/monitorRoute.js @@ -99,6 +99,12 @@ class MonitorRoutes { ); this.router.post("/seed", isAllowed(["superadmin"]), this.monitorController.seedDb); + + this.router.post( + "/test-email", + isAllowed(["admin", "superadmin"]), + this.monitorController.sendTestEmail + ); } getRouter() { diff --git a/server/service/emailService.js b/server/service/emailService.js index ac2e077c0..c9e34eeda 100755 --- a/server/service/emailService.js +++ b/server/service/emailService.js @@ -67,6 +67,7 @@ class EmailService { serverIsUpTemplate: this.loadTemplate("serverIsUp"), passwordResetTemplate: this.loadTemplate("passwordReset"), hardwareIncidentTemplate: this.loadTemplate("hardwareIncident"), + testEmailTemplate: this.loadTemplate("testEmailTemplate") }; /** diff --git a/server/templates/testEmailTemplate.mjml b/server/templates/testEmailTemplate.mjml new file mode 100644 index 000000000..e9fd3221e --- /dev/null +++ b/server/templates/testEmailTemplate.mjml @@ -0,0 +1,17 @@ + + + + + + Hello! + + + This is a test email from the Monitoring System. + + + If you're receiving this, your email configuration is working correctly. + + + + + From 0b9535820f370d7cd15810bb98e94e6e9b4f18d6 Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:22:26 +0530 Subject: [PATCH 2/8] Added returing of error message if the mail fails. --- server/controllers/monitorController.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index 3b673a93f..1a37f2b24 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -668,6 +668,12 @@ class MonitorController { subject ); + if (!messageId) { + return res.error({ + msg: "Failed to send test email.", + }); + } + return res.success({ msg: "Test email sent successfully.", data: { messageId }, From 6d4b3f5174e08269e44afb871624bf5a4f3ac4e4 Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:25:02 +0530 Subject: [PATCH 3/8] Returing only internal error spaces. --- server/controllers/monitorController.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index 1a37f2b24..6343f2b28 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -653,9 +653,7 @@ class MonitorController { const { to } = req.body; if (!to || typeof to !== "string") { - return res - .status(400) - .json({ error: "A valid recipient email address is required." }); + return res.error({ error: "A valid recipient email address is required." }); } const subject = "Test Email from Monitoring System"; From 6268917abe32af74bd1c75803c763536813d2a15 Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:27:27 +0530 Subject: [PATCH 4/8] Use string service for success. --- server/controllers/monitorController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index 6343f2b28..733b20ed0 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -673,7 +673,7 @@ class MonitorController { } return res.success({ - msg: "Test email sent successfully.", + msg: this.stringService.sendTestEmail, data: { messageId }, }); } catch (error) { From f8233aef08cd693ec49cb311fbce2c2637d63e0f Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:28:54 +0530 Subject: [PATCH 5/8] local en addition. --- server/locales/en.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/locales/en.json b/server/locales/en.json index 449ab5a8e..b53c6d406 100755 --- a/server/locales/en.json +++ b/server/locales/en.json @@ -158,5 +158,6 @@ "platformRequired": "Platform is required", "testNotificationFailed": "Failed to send test notification", "monitorUpAlert": "Uptime Alert: One of your monitors is back online.\nšŸ“Œ Monitor: {monitorName}\nšŸ“… Time: {time}\nāš ļø Status: UP\nšŸ“Ÿ Status Code: {code}\n\u200B\n", - "monitorDownAlert": "Downtime Alert: One of your monitors went offline.\nšŸ“Œ Monitor: {monitorName}\nšŸ“… Time: {time}\nāš ļø Status: DOWN\nšŸ“Ÿ Status Code: {code}\n\u200B\n" + "monitorDownAlert": "Downtime Alert: One of your monitors went offline.\nšŸ“Œ Monitor: {monitorName}\nšŸ“… Time: {time}\nāš ļø Status: DOWN\nšŸ“Ÿ Status Code: {code}\n\u200B\n", + "sendTestEmail": "Test email sent successfully" } From 87573945f2b98fff0c96fdaa83fddfc0506a06fe Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:33:31 +0530 Subject: [PATCH 6/8] returing msg instead of error. --- server/controllers/monitorController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index 733b20ed0..c917d1269 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -653,7 +653,7 @@ class MonitorController { const { to } = req.body; if (!to || typeof to !== "string") { - return res.error({ error: "A valid recipient email address is required." }); + return res.error({ msg: "A valid recipient email address is required." }); } const subject = "Test Email from Monitoring System"; From c2eeed81217bc81f4f2057fd81ad0666f4265e6b Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:37:01 +0530 Subject: [PATCH 7/8] Finall touch on string msgs. --- server/controllers/monitorController.js | 2 +- server/locales/en.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index c917d1269..5d6a4665e 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -653,7 +653,7 @@ class MonitorController { const { to } = req.body; if (!to || typeof to !== "string") { - return res.error({ msg: "A valid recipient email address is required." }); + return res.error({ msg: this.stringService.errorForValidEmailAddress }); } const subject = "Test Email from Monitoring System"; diff --git a/server/locales/en.json b/server/locales/en.json index b53c6d406..5496b0e74 100755 --- a/server/locales/en.json +++ b/server/locales/en.json @@ -159,5 +159,6 @@ "testNotificationFailed": "Failed to send test notification", "monitorUpAlert": "Uptime Alert: One of your monitors is back online.\nšŸ“Œ Monitor: {monitorName}\nšŸ“… Time: {time}\nāš ļø Status: UP\nšŸ“Ÿ Status Code: {code}\n\u200B\n", "monitorDownAlert": "Downtime Alert: One of your monitors went offline.\nšŸ“Œ Monitor: {monitorName}\nšŸ“… Time: {time}\nāš ļø Status: DOWN\nšŸ“Ÿ Status Code: {code}\n\u200B\n", - "sendTestEmail": "Test email sent successfully" + "sendTestEmail": "Test email sent successfully", + "errorForValidEmailAddress": "A valid recipient email address is required." } From bd236480790e0573ea49205104aca771200b6af3 Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:38:38 +0530 Subject: [PATCH 8/8] string service for subject. --- server/controllers/monitorController.js | 2 +- server/locales/en.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index 5d6a4665e..6295a9aba 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -656,7 +656,7 @@ class MonitorController { return res.error({ msg: this.stringService.errorForValidEmailAddress }); } - const subject = "Test Email from Monitoring System"; + const subject = this.stringService.testEmailSubject; const context = { testName: "Monitoring System" }; const messageId = await this.emailService.buildAndSendEmail( diff --git a/server/locales/en.json b/server/locales/en.json index 5496b0e74..6cd956c37 100755 --- a/server/locales/en.json +++ b/server/locales/en.json @@ -160,5 +160,6 @@ "monitorUpAlert": "Uptime Alert: One of your monitors is back online.\nšŸ“Œ Monitor: {monitorName}\nšŸ“… Time: {time}\nāš ļø Status: UP\nšŸ“Ÿ Status Code: {code}\n\u200B\n", "monitorDownAlert": "Downtime Alert: One of your monitors went offline.\nšŸ“Œ Monitor: {monitorName}\nšŸ“… Time: {time}\nāš ļø Status: DOWN\nšŸ“Ÿ Status Code: {code}\n\u200B\n", "sendTestEmail": "Test email sent successfully", - "errorForValidEmailAddress": "A valid recipient email address is required." + "errorForValidEmailAddress": "A valid recipient email address is required.", + "testEmailSubject": "Test Email from Monitoring System" }