js->ts
This commit is contained in:
1 parent
846f1dfa6c
commit
7b2ff593a4
2 files changed
+39
-42
No files matched your search
@@ -30,10 +30,7 @@ export const initializeControllers = (services) => {
|
|||||||
|
|
||||||
controllers.monitorController = new MonitorController(services.monitorService);
|
controllers.monitorController = new MonitorController(services.monitorService);
|
||||||
|
|
||||||
controllers.settingsController = new SettingsController(commonDependencies, {
|
controllers.settingsController = new SettingsController(services.settingsService, services.emailService, services.db);
|
||||||
settingsService: services.settingsService,
|
|
||||||
emailService: services.emailService,
|
|
||||||
});
|
|
||||||
controllers.checkController = new CheckController(commonDependencies, {
|
controllers.checkController = new CheckController(commonDependencies, {
|
||||||
settingsService: services.settingsService,
|
settingsService: services.settingsService,
|
||||||
checkService: services.checkService,
|
checkService: services.checkService,
|
||||||
|
|||||||
+38
-38
@@ -1,28 +1,32 @@
|
|||||||
import { updateAppSettingsBodyValidation } from "../../validation/joi.js";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { sendTestEmailBodyValidation } from "../../validation/joi.js";
|
import { sendTestEmailBodyValidation, updateAppSettingsBodyValidation } from "@/validation/joi.js";
|
||||||
import BaseController from "./baseController.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "SettingsController";
|
const SERVICE_NAME = "SettingsController";
|
||||||
|
|
||||||
class SettingsController extends BaseController {
|
class SettingsController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
constructor(commonDependencies, { settingsService, emailService }) {
|
private settingsService: any;
|
||||||
super(commonDependencies);
|
private emailService: any;
|
||||||
|
private db: any;
|
||||||
|
constructor(settingsService: any, emailService: any, db: any) {
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
return SettingsController.SERVICE_NAME;
|
return SettingsController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
buildAppSettings = (dbSettings) => {
|
buildAppSettings = (dbSettings: any) => {
|
||||||
const sanitizedSettings = { ...dbSettings };
|
const sanitizedSettings = { ...dbSettings };
|
||||||
delete sanitizedSettings.version;
|
delete sanitizedSettings.version;
|
||||||
|
|
||||||
const returnSettings = {
|
const returnSettings = {
|
||||||
pagespeedKeySet: false,
|
pagespeedKeySet: false,
|
||||||
emailPasswordSet: false,
|
emailPasswordSet: false,
|
||||||
|
settings: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof sanitizedSettings.pagespeedApiKey !== "undefined") {
|
if (typeof sanitizedSettings.pagespeedApiKey !== "undefined") {
|
||||||
@@ -37,37 +41,33 @@ class SettingsController extends BaseController {
|
|||||||
return returnSettings;
|
return returnSettings;
|
||||||
};
|
};
|
||||||
|
|
||||||
getAppSettings = this.asyncHandler(
|
getAppSettings = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
async (req, res) => {
|
try {
|
||||||
const dbSettings = await this.settingsService.getDBSettings();
|
const dbSettings = await this.settingsService.getDBSettings();
|
||||||
|
|
||||||
const returnSettings = this.buildAppSettings(dbSettings);
|
const returnSettings = this.buildAppSettings(dbSettings);
|
||||||
return res.success({
|
return res.status(200).json({
|
||||||
msg: this.stringService.getAppSettings,
|
msg: "App settings fetched successfully",
|
||||||
data: returnSettings,
|
data: returnSettings,
|
||||||
});
|
});
|
||||||
},
|
} catch (error) {
|
||||||
SERVICE_NAME,
|
next(error);
|
||||||
"getAppSettings"
|
}
|
||||||
);
|
};
|
||||||
|
|
||||||
updateAppSettings = this.asyncHandler(
|
updateAppSettings = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
async (req, res) => {
|
await updateAppSettingsBodyValidation.validateAsync(req.body);
|
||||||
await updateAppSettingsBodyValidation.validateAsync(req.body);
|
|
||||||
|
|
||||||
const updatedSettings = await this.db.settingsModule.updateAppSettings(req.body);
|
const updatedSettings = await this.db.settingsModule.updateAppSettings(req.body);
|
||||||
const returnSettings = this.buildAppSettings(updatedSettings);
|
const returnSettings = this.buildAppSettings(updatedSettings);
|
||||||
return res.success({
|
return res.status(200).json({
|
||||||
msg: this.stringService.updateAppSettings,
|
msg: "App settings updated successfully",
|
||||||
data: returnSettings,
|
data: returnSettings,
|
||||||
});
|
});
|
||||||
},
|
};
|
||||||
SERVICE_NAME,
|
|
||||||
"updateAppSettings"
|
|
||||||
);
|
|
||||||
|
|
||||||
sendTestEmail = this.asyncHandler(
|
sendTestEmail = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
async (req, res) => {
|
try {
|
||||||
await sendTestEmailBodyValidation.validateAsync(req.body);
|
await sendTestEmailBodyValidation.validateAsync(req.body);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -86,7 +86,7 @@ class SettingsController extends BaseController {
|
|||||||
systemEmailTLSServername,
|
systemEmailTLSServername,
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
const subject = this.stringService.testEmailSubject;
|
const subject = "This is a test email from Checkmate";
|
||||||
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);
|
||||||
@@ -106,17 +106,17 @@ class SettingsController extends BaseController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!messageId) {
|
if (!messageId) {
|
||||||
throw this.errorService.createServerError("Failed to send test email.");
|
throw new AppError({ message: "Failed to send test email.", status: 500 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.success({
|
return res.status(200).json({
|
||||||
msg: this.stringService.sendTestEmail,
|
msg: "Test email sent successfully",
|
||||||
data: { messageId },
|
data: { messageId },
|
||||||
});
|
});
|
||||||
},
|
} catch (error) {
|
||||||
SERVICE_NAME,
|
next(error);
|
||||||
"sendTestEmail"
|
}
|
||||||
);
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SettingsController;
|
export default SettingsController;
|
||||||
Reference in new issue
Block a user