From e8908351a27c0b0292df44aa96e8ebc185b40a39 Mon Sep 17 00:00:00 2001 From: Br0wnHammer Date: Fri, 20 Jun 2025 15:39:13 +0530 Subject: [PATCH] Route for Updating a single Incident Status --- .../Components/IncidentTable/index.jsx | 4 +-- client/src/Utils/NetworkService.js | 18 +++++++++++ server/controllers/checkController.js | 27 ++++++++++++++++- server/db/mongo/modules/checkModule.js | 30 +++++++++++++++++-- server/routes/checkRoute.js | 2 ++ server/validation/joi.js | 5 ++++ 6 files changed, 81 insertions(+), 5 deletions(-) diff --git a/client/src/Pages/Incidents/Components/IncidentTable/index.jsx b/client/src/Pages/Incidents/Components/IncidentTable/index.jsx index b3e2d2f30..b4bfbe9dc 100644 --- a/client/src/Pages/Incidents/Components/IncidentTable/index.jsx +++ b/client/src/Pages/Incidents/Components/IncidentTable/index.jsx @@ -42,7 +42,7 @@ const IncidentTable = ({ sortOrder: "desc", limit: null, dateRange, - filter: filter === "resolved" ? "resolve" : filter, + filter: filter === "resolved" ? "all" : filter, page: page, rowsPerPage: rowsPerPage, enabled: selectedMonitor !== "0", @@ -54,7 +54,7 @@ const IncidentTable = ({ sortOrder: "desc", limit: null, dateRange, - filter: filter === "resolved" ? "resolve" : filter, + filter: filter === "resolved" ? "all" : filter, page: page, rowsPerPage: rowsPerPage, enabled: selectedMonitor === "0", diff --git a/client/src/Utils/NetworkService.js b/client/src/Utils/NetworkService.js index eee795268..8625b69e3 100644 --- a/client/src/Utils/NetworkService.js +++ b/client/src/Utils/NetworkService.js @@ -598,6 +598,24 @@ class NetworkService { return this.axiosInstance.get(`/checks/team?${params.toString()}`); }; + /** + * ************************************ + * Update the status of a check + * ************************************ + * + * @async + * @param {Object} config - The configuration object. + * @param {string} config.checkId - The ID of the check to update. + * @param {boolean} config.status - The status to update the check to. + * @returns {Promise} The response from the axios PUT request. + * + */ + async updateCheckStatus(config) { + return this.axiosInstance.put(`/checks/${config.checkId}`, { + status: config.status, + }); + } + /** * ************************************ * Get all checks for a given user diff --git a/server/controllers/checkController.js b/server/controllers/checkController.js index 387839beb..0dece0a6d 100755 --- a/server/controllers/checkController.js +++ b/server/controllers/checkController.js @@ -8,6 +8,7 @@ import { deleteChecksParamValidation, deleteChecksByTeamIdParamValidation, updateChecksTTLBodyValidation, + updateCheckStatusBodyValidation, } from "../validation/joi.js"; import jwt from "jsonwebtoken"; import { getTokenFromHeaders } from "../utils/utils.js"; @@ -85,7 +86,7 @@ class CheckController { return; } try { - let { sortOrder, dateRange, filter, page, rowsPerPage } = req.query; + let { sortOrder, dateRange, filter, page, rowsPerPage, status } = req.query; const { teamId } = req.user; const checkData = await this.db.getChecksByTeam({ @@ -95,6 +96,7 @@ class CheckController { page, rowsPerPage, teamId, + status, }); return res.success({ msg: this.stringService.checkGet, @@ -105,6 +107,29 @@ class CheckController { } }; + updateCheckStatus = async (req, res, next) => { + try { + await updateCheckStatusBodyValidation.validateAsync(req.body); + } catch (error) { + next(handleValidationError(error, SERVICE_NAME)); + return; + } + + try { + const { checkId } = req.params; + const { status } = req.body; + + const updatedCheck = await this.db.updateCheckStatus(checkId, status); + + return res.success({ + msg: this.stringService.checkUpdateStatus, + data: updatedCheck, + }); + } catch (error) { + next(handleError(error, SERVICE_NAME, "updateCheckStatus")); + } + }; + 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 f409fd3d2..97f088953 100755 --- a/server/db/mongo/modules/checkModule.js +++ b/server/db/mongo/modules/checkModule.js @@ -68,7 +68,7 @@ const getChecksByMonitor = async ({ status, }) => { try { - status = typeof status !== "undefined" ? false : undefined; + status = status === "true" ? true : status === "false" ? false : undefined; page = parseInt(page); rowsPerPage = parseInt(rowsPerPage); // Match @@ -156,13 +156,15 @@ const getChecksByTeam = async ({ page, rowsPerPage, teamId, + status, }) => { try { + status = status === "true" ? true : false; page = parseInt(page); rowsPerPage = parseInt(rowsPerPage); const matchStage = { teamId: ObjectId.createFromHexString(teamId), - status: false, + status: status, ...(dateRangeLookup[dateRange] && { createdAt: { $gte: dateRangeLookup[dateRange], @@ -236,6 +238,29 @@ const getChecksByTeam = async ({ } }; +/** + * Update the status of a check + * @async + * @param {string} checkId + * @param {boolean} status + * @returns {Promise} + * @throws {Error} + */ +const updateCheckStatus = async (checkId, status) => { + try { + const updatedCheck = await Check.findOneAndUpdate( + { _id: checkId }, + { status }, + { new: true } + ); + return updatedCheck; + } catch (error) { + error.service = SERVICE_NAME; + error.method = "updateCheckStatus"; + throw error; + } +}; + /** * Delete all checks for a monitor * @async @@ -317,6 +342,7 @@ export { createChecks, getChecksByMonitor, getChecksByTeam, + updateCheckStatus, deleteChecks, deleteChecksByTeamId, updateChecksTTL, diff --git a/server/routes/checkRoute.js b/server/routes/checkRoute.js index aedca991a..d2b0590a0 100755 --- a/server/routes/checkRoute.js +++ b/server/routes/checkRoute.js @@ -20,6 +20,8 @@ class CheckRoutes { this.router.get("/:monitorId", this.checkController.getChecksByMonitor); + this.router.put("/:checkId", this.checkController.updateCheckStatus); + this.router.post( "/:monitorId", verifyOwnership(Monitor, "monitorId"), diff --git a/server/validation/joi.js b/server/validation/joi.js index e684cefa2..3c893260d 100755 --- a/server/validation/joi.js +++ b/server/validation/joi.js @@ -289,6 +289,10 @@ const createCheckBodyValidation = joi.object({ message: joi.string().required(), }); +const updateCheckStatusBodyValidation = joi.object({ + status: joi.boolean(), +}); + const getChecksParamValidation = joi.object({ monitorId: joi.string().required(), }); @@ -662,6 +666,7 @@ export { getChecksQueryValidation, getTeamChecksParamValidation, getTeamChecksQueryValidation, + updateCheckStatusBodyValidation, deleteChecksParamValidation, deleteChecksByTeamIdParamValidation, updateChecksTTLBodyValidation,