implemented DB operations for Checks
This commit is contained in:
1 parent
acc8f6a1e5
commit
6437e8e9bf
3 files changed
+127
-41
No files matched your search
@@ -1,29 +1,73 @@
|
|||||||
var jwt = require("jsonwebtoken");
|
const {
|
||||||
|
createCheckParamValidation,
|
||||||
|
createCheckBodyValidation,
|
||||||
|
getChecksParamValidation,
|
||||||
|
deleteChecksParamValidation,
|
||||||
|
} = require("../validation/joi");
|
||||||
const SERVICE_NAME = "check";
|
const SERVICE_NAME = "check";
|
||||||
|
|
||||||
const createCheck = (req, res, next) => {
|
const createCheck = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
req.db.createCheck(req, res);
|
await createCheckParamValidation.validateAsync(req.params);
|
||||||
|
await createCheckBodyValidation.validateAsync(req.body);
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const checkData = { ...req.body };
|
||||||
|
const check = await req.db.createCheck(checkData);
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Check created", data: check });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getChecks = (req, res, next) => {
|
const getChecks = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
req.db.getChecks(req, res);
|
await getChecksParamValidation.validateAsync(req.params);
|
||||||
// Return all checks for a monitor
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const checks = await req.db.getChecks(req.params.monitorId);
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Checks retrieved", data: checks });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteChecks = (req, res, next) => {
|
const deleteChecks = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
req.db.deleteChecks(req, res);
|
await deleteChecksParamValidation.validateAsync(req.params);
|
||||||
// Delete all checks for a monitor
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const deletedCount = await req.db.deleteChecks(req.params.monitorId);
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Checks deleted", data: { deletedCount } });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
|
|||||||
+25
-23
@@ -1,6 +1,7 @@
|
|||||||
const Monitor = require("../models/Monitor");
|
const Monitor = require("../models/Monitor");
|
||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
const UserModel = require("../models/user");
|
const UserModel = require("../models/user");
|
||||||
|
const Check = require("../models/Check");
|
||||||
|
|
||||||
const verifyId = (userId, monitorId) => {
|
const verifyId = (userId, monitorId) => {
|
||||||
return userId.toString() === monitorId.toString();
|
return userId.toString() === monitorId.toString();
|
||||||
@@ -209,17 +210,20 @@ const editMonitor = async (req, res) => {
|
|||||||
/**
|
/**
|
||||||
* Create a check for a monitor
|
* Create a check for a monitor
|
||||||
* @async
|
* @async
|
||||||
* @param {Express.Request} req
|
* @param {Object} checkData
|
||||||
* @param {Express.Response} res
|
* @param {string} checkData.monitorId
|
||||||
|
* @param {boolean} checkData.status
|
||||||
|
* @param {number} checkData.responseTime
|
||||||
|
* @param {number} checkData.statusCode
|
||||||
|
* @param {string} checkData.message
|
||||||
|
* @returns {Promise<Check>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const createCheck = async (req, res) => {
|
const createCheck = async (checkData) => {
|
||||||
try {
|
try {
|
||||||
// Create check with monitor id
|
const check = await new Check({ ...checkData }).save();
|
||||||
res
|
return check;
|
||||||
.status(200)
|
|
||||||
.json({ success: true, msg: "Create check", data: req.params.monitorId });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -228,17 +232,15 @@ const createCheck = async (req, res) => {
|
|||||||
/**
|
/**
|
||||||
* Get all checks for a monitor
|
* Get all checks for a monitor
|
||||||
* @async
|
* @async
|
||||||
* @param {Express.Request} req
|
* @param {string} monitorId
|
||||||
* @param {Express.Response} res
|
* @returns {Promise<Array<Check>>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const getChecks = async (req, res) => {
|
const getChecks = async (monitorId) => {
|
||||||
try {
|
try {
|
||||||
// TODO get all checks for a monitor from DB
|
const checks = await Check.find({ monitorId });
|
||||||
res
|
return checks;
|
||||||
.status(200)
|
|
||||||
.json({ success: true, msg: "Get checks", data: req.params.monitorId });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -247,19 +249,19 @@ const getChecks = async (req, res) => {
|
|||||||
/**
|
/**
|
||||||
* Delete all checks for a monitor
|
* Delete all checks for a monitor
|
||||||
* @async
|
* @async
|
||||||
* @param {Express.Request} req
|
* @param {string} monitorId
|
||||||
* @param {Express.Response} res
|
* @returns {boolean}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const deleteChecks = async (req, res) => {
|
const deleteChecks = async (monitorId) => {
|
||||||
try {
|
try {
|
||||||
// TODO Delete all checks for a monitor
|
const result = await Check.deleteMany({ monitorId });
|
||||||
res.status(200).json({
|
if (result.deletedCount > 0) {
|
||||||
success: true,
|
return result.deletedCount;
|
||||||
msg: "Deleted checks",
|
} else {
|
||||||
data: req.params.monitorId,
|
throw new Error(`No checks found for monitor with id ${monitorId}`);
|
||||||
});
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
const joi = require("joi");
|
const joi = require("joi");
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Auth
|
||||||
|
//****************************************
|
||||||
|
|
||||||
const loginValidation = joi.object({
|
const loginValidation = joi.object({
|
||||||
email: joi.string().email().required(),
|
email: joi.string().email().required(),
|
||||||
password: joi.string().min(8).required(),
|
password: joi.string().min(8).required(),
|
||||||
@@ -11,6 +15,20 @@ const registerValidation = joi.object({
|
|||||||
password: joi.string().min(8).required(),
|
password: joi.string().min(8).required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const editUserParamValidation = joi.object({
|
||||||
|
userId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const editUserBodyValidation = joi.object({
|
||||||
|
firstname: joi.string(),
|
||||||
|
lastname: joi.string(),
|
||||||
|
profilePicUrl: joi.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Monitors
|
||||||
|
//****************************************
|
||||||
|
|
||||||
const getMonitorByIdValidation = joi.object({
|
const getMonitorByIdValidation = joi.object({
|
||||||
monitorId: joi.string().required(),
|
monitorId: joi.string().required(),
|
||||||
});
|
});
|
||||||
@@ -29,15 +47,9 @@ const monitorValidation = joi.object({
|
|||||||
interval: joi.number(),
|
interval: joi.number(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const editUserParamValidation = joi.object({
|
//****************************************
|
||||||
userId: joi.string().required(),
|
// Alerts
|
||||||
});
|
//****************************************
|
||||||
|
|
||||||
const editUserBodyValidation = joi.object({
|
|
||||||
firstname: joi.string(),
|
|
||||||
lastname: joi.string(),
|
|
||||||
profilePicUrl: joi.string(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const createAlertParamValidation = joi.object({
|
const createAlertParamValidation = joi.object({
|
||||||
monitorId: joi.string().required(),
|
monitorId: joi.string().required(),
|
||||||
@@ -80,6 +92,30 @@ const deleteAlertParamValidation = joi.object({
|
|||||||
alertId: joi.string().required(),
|
alertId: joi.string().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Checks
|
||||||
|
//****************************************
|
||||||
|
|
||||||
|
const createCheckParamValidation = joi.object({
|
||||||
|
monitorId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const createCheckBodyValidation = joi.object({
|
||||||
|
monitorId: joi.string().required(),
|
||||||
|
status: joi.boolean().required(),
|
||||||
|
responseTime: joi.number().required(),
|
||||||
|
statusCode: joi.number().required(),
|
||||||
|
message: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const getChecksParamValidation = joi.object({
|
||||||
|
monitorId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const deleteChecksParamValidation = joi.object({
|
||||||
|
monitorId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
loginValidation,
|
loginValidation,
|
||||||
registerValidation,
|
registerValidation,
|
||||||
@@ -96,4 +132,8 @@ module.exports = {
|
|||||||
editAlertParamValidation,
|
editAlertParamValidation,
|
||||||
editAlertBodyValidation,
|
editAlertBodyValidation,
|
||||||
deleteAlertParamValidation,
|
deleteAlertParamValidation,
|
||||||
|
createCheckParamValidation,
|
||||||
|
createCheckBodyValidation,
|
||||||
|
getChecksParamValidation,
|
||||||
|
deleteChecksParamValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user