Route Update All Monitor/Team Checks

This commit is contained in:
Br0wnHammer committed 2025-06-20 16:11:22 +05:30
1 parent e8908351a2
commit a9c1d131a5
4 files changed
+68

No files matched your search

+30
View File
@@ -9,6 +9,8 @@ import {
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,
updateCheckStatusBodyValidation,
updateAllChecksStatusBodyValidation,
updateCheckStatusParamValidation,
} from "../validation/joi.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js";
@@ -130,6 +132,34 @@ class CheckController {
}
};
updateAllChecksStatus = async (req, res, next) => {
try {
await updateCheckStatusParamValidation.validateAsync(req.params);
await updateAllChecksStatusBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId, teamId, target } = req.params;
const { status } = req.body;
const updatedChecks = await this.db.updateAllChecksStatus(
target === "monitor" ? monitorId : teamId,
status,
target
);
return res.success({
msg: this.stringService.checkUpdateStatus,
data: updatedChecks,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "updateAllChecksStatus"));
}
};
deleteChecks = async (req, res, next) => {
try {
await deleteChecksParamValidation.validateAsync(req.params);
+24
View File
@@ -261,6 +261,29 @@ const updateCheckStatus = async (checkId, status) => {
}
};
/**
* Update the status of all checks for a monitor or team
* @async
* @param {string} id
* @param {boolean} status
* @param {string} target
* @returns {Promise<number>}
* @throws {Error}
*/
const updateAllChecksStatus = async (id, status, target) => {
try {
const updatedChecks = await Check.updateMany(
target === "monitor" ? { monitorId: id } : { teamId: id },
{ $set: { status: true } }
);
return updatedChecks.modifiedCount;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "updateAllChecksStatus";
throw error;
}
};
/**
* Delete all checks for a monitor
* @async
@@ -343,6 +366,7 @@ export {
getChecksByMonitor,
getChecksByTeam,
updateCheckStatus,
updateAllChecksStatus,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
+2
View File
@@ -22,6 +22,8 @@ class CheckRoutes {
this.router.put("/:checkId", this.checkController.updateCheckStatus);
this.router.put("/:monitorId/:target", this.checkController.updateAllChecksStatus);
this.router.post(
"/:monitorId",
verifyOwnership(Monitor, "monitorId"),
+12
View File
@@ -293,6 +293,16 @@ const updateCheckStatusBodyValidation = joi.object({
status: joi.boolean(),
});
const updateCheckStatusParamValidation = joi.object({
monitorId: joi.string(),
teamId: joi.string(),
target: joi.string().allow("monitor", "team"),
});
const updateAllChecksStatusBodyValidation = joi.object({
status: joi.boolean(),
});
const getChecksParamValidation = joi.object({
monitorId: joi.string().required(),
});
@@ -667,6 +677,8 @@ export {
getTeamChecksParamValidation,
getTeamChecksQueryValidation,
updateCheckStatusBodyValidation,
updateAllChecksStatusBodyValidation,
updateCheckStatusParamValidation,
deleteChecksParamValidation,
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,