add an error service, base controller

This commit is contained in:
Alex Holliday committed 2025-07-24 15:06:46 -07:00
1 parent 0cfe9d60fb
commit ddf3e8251d
20 files changed
+412 -301

No files matched your search

+13 -16
View File
@@ -1,15 +1,17 @@
import { updateAppSettingsBodyValidation } from "../validation/joi.js";
import { sendTestEmailBodyValidation } from "../validation/joi.js";
import { asyncHandler, createServerError } from "../utils/errorUtils.js";
import BaseController from "./baseController.js";
const SERVICE_NAME = "SettingsController";
class SettingsController {
constructor({ db, settingsService, stringService, emailService }) {
class SettingsController extends BaseController {
constructor({ db, settingsService, stringService, emailService, errorService }) {
super();
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
this.emailService = emailService;
this.errorService = errorService;
}
buildAppSettings = (dbSettings) => {
@@ -33,8 +35,8 @@ class SettingsController {
return returnSettings;
};
getAppSettings = asyncHandler(
async (req, res, next) => {
getAppSettings = this.asyncHandler(
async (req, res) => {
const dbSettings = await this.settingsService.getDBSettings();
const returnSettings = this.buildAppSettings(dbSettings);
@@ -47,8 +49,8 @@ class SettingsController {
"getAppSettings"
);
updateAppSettings = asyncHandler(
async (req, res, next) => {
updateAppSettings = this.asyncHandler(
async (req, res) => {
await updateAppSettingsBodyValidation.validateAsync(req.body);
const updatedSettings = await this.db.updateAppSettings(req.body);
@@ -62,14 +64,9 @@ class SettingsController {
"updateAppSettings"
);
sendTestEmail = asyncHandler(
async (req, res, next) => {
try {
await sendTestEmailBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
sendTestEmail = this.asyncHandler(
async (req, res) => {
await sendTestEmailBodyValidation.validateAsync(req.body);
const {
to,
@@ -107,7 +104,7 @@ class SettingsController {
});
if (!messageId) {
throw createServerError("Failed to send test email.");
throw this.errorService.createServerError("Failed to send test email.");
}
return res.success({