refactor out common dependenceis
This commit is contained in:
1 parent
ddf3e8251d
commit
dc321580c3
20 files changed
+170
-233
No files matched your search
@@ -1,5 +1,4 @@
|
||||
import { createAnnouncementValidation } from "../validation/joi.js";
|
||||
import { asyncHandler } from "../utils/errorUtils.js";
|
||||
|
||||
const SERVICE_NAME = "announcementController";
|
||||
|
||||
@@ -10,10 +9,9 @@ const SERVICE_NAME = "announcementController";
|
||||
* @class AnnouncementController
|
||||
*/
|
||||
|
||||
class AnnouncementController {
|
||||
constructor(db, stringService) {
|
||||
this.db = db;
|
||||
this.stringService = stringService;
|
||||
class AnnouncementController extends BaseController {
|
||||
constructor(commonDependencies) {
|
||||
super(commonDependencies);
|
||||
this.createAnnouncement = this.createAnnouncement.bind(this);
|
||||
this.getAnnouncement = this.getAnnouncement.bind(this);
|
||||
}
|
||||
|
||||
@@ -27,25 +27,19 @@ class AuthController extends BaseController {
|
||||
/**
|
||||
* Creates an instance of AuthController.
|
||||
*
|
||||
* @param {Object} commonDependencies - Common dependencies injected into the controller
|
||||
* @param {Object} dependencies - The dependencies required by the controller
|
||||
* @param {Object} dependencies.db - Database service for data operations
|
||||
* @param {Object} dependencies.settingsService - Service for application settings
|
||||
* @param {Object} dependencies.emailService - Service for email operations
|
||||
* @param {Object} dependencies.jobQueue - Service for job queue operations
|
||||
* @param {Object} dependencies.stringService - Service for string/localization
|
||||
* @param {Object} dependencies.logger - Logger service
|
||||
* @param {Object} dependencies.userService - User business logic service
|
||||
*/
|
||||
constructor({ db, settingsService, emailService, jobQueue, stringService, logger, userService, errorService }) {
|
||||
super();
|
||||
this.db = db;
|
||||
constructor(commonDependencies, { settingsService, emailService, jobQueue, userService }) {
|
||||
super(commonDependencies);
|
||||
this.settingsService = settingsService;
|
||||
this.emailService = emailService;
|
||||
this.jobQueue = jobQueue;
|
||||
this.stringService = stringService;
|
||||
this.logger = logger;
|
||||
this.userService = userService;
|
||||
this.errorService = errorService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,83 @@
|
||||
import { asyncHandler } from "../service/infrastructure/errorService.js";
|
||||
import { AppError } from "../service/infrastructure/errorService.js";
|
||||
|
||||
export const createCommonDependencies = (serviceRegistry, dbServiceName, loggerServiceName, errorServiceName, stringServiceName) => {
|
||||
return {
|
||||
db: serviceRegistry.get(dbServiceName),
|
||||
errorService: serviceRegistry.get(errorServiceName),
|
||||
logger: serviceRegistry.get(loggerServiceName),
|
||||
stringService: serviceRegistry.get(stringServiceName),
|
||||
};
|
||||
};
|
||||
|
||||
class BaseController {
|
||||
constructor() {
|
||||
this.asyncHandler = asyncHandler;
|
||||
constructor({ db, logger, errorService, ...additionalDependencies }) {
|
||||
this.db = db;
|
||||
this.logger = logger;
|
||||
this.errorService = errorService;
|
||||
Object.assign(this, additionalDependencies);
|
||||
|
||||
this.asyncHandler = (fn, serviceName, methodName) => {
|
||||
return async (req, res, next) => {
|
||||
try {
|
||||
await fn(req, res, next);
|
||||
} catch (error) {
|
||||
// Handle validation errors
|
||||
if (error.isJoi) {
|
||||
const validationError = this.errorService.createValidationError(error.message, error.details, serviceName, methodName);
|
||||
return next(validationError);
|
||||
}
|
||||
|
||||
if (error.name === "ValidationError") {
|
||||
const validationError = this.errorService.createValidationError("Database validation failed", error.errors, serviceName, methodName);
|
||||
return next(validationError);
|
||||
}
|
||||
|
||||
if (error.name === "CastError") {
|
||||
const notFoundError = this.errorService.createNotFoundError(
|
||||
"Invalid resource identifier",
|
||||
{ field: error.path, value: error.value },
|
||||
serviceName,
|
||||
methodName
|
||||
);
|
||||
return next(notFoundError);
|
||||
}
|
||||
|
||||
if (error.code === "11000") {
|
||||
const conflictError = this.errorService.createConflictError("Resource already exists", {
|
||||
originalError: error.message,
|
||||
code: error.code,
|
||||
});
|
||||
conflictError.service = serviceName;
|
||||
conflictError.method = methodName;
|
||||
return next(conflictError);
|
||||
}
|
||||
|
||||
if (error instanceof AppError) {
|
||||
error.service = error.service || serviceName;
|
||||
error.method = error.method || methodName;
|
||||
return next(error);
|
||||
}
|
||||
|
||||
if (error.status) {
|
||||
const appError = this.errorService.createError(error.message, error.status, serviceName, methodName, {
|
||||
originalError: error.message,
|
||||
stack: error.stack,
|
||||
});
|
||||
return next(appError);
|
||||
}
|
||||
|
||||
// For unknown errors, create a server error
|
||||
const appError = this.errorService.createServerError(error.message || "An unexpected error occurred", {
|
||||
originalError: error.message,
|
||||
stack: error.stack,
|
||||
});
|
||||
appError.service = serviceName;
|
||||
appError.method = methodName;
|
||||
appError.stack = error.stack; // Preserve original stack
|
||||
return next(appError);
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
export default BaseController;
|
||||
@@ -26,20 +26,15 @@ class CheckController extends BaseController {
|
||||
/**
|
||||
* Creates an instance of CheckController.
|
||||
*
|
||||
* @param {Object} commonDependencies - Common dependencies injected into the controller
|
||||
* @param {Object} dependencies - The dependencies required by the controller
|
||||
* @param {Object} dependencies.db - Database service for data operations
|
||||
* @param {Object} dependencies.settingsService - Service for application settings
|
||||
* @param {Object} dependencies.stringService - Service for string/localization
|
||||
* @param {Object} dependencies.checkService - Check business logic service
|
||||
* @param {Object} dependencies.errorService - Error service for error handling
|
||||
*/
|
||||
constructor({ db, settingsService, stringService, checkService, errorService }) {
|
||||
super();
|
||||
this.db = db;
|
||||
constructor(commonDependencies, { settingsService, checkService }) {
|
||||
super(commonDependencies);
|
||||
this.settingsService = settingsService;
|
||||
this.stringService = stringService;
|
||||
this.checkService = checkService;
|
||||
this.errorService = errorService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,15 +12,13 @@ import BaseController from "./baseController.js";
|
||||
class DiagnosticController extends BaseController {
|
||||
/**
|
||||
* Creates an instance of DiagnosticController.
|
||||
*
|
||||
* @param {Object} commonDependencies - Common dependencies injected into the controller
|
||||
* @param {Object} dependencies - The dependencies required by the controller
|
||||
* @param {Object} dependencies.diagnosticService - Service for system diagnostics and monitoring
|
||||
* @param {Object} dependencies.errorService - Service for error handling
|
||||
*/
|
||||
constructor({ diagnosticService, errorService }) {
|
||||
super();
|
||||
constructor(commonDependencies, { diagnosticService }) {
|
||||
super(commonDependencies);
|
||||
this.diagnosticService = diagnosticService;
|
||||
this.errorService = errorService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,16 +9,12 @@ const SERVICE_NAME = "inviteController";
|
||||
class InviteController extends BaseController {
|
||||
/**
|
||||
* Creates a new InviteController instance
|
||||
* @param {Object} dependencies - Dependencies injected into the controller
|
||||
* @param {Object} dependencies.stringService - Service for internationalized strings
|
||||
* @param {Object} dependencies.inviteServ ice - Service for invite-related operations
|
||||
* @param {Object} dependencies.errorService - Service for error handling
|
||||
* @param {Object} commonDependencies - Common dependencies injected into the controller
|
||||
* @param {Object} dependencies.inviteService - Service for invite-related operations
|
||||
*/
|
||||
constructor({ stringService, inviteService, errorService }) {
|
||||
super();
|
||||
this.stringService = stringService;
|
||||
constructor(commonDependencies, { inviteService }) {
|
||||
super(commonDependencies);
|
||||
this.inviteService = inviteService;
|
||||
this.errorService = errorService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,10 +2,8 @@ import BaseController from "./baseController.js";
|
||||
const SERVICE_NAME = "LogController";
|
||||
|
||||
class LogController extends BaseController {
|
||||
constructor({ logger, errorService }) {
|
||||
super();
|
||||
this.logger = logger;
|
||||
this.errorService = errorService;
|
||||
constructor(commonDependencies) {
|
||||
super(commonDependencies);
|
||||
}
|
||||
|
||||
getLogs = this.asyncHandler(
|
||||
|
||||
@@ -12,14 +12,10 @@ import BaseController from "./baseController.js";
|
||||
const SERVICE_NAME = "maintenanceWindowController";
|
||||
|
||||
class MaintenanceWindowController extends BaseController {
|
||||
constructor({ db, settingsService, stringService, maintenanceWindowService, errorService }) {
|
||||
super();
|
||||
this.db = db;
|
||||
constructor(commonDependencies, { settingsService, maintenanceWindowService }) {
|
||||
super(commonDependencies);
|
||||
this.settingsService = settingsService;
|
||||
this.stringService = stringService;
|
||||
this.maintenanceWindowService = maintenanceWindowService;
|
||||
this.errorService = errorService;
|
||||
this.asyncHandler = errorService.asyncHandler;
|
||||
}
|
||||
|
||||
createMaintenanceWindows = this.asyncHandler(
|
||||
|
||||
@@ -18,16 +18,12 @@ import BaseController from "./baseController.js";
|
||||
|
||||
const SERVICE_NAME = "monitorController";
|
||||
class MonitorController extends BaseController {
|
||||
constructor({ db, settingsService, jobQueue, stringService, emailService, monitorService, errorService }) {
|
||||
super();
|
||||
this.db = db;
|
||||
constructor(commonDependencies, { settingsService, jobQueue, emailService, monitorService }) {
|
||||
super(commonDependencies);
|
||||
this.settingsService = settingsService;
|
||||
this.jobQueue = jobQueue;
|
||||
this.stringService = stringService;
|
||||
this.emailService = emailService;
|
||||
this.monitorService = monitorService;
|
||||
this.errorService = errorService;
|
||||
this.asyncHandler = errorService.asyncHandler;
|
||||
}
|
||||
|
||||
async verifyTeamAccess(teamId, monitorId) {
|
||||
|
||||
@@ -4,14 +4,10 @@ import BaseController from "./baseController.js";
|
||||
const SERVICE_NAME = "NotificationController";
|
||||
|
||||
class NotificationController extends BaseController {
|
||||
constructor({ notificationService, stringService, statusService, db, errorService }) {
|
||||
super();
|
||||
constructor(commonDependencies, { notificationService, statusService }) {
|
||||
super(commonDependencies);
|
||||
this.notificationService = notificationService;
|
||||
this.stringService = stringService;
|
||||
this.statusService = statusService;
|
||||
this.db = db;
|
||||
this.errorService = errorService;
|
||||
this.asyncHandler = errorService.asyncHandler;
|
||||
}
|
||||
|
||||
testNotification = this.asyncHandler(
|
||||
|
||||
@@ -2,11 +2,9 @@ import BaseController from "./baseController.js";
|
||||
const SERVICE_NAME = "JobQueueController";
|
||||
|
||||
class JobQueueController extends BaseController {
|
||||
constructor({ jobQueue, stringService, errorService }) {
|
||||
super();
|
||||
constructor(commonDependencies, { jobQueue }) {
|
||||
super(commonDependencies);
|
||||
this.jobQueue = jobQueue;
|
||||
this.stringService = stringService;
|
||||
this.errorService = errorService;
|
||||
}
|
||||
|
||||
getMetrics = this.asyncHandler(
|
||||
|
||||
@@ -5,13 +5,10 @@ import BaseController from "./baseController.js";
|
||||
const SERVICE_NAME = "SettingsController";
|
||||
|
||||
class SettingsController extends BaseController {
|
||||
constructor({ db, settingsService, stringService, emailService, errorService }) {
|
||||
super();
|
||||
this.db = db;
|
||||
constructor(commonDependencies, { settingsService, emailService }) {
|
||||
super(commonDependencies);
|
||||
this.settingsService = settingsService;
|
||||
this.stringService = stringService;
|
||||
this.emailService = emailService;
|
||||
this.errorService = errorService;
|
||||
}
|
||||
|
||||
buildAppSettings = (dbSettings) => {
|
||||
|
||||
@@ -4,11 +4,8 @@ import BaseController from "./baseController.js";
|
||||
const SERVICE_NAME = "statusPageController";
|
||||
|
||||
class StatusPageController extends BaseController {
|
||||
constructor({ db, stringService, errorService }) {
|
||||
super();
|
||||
this.db = db;
|
||||
this.stringService = stringService;
|
||||
this.errorService = errorService;
|
||||
constructor(commonDependencies) {
|
||||
super(commonDependencies);
|
||||
}
|
||||
|
||||
createStatusPage = this.asyncHandler(
|
||||
|
||||
Reference in new issue
Block a user