Files
Checkmate/server/src/controllers/settingsController.ts
T

140 lines
4.2 KiB
TypeScript
Raw Normal View History

2026-01-09 15:16:17 -08:00
import { Request, Response, NextFunction } from "express";
2026-03-01 17:35:00 +00:00
import { updateAppSettingsBodyValidation } from "@/validation/settingsValidation.js";
2026-03-02 17:29:48 +00:00
import { sendTestEmailBodyValidation } from "@/validation/notificationValidation.js";
2026-01-09 15:16:17 -08:00
import { AppError } from "@/utils/AppError.js";
2026-03-12 09:13:35 -07:00
import { IEmailService, ISettingsService } from "@/service/index.js";
2026-03-20 12:40:27 -07:00
import { Settings } from "@/types/settings.js";
2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "SettingsController";
2026-03-20 11:42:40 -07:00
export interface ISettingsController {
serviceName: string;
getAppSettings(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
updateAppSettings(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
sendTestEmail(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
}
class SettingsController implements ISettingsController {
2025-07-25 15:15:14 -07:00
static SERVICE_NAME = SERVICE_NAME;
2026-03-12 09:13:35 -07:00
private settingsService: ISettingsService;
private emailService: IEmailService;
constructor(settingsService: ISettingsService, emailService: IEmailService) {
2025-04-20 11:29:53 -07:00
this.settingsService = settingsService;
2025-05-16 15:01:06 -07:00
this.emailService = emailService;
2025-04-20 11:29:53 -07:00
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return SettingsController.SERVICE_NAME;
}
2026-03-20 12:40:27 -07:00
buildAppSettings = (dbSettings: Settings) => {
const sanitizedSettings: Record<string, unknown> = { ...dbSettings };
2025-05-13 13:51:11 -07:00
delete sanitizedSettings.version;
2026-01-21 13:01:31 -08:00
delete sanitizedSettings.jwtSecret;
2026-03-20 12:40:27 -07:00
const returnSettings: Record<string, unknown | null> = {
2025-05-09 14:53:36 -07:00
pagespeedKeySet: false,
emailPasswordSet: false,
2026-01-09 15:16:17 -08:00
settings: null,
2025-05-09 14:53:36 -07:00
};
if (typeof sanitizedSettings.pagespeedApiKey !== "undefined") {
2025-05-09 14:53:36 -07:00
returnSettings.pagespeedKeySet = true;
delete sanitizedSettings.pagespeedApiKey;
2025-04-20 11:29:53 -07:00
}
2025-05-09 14:53:36 -07:00
if (typeof sanitizedSettings.systemEmailPassword !== "undefined") {
returnSettings.emailPasswordSet = true;
delete sanitizedSettings.systemEmailPassword;
}
returnSettings.settings = sanitizedSettings;
2025-06-17 10:58:30 +08:00
return returnSettings;
};
2026-01-09 15:16:17 -08:00
getAppSettings = async (req: Request, res: Response, next: NextFunction) => {
try {
const dbSettings = await this.settingsService.getDBSettings();
2025-06-17 10:58:30 +08:00
const returnSettings = this.buildAppSettings(dbSettings);
2026-01-09 15:16:17 -08:00
return res.status(200).json({
2026-01-09 15:32:53 -08:00
success: true,
2026-01-09 15:16:17 -08:00
msg: "App settings fetched successfully",
data: returnSettings,
});
2026-01-09 15:16:17 -08:00
} catch (error) {
next(error);
}
};
updateAppSettings = async (req: Request, res: Response, next: NextFunction) => {
2026-03-02 18:46:21 +00:00
try {
2026-03-12 09:13:35 -07:00
const validatedBody = updateAppSettingsBodyValidation.parse(req.body);
2025-05-16 15:01:06 -07:00
2026-03-12 09:13:35 -07:00
const updatedSettings = await this.settingsService.updateDbSettings(validatedBody);
2026-03-02 18:46:21 +00:00
const returnSettings = this.buildAppSettings(updatedSettings);
return res.status(200).json({
success: true,
msg: "App settings updated successfully",
data: returnSettings,
});
} catch (error) {
next(error);
}
2026-01-09 15:16:17 -08:00
};
sendTestEmail = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-01 17:35:00 +00:00
sendTestEmailBodyValidation.parse(req.body);
2025-05-16 15:01:06 -07:00
const {
to,
systemEmailHost,
systemEmailPort,
systemEmailAddress,
systemEmailPassword,
systemEmailUser,
systemEmailConnectionHost,
2025-06-12 00:39:47 +05:30
systemEmailSecure,
systemEmailPool,
systemEmailIgnoreTLS,
systemEmailRequireTLS,
systemEmailRejectUnauthorized,
systemEmailTLSServername,
2025-05-16 15:01:06 -07:00
} = req.body;
2026-01-09 15:16:17 -08:00
const subject = "This is a test email from Checkmate";
2025-05-16 15:01:06 -07:00
const context = { testName: "Monitoring System" };
2025-06-18 10:44:59 +08:00
const html = await this.emailService.buildEmail("testEmailTemplate", context);
2026-03-12 09:13:35 -07:00
if (!html) {
throw new AppError({ message: "Failed to build email template.", status: 500 });
}
2025-06-18 10:44:59 +08:00
const messageId = await this.emailService.sendEmail(to, subject, html, {
systemEmailHost,
systemEmailPort,
systemEmailUser,
systemEmailAddress,
systemEmailPassword,
systemEmailConnectionHost,
systemEmailSecure,
systemEmailPool,
systemEmailIgnoreTLS,
systemEmailRequireTLS,
systemEmailRejectUnauthorized,
systemEmailTLSServername,
});
2025-05-16 15:01:06 -07:00
if (!messageId) {
2026-01-09 15:16:17 -08:00
throw new AppError({ message: "Failed to send test email.", status: 500 });
2025-05-16 15:01:06 -07:00
}
2026-01-09 15:16:17 -08:00
return res.status(200).json({
2026-01-09 15:32:53 -08:00
success: true,
2026-01-09 15:16:17 -08:00
msg: "Test email sent successfully",
2025-05-16 15:01:06 -07:00
data: { messageId },
});
2026-01-09 15:16:17 -08:00
} catch (error) {
next(error);
}
};
2025-04-20 11:29:53 -07:00
}
export default SettingsController;