From a9c1d131a5c417d2d1eef9462f3b7623769ed6ce Mon Sep 17 00:00:00 2001 From: Br0wnHammer Date: Fri, 20 Jun 2025 16:11:22 +0530 Subject: [PATCH] Route Update All Monitor/Team Checks --- server/controllers/checkController.js | 30 ++++++++++++++++++++++++++ server/db/mongo/modules/checkModule.js | 24 +++++++++++++++++++++ server/routes/checkRoute.js | 2 ++ server/validation/joi.js | 12 +++++++++++ 4 files changed, 68 insertions(+) diff --git a/server/controllers/checkController.js b/server/controllers/checkController.js index 0dece0a6d..230d58d0a 100755 --- a/server/controllers/checkController.js +++ b/server/controllers/checkController.js @@ -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); diff --git a/server/db/mongo/modules/checkModule.js b/server/db/mongo/modules/checkModule.js index 97f088953..9cae8ed68 100755 --- a/server/db/mongo/modules/checkModule.js +++ b/server/db/mongo/modules/checkModule.js @@ -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} + * @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, diff --git a/server/routes/checkRoute.js b/server/routes/checkRoute.js index d2b0590a0..c6d0c584f 100755 --- a/server/routes/checkRoute.js +++ b/server/routes/checkRoute.js @@ -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"), diff --git a/server/validation/joi.js b/server/validation/joi.js index 3c893260d..f8e57dc58 100755 --- a/server/validation/joi.js +++ b/server/validation/joi.js @@ -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,