Files
Checkmate/server/controllers/checkController.js
T

199 lines
4.6 KiB
JavaScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
import {
createCheckParamValidation,
createCheckBodyValidation,
getChecksParamValidation,
getChecksQueryValidation,
getTeamChecksParamValidation,
getTeamChecksQueryValidation,
deleteChecksParamValidation,
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,
2025-06-25 11:52:38 +05:30
ackCheckBodyValidation,
ackAllChecksParamValidation,
ackAllChecksBodyValidation,
2025-04-20 11:29:53 -07:00
} from "../validation/joi.js";
import { asyncHandler } from "../utils/errorUtils.js";
2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "checkController";
class CheckController {
constructor(db, settingsService, stringService) {
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
}
createCheck = asyncHandler(
async (req, res, next) => {
2025-04-20 11:29:53 -07:00
await createCheckParamValidation.validateAsync(req.params);
await createCheckBodyValidation.validateAsync(req.body);
const checkData = { ...req.body };
const check = await this.db.createCheck(checkData);
return res.success({
msg: this.stringService.checkCreate,
data: check,
});
},
SERVICE_NAME,
"createCheck"
);
2025-04-20 11:29:53 -07:00
getChecksByMonitor = asyncHandler(
async (req, res, next) => {
2025-04-20 11:29:53 -07:00
await getChecksParamValidation.validateAsync(req.params);
await getChecksQueryValidation.validateAsync(req.query);
2025-06-13 12:04:39 +08:00
const { monitorId } = req.params;
2025-06-24 14:44:02 +05:30
let { type, sortOrder, dateRange, filter, ack, page, rowsPerPage, status } =
req.query;
2025-06-13 12:04:39 +08:00
const result = await this.db.getChecksByMonitor({
monitorId,
type,
sortOrder,
dateRange,
filter,
2025-06-24 14:44:02 +05:30
ack,
2025-06-13 12:04:39 +08:00
page,
rowsPerPage,
status,
});
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.checkGet,
data: result,
});
},
SERVICE_NAME,
"getChecksByMonitor"
);
2025-04-20 11:29:53 -07:00
getChecksByTeam = asyncHandler(
async (req, res, next) => {
2025-04-20 11:29:53 -07:00
await getTeamChecksParamValidation.validateAsync(req.params);
await getTeamChecksQueryValidation.validateAsync(req.query);
2025-06-25 14:16:00 +05:30
let { sortOrder, dateRange, filter, ack, page, rowsPerPage } = req.query;
2025-06-13 12:04:39 +08:00
const { teamId } = req.user;
const checkData = await this.db.getChecksByTeam({
sortOrder,
dateRange,
filter,
2025-06-24 14:44:02 +05:30
ack,
2025-06-13 12:04:39 +08:00
page,
rowsPerPage,
teamId,
});
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.checkGet,
data: checkData,
});
},
SERVICE_NAME,
"getChecksByTeam"
);
2025-04-20 11:29:53 -07:00
getChecksSummaryByTeamId = asyncHandler(
async (req, res, next) => {
2025-07-01 18:10:35 +05:30
const { teamId } = req.user;
const summary = await this.db.getChecksSummaryByTeamId({ teamId });
return res.success({
msg: this.stringService.checkGetSummary,
data: summary,
});
},
SERVICE_NAME,
"getChecksSummaryByTeamId"
);
2025-07-01 18:10:35 +05:30
ackCheck = asyncHandler(
async (req, res, next) => {
2025-06-25 11:52:38 +05:30
await ackCheckBodyValidation.validateAsync(req.body);
2025-06-20 15:39:13 +05:30
const { checkId } = req.params;
2025-06-23 22:07:03 +05:30
const { ack } = req.body;
2025-06-24 14:44:02 +05:30
const { teamId } = req.user;
2025-06-20 15:39:13 +05:30
2025-06-24 14:44:02 +05:30
const updatedCheck = await this.db.ackCheck(checkId, teamId, ack);
2025-06-20 15:39:13 +05:30
return res.success({
msg: this.stringService.checkUpdateStatus,
data: updatedCheck,
});
},
SERVICE_NAME,
"ackCheck"
);
2025-06-20 15:39:13 +05:30
ackAllChecks = asyncHandler(
async (req, res, next) => {
2025-06-25 11:52:38 +05:30
await ackAllChecksParamValidation.validateAsync(req.params);
await ackAllChecksBodyValidation.validateAsync(req.body);
2025-06-20 16:11:22 +05:30
2025-06-24 14:44:02 +05:30
const { monitorId, path } = req.params;
2025-06-23 22:07:03 +05:30
const { ack } = req.body;
2025-06-24 14:44:02 +05:30
const { teamId } = req.user;
2025-06-20 16:11:22 +05:30
2025-06-24 14:44:02 +05:30
const updatedChecks = await this.db.ackAllChecks(monitorId, teamId, ack, path);
2025-06-20 16:11:22 +05:30
return res.success({
msg: this.stringService.checkUpdateStatus,
data: updatedChecks,
});
},
SERVICE_NAME,
"ackAllChecks"
);
2025-06-20 16:11:22 +05:30
deleteChecks = asyncHandler(
async (req, res, next) => {
2025-04-20 11:29:53 -07:00
await deleteChecksParamValidation.validateAsync(req.params);
const deletedCount = await this.db.deleteChecks(req.params.monitorId);
return res.success({
msg: this.stringService.checkDelete,
data: { deletedCount },
});
},
SERVICE_NAME,
"deleteChecks"
);
2025-04-20 11:29:53 -07:00
deleteChecksByTeamId = asyncHandler(
async (req, res, next) => {
2025-04-20 11:29:53 -07:00
await deleteChecksByTeamIdParamValidation.validateAsync(req.params);
2025-06-13 12:04:39 +08:00
const { teamId } = req.user;
const deletedCount = await this.db.deleteChecksByTeamId(teamId);
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.checkDelete,
data: { deletedCount },
});
},
SERVICE_NAME,
"deleteChecksByTeamId"
);
2025-04-20 11:29:53 -07:00
updateChecksTTL = asyncHandler(
async (req, res, next) => {
const SECONDS_PER_DAY = 86400;
2025-04-20 11:29:53 -07:00
await updateChecksTTLBodyValidation.validateAsync(req.body);
2025-06-13 12:04:39 +08:00
const { teamId } = req.user;
2025-04-20 11:29:53 -07:00
const ttl = parseInt(req.body.ttl, 10) * SECONDS_PER_DAY;
await this.db.updateChecksTTL(teamId, ttl);
return res.success({
msg: this.stringService.checkUpdateTTL,
});
},
SERVICE_NAME,
"updateChecksTtl"
);
2025-04-20 11:29:53 -07:00
}
export default CheckController;