Finished routes, validation, and scaffold DB
This commit is contained in:
1 parent
90baac5067
commit
fdb8c6b20a
4 files changed
+280
-21
No files matched your search
@@ -1,40 +1,132 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const {} = require("../validation/joi");
|
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
const { createAlertParamValidation } = require("../validation/joi");
|
const {
|
||||||
|
createAlertParamValidation,
|
||||||
|
createAlertBodyValidation,
|
||||||
|
getAlertsByUserIdParamValidation,
|
||||||
|
getAlertsByMonitorIdParamValidation,
|
||||||
|
getAlertByIdParamValidation,
|
||||||
|
editAlertParamValidation,
|
||||||
|
editAlertBodyValidation,
|
||||||
|
deleteAlertParamValidation,
|
||||||
|
} = require("../validation/joi");
|
||||||
|
|
||||||
const SERVICE_NAME = "alerts";
|
const SERVICE_NAME = "alerts";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an alert for a monitor
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Express.Response>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
const createAlert = async (req, res, next) => {
|
const createAlert = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await createAlertParamValidation.validateAsync(req.params);
|
await createAlertParamValidation.validateAsync(req.params);
|
||||||
res
|
await createAlertBodyValidation.validateAsync(req.body);
|
||||||
.status(200)
|
} catch (error) {
|
||||||
.json({ success: true, msg: "Create Alert", data: req.params.monitorId });
|
error.status = 422;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
req.db.createAlert(req, res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const getAlertsByUserId = (req, res, next) => {
|
|
||||||
res.status(200).json({
|
/**
|
||||||
success: true,
|
* Gets all alerts for a user
|
||||||
msg: "Get Alerts By UserID",
|
* @async
|
||||||
data: req.params.userId,
|
* @param {Express.Request} req
|
||||||
});
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Express.Response>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const getAlertsByUserId = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
await getAlertsByUserIdParamValidation.validateAsync(req.params);
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
req.db.getAlertsByUserId(req, res);
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const getAlertsByMonitorId = (req, res, next) => {
|
|
||||||
res.status(200).json({ success: true, msg: "Get Alerts By MonitorID" });
|
const getAlertsByMonitorId = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
await getAlertsByMonitorIdParamValidation.validateAsync(req.params);
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Get Alerts By MonitorID",
|
||||||
|
data: req.params.monitorId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
res.status(200).json({ success: true, msg: "Edit alert" });
|
||||||
|
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const getAlertById = (req, res, next) => {
|
const getAlertById = async (req, res, next) => {
|
||||||
res.status(200).json({ success: true, msg: "Get Alert By alertID" });
|
try {
|
||||||
|
await getAlertByIdParamValidation.validateAsync(req.params);
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Get Alert By alertID",
|
||||||
|
data: req.params.alertId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const editAlert = (req, res, next) => {
|
const editAlert = async (req, res, next) => {
|
||||||
res.status(200).json({ success: true, msg: "Edit alert" });
|
try {
|
||||||
|
await editAlertParamValidation(req.params);
|
||||||
|
await editAlertBodyValidation(req.body);
|
||||||
|
res.status(200).json({ success: true, msg: "Edit alert" });
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const deleteAlert = (req, res, next) => {
|
const deleteAlert = async (req, res, next) => {
|
||||||
res.status(200).json({ success: true, msg: "Delete alert" });
|
try {
|
||||||
|
await deleteAlertParamValidation(req.params);
|
||||||
|
res.status(200).json({ success: true, msg: "Delete alert" });
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.message = error.details[0].message;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const SERVICE_NAME = "auth";
|
|||||||
* @returns {String}
|
* @returns {String}
|
||||||
*/
|
*/
|
||||||
const issueToken = (payload) => {
|
const issueToken = (payload) => {
|
||||||
return jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "1h" });
|
return jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "99d" });
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,7 +31,7 @@ const registerController = async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
await registerValidation.validateAsync(req.body);
|
await registerValidation.validateAsync(req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.status = 400;
|
error.status = 422;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.message = error.details[0].message;
|
error.message = error.details[0].message;
|
||||||
next(error);
|
next(error);
|
||||||
|
|||||||
@@ -265,6 +265,123 @@ const deleteChecks = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Alerts
|
||||||
|
//****************************************
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an Alert associated with a Monitor and User
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {<Promise<Alert>>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const createAlert = async (req, res) => {
|
||||||
|
try {
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Create Alert", data: req.params.monitorId });
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all alerts a User has set
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {<Promise<Array<Alert>>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const getAlertsByUserId = async (req, res) => {
|
||||||
|
try {
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Get Alerts By UserID",
|
||||||
|
data: req.params.userId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all alerts a for an associated Monitor
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {<Promise<Array<Alert>>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const getAlertsByMonitorId = async (req, res) => {
|
||||||
|
try {
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Get Alerts By MonitorID",
|
||||||
|
data: req.params.monitorId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an alert with specified ID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Alert>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const getAlertById = async (req, res) => {
|
||||||
|
try {
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Get Alert By alertID",
|
||||||
|
data: req.params.alertId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an edited monitor with the specified ID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Alert>>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const editAlert = async (req, res) => {
|
||||||
|
try {
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Edit alert", data: req.params.alertId });
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a monitor with the specified ID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const deleteAlert = async (req, res) => {
|
||||||
|
try {
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Delete alert", data: req.params.alertId });
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
connect,
|
connect,
|
||||||
insertUser,
|
insertUser,
|
||||||
@@ -279,4 +396,10 @@ module.exports = {
|
|||||||
createCheck,
|
createCheck,
|
||||||
getChecks,
|
getChecks,
|
||||||
deleteChecks,
|
deleteChecks,
|
||||||
|
createAlert,
|
||||||
|
getAlertsByUserId,
|
||||||
|
getAlertsByMonitorId,
|
||||||
|
getAlertById,
|
||||||
|
editAlert,
|
||||||
|
deleteAlert,
|
||||||
};
|
};
|
||||||
@@ -43,6 +43,43 @@ const createAlertParamValidation = joi.object({
|
|||||||
monitorId: joi.string().required(),
|
monitorId: joi.string().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const createAlertBodyValidation = joi.object({
|
||||||
|
checkId: joi.string().required(),
|
||||||
|
monitorId: joi.string().required(),
|
||||||
|
userId: joi.string().required(),
|
||||||
|
status: joi.boolean(),
|
||||||
|
message: joi.string(),
|
||||||
|
notifiedStatus: joi.boolean(),
|
||||||
|
acknowledgeStatus: joi.boolean(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const getAlertsByUserIdParamValidation = joi.object({
|
||||||
|
userId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const getAlertsByMonitorIdParamValidation = joi.object({
|
||||||
|
monitorId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const getAlertByIdParamValidation = joi.object({
|
||||||
|
alertId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const editAlertParamValidation = joi.object({
|
||||||
|
alertId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const editAlertBodyValidation = joi.object({
|
||||||
|
status: joi.boolean(),
|
||||||
|
message: joi.string(),
|
||||||
|
notifiedStatus: joi.boolean(),
|
||||||
|
acknowledgeStatus: joi.boolean(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const deleteAlertParamValidation = joi.object({
|
||||||
|
alertId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
loginValidation,
|
loginValidation,
|
||||||
registerValidation,
|
registerValidation,
|
||||||
@@ -52,4 +89,11 @@ module.exports = {
|
|||||||
editUserParamValidation,
|
editUserParamValidation,
|
||||||
editUserBodyValidation,
|
editUserBodyValidation,
|
||||||
createAlertParamValidation,
|
createAlertParamValidation,
|
||||||
|
createAlertBodyValidation,
|
||||||
|
getAlertsByUserIdParamValidation,
|
||||||
|
getAlertsByMonitorIdParamValidation,
|
||||||
|
getAlertByIdParamValidation,
|
||||||
|
editAlertParamValidation,
|
||||||
|
editAlertBodyValidation,
|
||||||
|
deleteAlertParamValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user