Merge pull request #2293 from bluewave-labs/feat/settings-test-email-endpoint

feat: settings test email endpoint
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-05-19 09:51:58 -07:00
commit 586c9d054c
6 files changed
+90 -11

No files matched your search

+56 -2
View File
@@ -1,13 +1,14 @@
import { updateAppSettingsBodyValidation } from "../validation/joi.js"; import { updateAppSettingsBodyValidation } from "../validation/joi.js";
import { handleValidationError, handleError } from "./controllerUtils.js"; import { handleValidationError, handleError } from "./controllerUtils.js";
import { sendTestEmailBodyValidation } from "../validation/joi.js";
const SERVICE_NAME = "SettingsController"; const SERVICE_NAME = "SettingsController";
class SettingsController { class SettingsController {
constructor(db, settingsService, stringService) { constructor({ db, settingsService, stringService, emailService }) {
this.db = db; this.db = db;
this.settingsService = settingsService; this.settingsService = settingsService;
this.stringService = stringService; this.stringService = stringService;
this.emailService = emailService;
} }
getAppSettings = async (req, res, next) => { getAppSettings = async (req, res, next) => {
@@ -55,6 +56,59 @@ class SettingsController {
next(handleError(error, SERVICE_NAME, "updateAppSettings")); next(handleError(error, SERVICE_NAME, "updateAppSettings"));
} }
}; };
sendTestEmail = async (req, res, next) => {
try {
await sendTestEmailBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const {
to,
systemEmailHost,
systemEmailPort,
systemEmailAddress,
systemEmailPassword,
systemEmailUser,
systemEmailConnectionHost,
} = req.body;
const subject = this.stringService.testEmailSubject;
const context = { testName: "Monitoring System" };
const messageId = await this.emailService.buildAndSendEmail(
"testEmailTemplate",
context,
to,
subject,
{
systemEmailHost,
systemEmailPort,
systemEmailUser,
systemEmailAddress,
systemEmailPassword,
systemEmailConnectionHost,
}
);
if (!messageId) {
return res.error({
msg: "Failed to send test email.",
});
}
return res.success({
msg: this.stringService.sendTestEmail,
data: { messageId },
});
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
};
} }
export default SettingsController; export default SettingsController;
+6 -5
View File
@@ -241,11 +241,12 @@ const startApp = async () => {
ServiceRegistry.get(EmailService.SERVICE_NAME) ServiceRegistry.get(EmailService.SERVICE_NAME)
); );
const settingsController = new SettingsController( const settingsController = new SettingsController({
ServiceRegistry.get(MongoDB.SERVICE_NAME), db: ServiceRegistry.get(MongoDB.SERVICE_NAME),
ServiceRegistry.get(SettingsService.SERVICE_NAME), settingsService: ServiceRegistry.get(SettingsService.SERVICE_NAME),
ServiceRegistry.get(StringService.SERVICE_NAME) stringService: ServiceRegistry.get(StringService.SERVICE_NAME),
); emailService: ServiceRegistry.get(EmailService.SERVICE_NAME),
});
const checkController = new CheckController( const checkController = new CheckController(
ServiceRegistry.get(MongoDB.SERVICE_NAME), ServiceRegistry.get(MongoDB.SERVICE_NAME),
+2 -2
View File
@@ -4,8 +4,8 @@ import multer from "multer";
import { fetchMonitorCertificate } from "../controllers/controllerUtils.js"; import { fetchMonitorCertificate } from "../controllers/controllerUtils.js";
const upload = multer({ const upload = multer({
storage: multer.memoryStorage() // Store file in memory as Buffer storage: multer.memoryStorage(), // Store file in memory as Buffer
}); });
class MonitorRoutes { class MonitorRoutes {
constructor(monitorController) { constructor(monitorController) {
+5
View File
@@ -15,6 +15,11 @@ class SettingsRoutes {
isAllowed(["admin", "superadmin"]), isAllowed(["admin", "superadmin"]),
this.settingsController.updateAppSettings this.settingsController.updateAppSettings
); );
this.router.post(
"/test-email",
isAllowed(["admin", "superadmin"]),
this.settingsController.sendTestEmail
);
} }
getRouter() { getRouter() {
+10 -2
View File
@@ -89,8 +89,16 @@ class EmailService {
* @param {string} subject - The subject of the email. * @param {string} subject - The subject of the email.
* @returns {Promise<string>} A promise that resolves to the messageId of the sent email. * @returns {Promise<string>} A promise that resolves to the messageId of the sent email.
*/ */
buildAndSendEmail = async (template, context, to, subject) => { buildAndSendEmail = async (template, context, to, subject, transportConfig) => {
// TODO - Consider an update transporter method so this only needs to be recreated when smtp settings change // TODO - Consider an update transporter method so this only needs to be recreated when smtp settings change
let config;
if (typeof transportConfig !== "undefined") {
config = transportConfig;
} else {
config = this.settingsService.getDBSettings();
}
const { const {
systemEmailHost, systemEmailHost,
systemEmailPort, systemEmailPort,
@@ -98,7 +106,7 @@ class EmailService {
systemEmailAddress, systemEmailAddress,
systemEmailPassword, systemEmailPassword,
systemEmailConnectionHost, systemEmailConnectionHost,
} = await this.settingsService.getDBSettings(); } = config;
const baseEmailConfig = { const baseEmailConfig = {
host: systemEmailHost, host: systemEmailHost,
+11
View File
@@ -591,6 +591,16 @@ const createAnnouncementValidation = joi.object({
userId: joi.string().required(), userId: joi.string().required(),
}); });
const sendTestEmailBodyValidation = joi.object({
to: joi.string().required(),
systemEmailHost: joi.string(),
systemEmailPort: joi.number(),
systemEmailAddress: joi.string(),
systemEmailPassword: joi.string(),
systemEmailUser: joi.string(),
systemEmailConnectionHost: joi.string(),
});
export { export {
roleValidatior, roleValidatior,
loginValidation, loginValidation,
@@ -654,4 +664,5 @@ export {
triggerNotificationBodyValidation, triggerNotificationBodyValidation,
webhookConfigValidation, webhookConfigValidation,
createAnnouncementValidation, createAnnouncementValidation,
sendTestEmailBodyValidation,
}; };