add error handling util, update middleware and controllers

This commit is contained in:
Alex Holliday committed 2025-07-15 16:33:35 -07:00
1 parent c54feb9306
commit 38bb915144
15 files changed
+683 -852

No files matched your search

+57 -99
View File
@@ -12,9 +12,7 @@ import {
ackAllChecksParamValidation,
ackAllChecksBodyValidation,
} from "../validation/joi.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js";
import { handleValidationError, handleError } from "./controllerUtils.js";
import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "checkController";
@@ -25,16 +23,11 @@ class CheckController {
this.stringService = stringService;
}
createCheck = async (req, res, next) => {
try {
createCheck = asyncHandler(
async (req, res, next) => {
await createCheckParamValidation.validateAsync(req.params);
await createCheckBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const checkData = { ...req.body };
const check = await this.db.createCheck(checkData);
@@ -42,21 +35,16 @@ class CheckController {
msg: this.stringService.checkCreate,
data: check,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "createCheck"));
}
};
},
SERVICE_NAME,
"createCheck"
);
getChecksByMonitor = async (req, res, next) => {
try {
getChecksByMonitor = asyncHandler(
async (req, res, next) => {
await getChecksParamValidation.validateAsync(req.params);
await getChecksQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId } = req.params;
let { type, sortOrder, dateRange, filter, ack, page, rowsPerPage, status } =
req.query;
@@ -76,20 +64,16 @@ class CheckController {
msg: this.stringService.checkGet,
data: result,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getChecks"));
}
};
},
SERVICE_NAME,
"getChecksByMonitor"
);
getChecksByTeam = async (req, res, next) => {
try {
getChecksByTeam = asyncHandler(
async (req, res, next) => {
await getTeamChecksParamValidation.validateAsync(req.params);
await getTeamChecksQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
let { sortOrder, dateRange, filter, ack, page, rowsPerPage } = req.query;
const { teamId } = req.user;
@@ -106,33 +90,28 @@ class CheckController {
msg: this.stringService.checkGet,
data: checkData,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getTeamChecks"));
}
};
},
SERVICE_NAME,
"getChecksByTeam"
);
getChecksSummaryByTeamId = async (req, res, next) => {
try {
getChecksSummaryByTeamId = asyncHandler(
async (req, res, next) => {
const { teamId } = req.user;
const summary = await this.db.getChecksSummaryByTeamId({ teamId });
return res.success({
msg: this.stringService.checkGetSummary,
data: summary,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getChecksSummaryByTeamId"));
}
};
},
SERVICE_NAME,
"getChecksSummaryByTeamId"
);
ackCheck = async (req, res, next) => {
try {
ackCheck = asyncHandler(
async (req, res, next) => {
await ackCheckBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { checkId } = req.params;
const { ack } = req.body;
const { teamId } = req.user;
@@ -143,21 +122,16 @@ class CheckController {
msg: this.stringService.checkUpdateStatus,
data: updatedCheck,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "ackCheck"));
}
};
},
SERVICE_NAME,
"ackCheck"
);
ackAllChecks = async (req, res, next) => {
try {
ackAllChecks = asyncHandler(
async (req, res, next) => {
await ackAllChecksParamValidation.validateAsync(req.params);
await ackAllChecksBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId, path } = req.params;
const { ack } = req.body;
const { teamId } = req.user;
@@ -168,40 +142,30 @@ class CheckController {
msg: this.stringService.checkUpdateStatus,
data: updatedChecks,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "ackAllChecks"));
}
};
},
SERVICE_NAME,
"ackAllChecks"
);
deleteChecks = async (req, res, next) => {
try {
deleteChecks = asyncHandler(
async (req, res, next) => {
await deleteChecksParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const deletedCount = await this.db.deleteChecks(req.params.monitorId);
return res.success({
msg: this.stringService.checkDelete,
data: { deletedCount },
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteChecks"));
}
};
},
SERVICE_NAME,
"deleteChecks"
);
deleteChecksByTeamId = async (req, res, next) => {
try {
deleteChecksByTeamId = asyncHandler(
async (req, res, next) => {
await deleteChecksByTeamIdParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { teamId } = req.user;
const deletedCount = await this.db.deleteChecksByTeamId(teamId);
@@ -209,23 +173,17 @@ class CheckController {
msg: this.stringService.checkDelete,
data: { deletedCount },
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteChecksByTeamId"));
}
};
},
SERVICE_NAME,
"deleteChecksByTeamId"
);
updateChecksTTL = async (req, res, next) => {
const SECONDS_PER_DAY = 86400;
updateChecksTTL = asyncHandler(
async (req, res, next) => {
const SECONDS_PER_DAY = 86400;
try {
await updateChecksTTLBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
// Get user's teamId
const { teamId } = req.user;
const ttl = parseInt(req.body.ttl, 10) * SECONDS_PER_DAY;
await this.db.updateChecksTTL(teamId, ttl);
@@ -233,9 +191,9 @@ class CheckController {
return res.success({
msg: this.stringService.checkUpdateTTL,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "updateTTL"));
}
};
},
SERVICE_NAME,
"updateChecksTtl"
);
}
export default CheckController;