From 73e3aaf017febe0cf4313f680bf1926f6587d2df Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 6 May 2025 00:08:11 +0530 Subject: [PATCH] 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. + + + + +