Add validation for updateTTL body

This commit is contained in:
Alex Holliday
2024-09-14 12:16:13 +08:00
parent 15e067c9bf
commit 9bbd5ce12a
5 changed files with 28 additions and 8 deletions

View File

@@ -7,9 +7,10 @@ const {
getTeamChecksQueryValidation,
deleteChecksParamValidation,
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,
} = require("../validation/joi");
const { successMessages } = require("../utils/messages");
const SERVICE_NAME = "check";
const SERVICE_NAME = "checkController";
const createCheck = async (req, res, next) => {
try {
@@ -125,6 +126,7 @@ const deleteChecksByTeamId = async (req, res, next) => {
error.method === undefined ? (error.method = "deleteChecksByTeam") : null;
error.status = 422;
next(error);
return;
}
try {
@@ -143,7 +145,20 @@ const deleteChecksByTeamId = async (req, res, next) => {
const updateChecksTTL = async (req, res, next) => {
try {
await req.db.updateChecksTTL(1);
await updateChecksTTLBodyValidation.validateAsync(req.body);
} catch (error) {
error.status = 422;
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "updateChecksTTL") : null;
error.message =
error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
try {
const ttl = req.body.ttl;
await req.db.updateChecksTTL(1, ttl);
return res.status(200).json({
success: true,
msg: successMessages.CHECK_UPDATE_TTL,

View File

@@ -239,16 +239,16 @@ const deleteChecksByTeamId = async (teamId) => {
}
};
const updateChecksTTL = async (teamId) => {
const updateChecksTTL = async (teamId, ttl) => {
try {
await Check.collection.dropIndex("expiry_1");
await Check.collection.createIndex(
{ expiry: 1 }, // Field to index
{ expireAfterSeconds: 60 } // TTL in seconds, adjust as necessary
{ expiry: 1 },
{ expireAfterSeconds: ttl } // TTL in seconds, adjust as necessary
);
} catch (error) {
error.service = SERVICE_NAME;
error.method = updateTTL;
error.method = "upddateChecksTTL";
throw error;
}
};

View File

@@ -262,7 +262,7 @@ const getMonitorStatsById = async (req) => {
return monitorStats;
} catch (error) {
error.methodName = "getMonitorStatsById";
error.method = "getMonitorStatsById";
throw error;
}
};

View File

@@ -7,7 +7,7 @@ const handleErrors = (error, req, res, next) => {
const service = error.service || errorMessages.UNKNOWN_SERVICE;
logger.error(error.message, {
service: service,
methodName: error.methodName,
method: error.method,
});
res.status(status).json({ success: false, msg: message });
};

View File

@@ -335,6 +335,10 @@ const deleteChecksByTeamIdParamValidation = joi.object({
teamId: joi.string().required(),
});
const updateChecksTTLBodyValidation = joi.object({
ttl: joi.number().required(),
});
//****************************************
// PageSpeedCheckValidation
//****************************************
@@ -423,6 +427,7 @@ module.exports = {
getTeamChecksQueryValidation,
deleteChecksParamValidation,
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,
deleteUserParamValidation,
getPageSpeedCheckParamValidation,
createPageSpeedCheckParamValidation,