Merge pull request #3414 from bluewave-labs/fix/settings-controller-typing
fix: SettingsController correct types
This commit is contained in:
4 files changed
+26
-29
No files matched your search
@@ -262,15 +262,7 @@ export const SettingsPage = () => {
|
||||
if (value === undefined) {
|
||||
(acc as any)[typedKey] = "";
|
||||
} else if (typeof value === "object" && value !== null) {
|
||||
// Handle nested objects like globalThresholds
|
||||
// Convert 0 to "" for unsetting thresholds
|
||||
(acc as any)[typedKey] = Object.entries(value).reduce(
|
||||
(nested, [nestedKey, nestedValue]) => ({
|
||||
...nested,
|
||||
[nestedKey]: nestedValue === undefined ? "" : nestedValue,
|
||||
}),
|
||||
{}
|
||||
);
|
||||
(acc as any)[typedKey] = value;
|
||||
} else {
|
||||
(acc as any)[typedKey] = value;
|
||||
}
|
||||
|
||||
@@ -2,14 +2,15 @@ import { Request, Response, NextFunction } from "express";
|
||||
import { updateAppSettingsBodyValidation } from "@/validation/settingsValidation.js";
|
||||
import { sendTestEmailBodyValidation } from "@/validation/notificationValidation.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { IEmailService, ISettingsService } from "@/service/index.js";
|
||||
|
||||
const SERVICE_NAME = "SettingsController";
|
||||
|
||||
class SettingsController {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
private settingsService: any;
|
||||
private emailService: any;
|
||||
constructor(settingsService: any, emailService: any) {
|
||||
private settingsService: ISettingsService;
|
||||
private emailService: IEmailService;
|
||||
constructor(settingsService: ISettingsService, emailService: IEmailService) {
|
||||
this.settingsService = settingsService;
|
||||
this.emailService = emailService;
|
||||
}
|
||||
@@ -57,9 +58,9 @@ class SettingsController {
|
||||
|
||||
updateAppSettings = async (req: Request, res: Response, next: NextFunction) => {
|
||||
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);
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
@@ -95,6 +96,9 @@ class SettingsController {
|
||||
const context = { testName: "Monitoring System" };
|
||||
|
||||
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, {
|
||||
systemEmailHost,
|
||||
systemEmailPort,
|
||||
|
||||
@@ -19,6 +19,7 @@ export interface ISettingsService {
|
||||
loadSettings(): EnvConfig;
|
||||
getSettings(): EnvConfig;
|
||||
getDBSettings(): Promise<Settings>;
|
||||
updateDbSettings(newSettings: Partial<Settings>): Promise<Settings>;
|
||||
}
|
||||
|
||||
export class SettingsService implements ISettingsService {
|
||||
|
||||
@@ -7,17 +7,17 @@ import { z } from "zod";
|
||||
|
||||
export const updateAppSettingsBodyValidation = z
|
||||
.object({
|
||||
checkTTL: z.union([z.number().int().min(1).max(CHECK_TTL_SENTINEL), z.literal("")]).optional(),
|
||||
systemEmailPort: z.union([z.number(), z.literal("")]).optional(),
|
||||
pagespeedApiKey: z.union([z.string(), z.literal("")]).optional(),
|
||||
language: z.union([z.string(), z.literal("")]).optional(),
|
||||
timezone: z.union([z.string(), z.literal("")]).optional(),
|
||||
systemEmailHost: z.union([z.string(), z.literal("")]).optional(),
|
||||
systemEmailAddress: z.union([z.string(), z.literal("")]).optional(),
|
||||
systemEmailPassword: z.union([z.string(), z.literal("")]).optional(),
|
||||
systemEmailUser: z.union([z.string(), z.literal("")]).optional(),
|
||||
systemEmailConnectionHost: z.union([z.string(), z.literal("")]).optional(),
|
||||
systemEmailTLSServername: z.union([z.string(), z.literal("")]).optional(),
|
||||
checkTTL: z.number().int().min(1).max(CHECK_TTL_SENTINEL).optional(),
|
||||
systemEmailPort: z.number().optional(),
|
||||
pagespeedApiKey: z.string().optional(),
|
||||
language: z.string().optional(),
|
||||
timezone: z.string().optional(),
|
||||
systemEmailHost: z.string().optional(),
|
||||
systemEmailAddress: z.string().optional(),
|
||||
systemEmailPassword: z.string().optional(),
|
||||
systemEmailUser: z.string().optional(),
|
||||
systemEmailConnectionHost: z.string().optional(),
|
||||
systemEmailTLSServername: z.string().optional(),
|
||||
|
||||
showURL: z.boolean().optional(),
|
||||
systemEmailSecure: z.boolean().optional(),
|
||||
@@ -28,10 +28,10 @@ export const updateAppSettingsBodyValidation = z
|
||||
|
||||
globalThresholds: z
|
||||
.object({
|
||||
cpu: z.union([z.number().min(1).max(100), z.literal("")]).optional(),
|
||||
memory: z.union([z.number().min(1).max(100), z.literal("")]).optional(),
|
||||
disk: z.union([z.number().min(1).max(100), z.literal("")]).optional(),
|
||||
temperature: z.union([z.number().min(1).max(150), z.literal("")]).optional(),
|
||||
cpu: z.number().min(1).max(100).optional(),
|
||||
memory: z.number().min(1).max(100).optional(),
|
||||
disk: z.number().min(1).max(100).optional(),
|
||||
temperature: z.number().min(1).max(150).optional(),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user