Route Update All Monitor/Team Checks
This commit is contained in:
1 parent
e8908351a2
commit
a9c1d131a5
4 files changed
+68
No files matched your search
@@ -9,6 +9,8 @@ import {
|
|||||||
deleteChecksByTeamIdParamValidation,
|
deleteChecksByTeamIdParamValidation,
|
||||||
updateChecksTTLBodyValidation,
|
updateChecksTTLBodyValidation,
|
||||||
updateCheckStatusBodyValidation,
|
updateCheckStatusBodyValidation,
|
||||||
|
updateAllChecksStatusBodyValidation,
|
||||||
|
updateCheckStatusParamValidation,
|
||||||
} from "../validation/joi.js";
|
} from "../validation/joi.js";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { getTokenFromHeaders } from "../utils/utils.js";
|
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) => {
|
deleteChecks = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await deleteChecksParamValidation.validateAsync(req.params);
|
await deleteChecksParamValidation.validateAsync(req.params);
|
||||||
|
|||||||
@@ -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
|
* Delete all checks for a monitor
|
||||||
* @async
|
* @async
|
||||||
@@ -343,6 +366,7 @@ export {
|
|||||||
getChecksByMonitor,
|
getChecksByMonitor,
|
||||||
getChecksByTeam,
|
getChecksByTeam,
|
||||||
updateCheckStatus,
|
updateCheckStatus,
|
||||||
|
updateAllChecksStatus,
|
||||||
deleteChecks,
|
deleteChecks,
|
||||||
deleteChecksByTeamId,
|
deleteChecksByTeamId,
|
||||||
updateChecksTTL,
|
updateChecksTTL,
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ class CheckRoutes {
|
|||||||
|
|
||||||
this.router.put("/:checkId", this.checkController.updateCheckStatus);
|
this.router.put("/:checkId", this.checkController.updateCheckStatus);
|
||||||
|
|
||||||
|
this.router.put("/:monitorId/:target", this.checkController.updateAllChecksStatus);
|
||||||
|
|
||||||
this.router.post(
|
this.router.post(
|
||||||
"/:monitorId",
|
"/:monitorId",
|
||||||
verifyOwnership(Monitor, "monitorId"),
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
|
|||||||
@@ -293,6 +293,16 @@ const updateCheckStatusBodyValidation = joi.object({
|
|||||||
status: joi.boolean(),
|
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({
|
const getChecksParamValidation = joi.object({
|
||||||
monitorId: joi.string().required(),
|
monitorId: joi.string().required(),
|
||||||
});
|
});
|
||||||
@@ -667,6 +677,8 @@ export {
|
|||||||
getTeamChecksParamValidation,
|
getTeamChecksParamValidation,
|
||||||
getTeamChecksQueryValidation,
|
getTeamChecksQueryValidation,
|
||||||
updateCheckStatusBodyValidation,
|
updateCheckStatusBodyValidation,
|
||||||
|
updateAllChecksStatusBodyValidation,
|
||||||
|
updateCheckStatusParamValidation,
|
||||||
deleteChecksParamValidation,
|
deleteChecksParamValidation,
|
||||||
deleteChecksByTeamIdParamValidation,
|
deleteChecksByTeamIdParamValidation,
|
||||||
updateChecksTTLBodyValidation,
|
updateChecksTTLBodyValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user