refactor settingsController
This commit is contained in:
1 parent
a6b92d3c51
commit
84ca5db0fa
3 files changed
+25
-20
No files matched your search
@@ -2,14 +2,15 @@ import { Request, Response, NextFunction } from "express";
|
|||||||
import { updateAppSettingsBodyValidation } from "@/validation/settingsValidation.js";
|
import { updateAppSettingsBodyValidation } from "@/validation/settingsValidation.js";
|
||||||
import { sendTestEmailBodyValidation } from "@/validation/notificationValidation.js";
|
import { sendTestEmailBodyValidation } from "@/validation/notificationValidation.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { IEmailService, ISettingsService } from "@/service/index.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "SettingsController";
|
const SERVICE_NAME = "SettingsController";
|
||||||
|
|
||||||
class SettingsController {
|
class SettingsController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
private settingsService: any;
|
private settingsService: ISettingsService;
|
||||||
private emailService: any;
|
private emailService: IEmailService;
|
||||||
constructor(settingsService: any, emailService: any) {
|
constructor(settingsService: ISettingsService, emailService: IEmailService) {
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
}
|
}
|
||||||
@@ -57,9 +58,9 @@ class SettingsController {
|
|||||||
|
|
||||||
updateAppSettings = async (req: Request, res: Response, next: NextFunction) => {
|
updateAppSettings = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
updateAppSettingsBodyValidation.parse(req.body);
|
const validatedBody = updateAppSettingsBodyValidation.parse(req.body);
|
||||||
|
|
||||||
const updatedSettings = await this.settingsService.updateDbSettings(req.body);
|
const updatedSettings = await this.settingsService.updateDbSettings(validatedBody);
|
||||||
const returnSettings = this.buildAppSettings(updatedSettings);
|
const returnSettings = this.buildAppSettings(updatedSettings);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -95,6 +96,9 @@ class SettingsController {
|
|||||||
const context = { testName: "Monitoring System" };
|
const context = { testName: "Monitoring System" };
|
||||||
|
|
||||||
const html = await this.emailService.buildEmail("testEmailTemplate", context);
|
const html = await this.emailService.buildEmail("testEmailTemplate", context);
|
||||||
|
if (!html) {
|
||||||
|
throw new AppError({ message: "Failed to build email template.", status: 500 });
|
||||||
|
}
|
||||||
const messageId = await this.emailService.sendEmail(to, subject, html, {
|
const messageId = await this.emailService.sendEmail(to, subject, html, {
|
||||||
systemEmailHost,
|
systemEmailHost,
|
||||||
systemEmailPort,
|
systemEmailPort,
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export interface ISettingsService {
|
|||||||
loadSettings(): EnvConfig;
|
loadSettings(): EnvConfig;
|
||||||
getSettings(): EnvConfig;
|
getSettings(): EnvConfig;
|
||||||
getDBSettings(): Promise<Settings>;
|
getDBSettings(): Promise<Settings>;
|
||||||
|
updateDbSettings(newSettings: Partial<Settings>): Promise<Settings>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SettingsService implements ISettingsService {
|
export class SettingsService implements ISettingsService {
|
||||||
|
|||||||
@@ -7,17 +7,17 @@ import { z } from "zod";
|
|||||||
|
|
||||||
export const updateAppSettingsBodyValidation = z
|
export const updateAppSettingsBodyValidation = z
|
||||||
.object({
|
.object({
|
||||||
checkTTL: z.union([z.number().int().min(1).max(CHECK_TTL_SENTINEL), z.literal("")]).optional(),
|
checkTTL: z.union([z.number().int().min(1).max(CHECK_TTL_SENTINEL)]).optional(),
|
||||||
systemEmailPort: z.union([z.number(), z.literal("")]).optional(),
|
systemEmailPort: z.number().optional(),
|
||||||
pagespeedApiKey: z.union([z.string(), z.literal("")]).optional(),
|
pagespeedApiKey: z.string().optional(),
|
||||||
language: z.union([z.string(), z.literal("")]).optional(),
|
language: z.string().optional(),
|
||||||
timezone: z.union([z.string(), z.literal("")]).optional(),
|
timezone: z.string().optional(),
|
||||||
systemEmailHost: z.union([z.string(), z.literal("")]).optional(),
|
systemEmailHost: z.string().optional(),
|
||||||
systemEmailAddress: z.union([z.string(), z.literal("")]).optional(),
|
systemEmailAddress: z.string().optional(),
|
||||||
systemEmailPassword: z.union([z.string(), z.literal("")]).optional(),
|
systemEmailPassword: z.string().optional(),
|
||||||
systemEmailUser: z.union([z.string(), z.literal("")]).optional(),
|
systemEmailUser: z.string().optional(),
|
||||||
systemEmailConnectionHost: z.union([z.string(), z.literal("")]).optional(),
|
systemEmailConnectionHost: z.string().optional(),
|
||||||
systemEmailTLSServername: z.union([z.string(), z.literal("")]).optional(),
|
systemEmailTLSServername: z.string().optional(),
|
||||||
|
|
||||||
showURL: z.boolean().optional(),
|
showURL: z.boolean().optional(),
|
||||||
systemEmailSecure: z.boolean().optional(),
|
systemEmailSecure: z.boolean().optional(),
|
||||||
@@ -28,10 +28,10 @@ export const updateAppSettingsBodyValidation = z
|
|||||||
|
|
||||||
globalThresholds: z
|
globalThresholds: z
|
||||||
.object({
|
.object({
|
||||||
cpu: z.union([z.number().min(1).max(100), z.literal("")]).optional(),
|
cpu: z.union([z.number().min(1).max(100)]).optional(),
|
||||||
memory: z.union([z.number().min(1).max(100), z.literal("")]).optional(),
|
memory: z.union([z.number().min(1).max(100)]).optional(),
|
||||||
disk: z.union([z.number().min(1).max(100), z.literal("")]).optional(),
|
disk: z.union([z.number().min(1).max(100)]).optional(),
|
||||||
temperature: z.union([z.number().min(1).max(150), z.literal("")]).optional(),
|
temperature: z.union([z.number().min(1).max(150)]).optional(),
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user