refactor out common dependenceis

This commit is contained in:
Alex Holliday committed 2025-07-25 10:16:29 -07:00
1 parent ddf3e8251d
commit dc321580c3
20 files changed
+170 -233

No files matched your search

+18 -37
View File
@@ -3,37 +3,30 @@ const SERVICE_NAME = "checkService";
class CheckService {
static SERVICE_NAME = SERVICE_NAME;
constructor({ db, settingsService, stringService }) {
constructor({ db, settingsService, stringService, errorService }) {
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
this.errorService = errorService;
}
getChecksByMonitor = async ({ monitorId, query, teamId }) => {
if (!monitorId) {
throw new Error("No monitor ID in request");
throw this.errorService.createBadRequestError("No monitor ID in request");
}
if (!teamId) {
throw new Error("No team ID in request");
throw this.errorService.createBadRequestError("No team ID in request");
}
const monitor = await this.db.getMonitorById(monitorId);
if (!monitor) {
const err = new Error("Monitor not found");
err.status = 404;
err.service = SERVICE_NAME;
err.method = "getChecksByMonitor";
throw err;
throw this.errorService.createNotFoundError("Monitor not found");
}
if (!monitor.teamId.equals(teamId)) {
const err = new Error("Unauthorized");
err.status = 403;
err.service = SERVICE_NAME;
err.method = "getChecksByMonitor";
throw err;
throw this.errorService.createAuthorizationError();
}
let { type, sortOrder, dateRange, filter, ack, page, rowsPerPage, status } = query;
@@ -55,7 +48,7 @@ class CheckService {
let { sortOrder, dateRange, filter, ack, page, rowsPerPage } = query;
if (!teamId) {
throw new Error("No team ID in request");
throw this.errorService.createBadRequestError("No team ID in request");
}
const checkData = await this.db.getChecksByTeam({
@@ -72,7 +65,7 @@ class CheckService {
getChecksSummaryByTeamId = async ({ teamId }) => {
if (!teamId) {
throw new Error("No team ID in request");
throw this.errorService.createBadRequestError("No team ID in request");
}
const summary = await this.db.getChecksSummaryByTeamId({ teamId });
@@ -81,11 +74,11 @@ class CheckService {
ackCheck = async ({ checkId, teamId, ack }) => {
if (!checkId) {
throw new Error("No check ID in request");
throw this.errorService.createBadRequestError("No check ID in request");
}
if (!teamId) {
throw new Error("No team ID in request");
throw this.errorService.createBadRequestError("No team ID in request");
}
const updatedCheck = await this.db.ackCheck(checkId, teamId, ack);
@@ -95,20 +88,16 @@ class CheckService {
ackAllChecks = async ({ monitorId, path, teamId, ack }) => {
if (path === "monitor") {
if (!monitorId) {
throw new Error("No monitor ID in request");
throw this.errorService.createBadRequestError("No monitor ID in request");
}
const monitor = await this.db.getMonitorById(monitorId);
if (!monitor) {
throw new Error("Monitor not found");
throw this.errorService.createNotFoundError("Monitor not found");
}
if (!monitor.teamId.equals(teamId)) {
const err = new Error("Unauthorized");
err.status = 403;
err.service = SERVICE_NAME;
err.method = "ackAllChecks";
throw err;
throw this.errorService.createAuthorizationError();
}
}
@@ -118,29 +107,21 @@ class CheckService {
deleteChecks = async ({ monitorId, teamId }) => {
if (!monitorId) {
throw new Error("No monitor ID in request");
throw this.errorService.createBadRequestError("No monitor ID in request");
}
if (!teamId) {
throw new Error("No team ID in request");
throw this.errorService.createBadRequestError("No team ID in request");
}
const monitor = await this.db.getMonitorById(monitorId);
if (!monitor) {
const err = new Error("Monitor not found");
err.status = 404;
err.service = SERVICE_NAME;
err.method = "deleteChecks";
throw err;
throw this.errorService.createNotFoundError("Monitor not found");
}
if (!monitor.teamId.equals(teamId)) {
const err = new Error("Unauthorized");
err.status = 403;
err.service = SERVICE_NAME;
err.method = "deleteChecks";
throw err;
throw this.errorService.createAuthorizationError();
}
const deletedCount = await this.db.deleteChecks(monitorId);
@@ -148,7 +129,7 @@ class CheckService {
};
deleteChecksByTeamId = async ({ teamId }) => {
if (!teamId) {
throw new Error("No team ID in request");
throw this.errorService.createBadRequestError("No team ID in request");
}
const deletedCount = await this.db.deleteChecksByTeamId(teamId);
@@ -2,10 +2,11 @@ const SERVICE_NAME = "maintenanceWindowService";
class MaintenanceWindowService {
static SERVICE_NAME = SERVICE_NAME;
constructor({ db, settingsService, stringService }) {
constructor({ db, settingsService, stringService, errorService }) {
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
this.errorService = errorService;
}
createMaintenanceWindow = async ({ teamId, body }) => {
@@ -15,11 +16,7 @@ class MaintenanceWindowService {
const unauthorizedMonitors = monitors.filter((monitor) => !monitor.teamId.equals(teamId));
if (unauthorizedMonitors.length > 0) {
const error = new Error("Unauthorized access to one or more monitors");
error.status = 403;
error.service = SERVICE_NAME;
error.method = "createMaintenanceWindows";
throw error;
throw this.errorService.createAuthorizationError();
}
const dbTransactions = monitorIds.map((monitorId) => {
@@ -13,7 +13,6 @@ class MonitorService {
this.papaparse = papaparse;
this.logger = logger;
this.errorService = errorService;
this.asyncHandler = errorService.asyncHandler;
}
verifyTeamAccess = async ({ teamId, monitorId }) => {
+4 -4
View File
@@ -154,23 +154,23 @@ class UserService {
deleteUser = async (user) => {
const email = user?.email;
if (!email) {
throw new Error("No email in request");
throw this.errorService.createBadRequestError("No email in request");
}
const teamId = user?.teamId;
const userId = user?._id;
if (!teamId) {
throw new Error("No team ID in request");
throw this.errorService.createBadRequestError("No team ID in request");
}
if (!userId) {
throw new Error("No user ID in request");
throw this.errorService.createBadRequestError("No user ID in request");
}
const roles = user?.role;
if (roles.includes("demo")) {
throw new Error("Demo user cannot be deleted");
throw this.errorService.createBadRequestError("Demo user cannot be deleted");
}
// 1. Find all the monitors associated with the team ID if superadmin
+1 -64
View File
@@ -1,4 +1,4 @@
class AppError extends Error {
export class AppError extends Error {
constructor(message, status = 500, service = null, method = null, details = null) {
super(message);
this.status = status;
@@ -96,66 +96,3 @@ class ErrorService {
}
export default ErrorService;
export const 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.createValidationError(error.message, error.details, serviceName, methodName);
return next(validationError);
}
if (error.name === "ValidationError") {
const validationError = this.createValidationError("Database validation failed", error.errors, serviceName, methodName);
return next(validationError);
}
if (error.name === "CastError") {
const notFoundError = this.createNotFoundError(
"Invalid resource identifier",
{ field: error.path, value: error.value },
serviceName,
methodName
);
return next(notFoundError);
}
if (error.code === "11000") {
const conflictError = this.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.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.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);
}
};
};