diff --git a/Server/controllers/alertController.js b/Server/controllers/alertController.js index b7295b901..91bb56971 100644 --- a/Server/controllers/alertController.js +++ b/Server/controllers/alertController.js @@ -1,40 +1,132 @@ const express = require("express"); -const {} = require("../validation/joi"); const logger = require("../utils/logger"); require("dotenv").config(); -const { createAlertParamValidation } = require("../validation/joi"); +const { + createAlertParamValidation, + createAlertBodyValidation, + getAlertsByUserIdParamValidation, + getAlertsByMonitorIdParamValidation, + getAlertByIdParamValidation, + editAlertParamValidation, + editAlertBodyValidation, + deleteAlertParamValidation, +} = require("../validation/joi"); const SERVICE_NAME = "alerts"; +/** + * Creates an alert for a monitor + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ const createAlert = async (req, res, next) => { try { await createAlertParamValidation.validateAsync(req.params); - res - .status(200) - .json({ success: true, msg: "Create Alert", data: req.params.monitorId }); + await createAlertBodyValidation.validateAsync(req.body); + } catch (error) { + error.status = 422; + error.message = error.details[0].message; + error.service = SERVICE_NAME; + next(error); + return; + } + + try { + req.db.createAlert(req, res); } catch (error) { error.service = SERVICE_NAME; next(error); } }; -const getAlertsByUserId = (req, res, next) => { - res.status(200).json({ - success: true, - msg: "Get Alerts By UserID", - data: req.params.userId, - }); + +/** + * Gets all alerts for a user + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @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) => { - res.status(200).json({ success: true, msg: "Get Alert By alertID" }); +const getAlertById = async (req, res, next) => { + 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) => { - res.status(200).json({ success: true, msg: "Edit alert" }); +const editAlert = async (req, res, next) => { + 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) => { - res.status(200).json({ success: true, msg: "Delete alert" }); +const deleteAlert = async (req, res, next) => { + 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 = { diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index cc476ab54..257048937 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -17,7 +17,7 @@ const SERVICE_NAME = "auth"; * @returns {String} */ 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 { await registerValidation.validateAsync(req.body); } catch (error) { - error.status = 400; + error.status = 422; error.service = SERVICE_NAME; error.message = error.details[0].message; next(error); diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index a4c9422a3..32d5d33f7 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -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 {>} + * @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 {>} + * @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 {>} + * @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} + * @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>} + * @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 = { connect, insertUser, @@ -279,4 +396,10 @@ module.exports = { createCheck, getChecks, deleteChecks, + createAlert, + getAlertsByUserId, + getAlertsByMonitorId, + getAlertById, + editAlert, + deleteAlert, }; diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 441e0ebc3..01a0d0ada 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -43,6 +43,43 @@ const createAlertParamValidation = joi.object({ 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 = { loginValidation, registerValidation, @@ -52,4 +89,11 @@ module.exports = { editUserParamValidation, editUserBodyValidation, createAlertParamValidation, + createAlertBodyValidation, + getAlertsByUserIdParamValidation, + getAlertsByMonitorIdParamValidation, + getAlertByIdParamValidation, + editAlertParamValidation, + editAlertBodyValidation, + deleteAlertParamValidation, };