mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-25 11:19:16 -06:00
Merge branch 'feat/ttl' into feat/be/error-handling
This commit is contained in:
@@ -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 {
|
||||
@@ -31,7 +32,8 @@ const createCheck = async (req, res, next) => {
|
||||
.status(200)
|
||||
.json({ success: true, msg: successMessages.CHECK_CREATE, data: check });
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "createCheck") : null;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
@@ -58,7 +60,8 @@ const getChecks = async (req, res, next) => {
|
||||
data: { checksCount, checks },
|
||||
});
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "getChecks") : null;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
@@ -83,7 +86,8 @@ const getTeamChecks = async (req, res, next) => {
|
||||
data: checkData,
|
||||
});
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "getTeamChecks") : null;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
@@ -108,7 +112,8 @@ const deleteChecks = async (req, res, next) => {
|
||||
data: { deletedCount },
|
||||
});
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "deleteChecks") : null;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
@@ -117,10 +122,11 @@ const deleteChecksByTeamId = async (req, res, next) => {
|
||||
try {
|
||||
await deleteChecksByTeamIdParamValidation.validateAsync(req.params);
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.method = "deleteChecksByTeam";
|
||||
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "deleteChecksByTeam") : null;
|
||||
error.status = 422;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -131,8 +137,35 @@ const deleteChecksByTeamId = async (req, res, next) => {
|
||||
data: { deletedCount },
|
||||
});
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.method = "deleteChecksByTeamId";
|
||||
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "deleteChecksByTeamId") : null;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
const updateChecksTTL = async (req, res, next) => {
|
||||
try {
|
||||
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,
|
||||
});
|
||||
} catch (error) {
|
||||
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "updateTTL") : null;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
@@ -143,4 +176,5 @@ module.exports = {
|
||||
getTeamChecks,
|
||||
deleteChecks,
|
||||
deleteChecksByTeamId,
|
||||
updateChecksTTL,
|
||||
};
|
||||
|
||||
@@ -97,6 +97,7 @@ const {
|
||||
getTeamChecks,
|
||||
deleteChecks,
|
||||
deleteChecksByTeamId,
|
||||
updateChecksTTL,
|
||||
} = require("./modules/checkModule");
|
||||
|
||||
//****************************************
|
||||
@@ -161,6 +162,7 @@ module.exports = {
|
||||
getTeamChecks,
|
||||
deleteChecks,
|
||||
deleteChecksByTeamId,
|
||||
updateChecksTTL,
|
||||
createAlert,
|
||||
getAlertsByUserId,
|
||||
getAlertsByMonitorId,
|
||||
|
||||
@@ -239,6 +239,20 @@ const deleteChecksByTeamId = async (teamId) => {
|
||||
}
|
||||
};
|
||||
|
||||
const updateChecksTTL = async (teamId, ttl) => {
|
||||
try {
|
||||
await Check.collection.dropIndex("expiry_1");
|
||||
await Check.collection.createIndex(
|
||||
{ expiry: 1 },
|
||||
{ expireAfterSeconds: ttl } // TTL in seconds, adjust as necessary
|
||||
);
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.method = "updateChecksTTL";
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createCheck,
|
||||
getChecksCount,
|
||||
@@ -246,4 +260,5 @@ module.exports = {
|
||||
getTeamChecks,
|
||||
deleteChecks,
|
||||
deleteChecksByTeamId,
|
||||
updateChecksTTL,
|
||||
};
|
||||
|
||||
@@ -262,7 +262,7 @@ const getMonitorStatsById = async (req) => {
|
||||
|
||||
return monitorStats;
|
||||
} catch (error) {
|
||||
error.methodName = "getMonitorStatsById";
|
||||
error.method = "getMonitorStatsById";
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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 });
|
||||
};
|
||||
|
||||
@@ -26,4 +26,10 @@ router.delete(
|
||||
checkController.deleteChecksByTeamId
|
||||
);
|
||||
|
||||
router.put(
|
||||
"/ttl",
|
||||
isAllowed(["admin", "superadmin"]),
|
||||
checkController.updateChecksTTL
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -73,6 +73,7 @@ const successMessages = {
|
||||
CHECK_CREATE: "Check created successfully",
|
||||
CHECK_GET: "Got checks successfully",
|
||||
CHECK_DELETE: "Checks deleted successfully",
|
||||
CHECK_UPDATE_TTL: "Checks TTL updated successfully",
|
||||
|
||||
//Monitor Controller
|
||||
MONITOR_GET_ALL: "Got all monitors successfully",
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user