diff --git a/Server/controllers/alertController.js b/Server/controllers/alertController.js deleted file mode 100644 index cd31db565..000000000 --- a/Server/controllers/alertController.js +++ /dev/null @@ -1,192 +0,0 @@ -const logger = require("../utils/logger"); -require("dotenv").config(); -const { - createAlertParamValidation, - createAlertBodyValidation, - getAlertsByUserIdParamValidation, - getAlertsByMonitorIdParamValidation, - getAlertByIdParamValidation, - editAlertParamValidation, - editAlertBodyValidation, - deleteAlertParamValidation, -} = require("../validation/joi"); -const { successMessages } = require("../utils/messages"); -const SERVICE_NAME = "alertController"; - -/** - * 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); - await createAlertBodyValidation.validateAsync(req.body); - } catch (error) { - error.status = 422; - error.message = - error.details?.[0]?.message || error.message || "Validation Error"; - error.service = SERVICE_NAME; - next(error); - return; - } - - try { - const alertData = { ...req.body }; - const alert = await req.db.createAlert(alertData); - return res - .status(200) - .json({ success: true, msg: successMessages.ALERT_CREATE, data: alert }); - } catch (error) { - error.service === undefined ? (error.service = SERVICE_NAME) : null; - error.method === undefined ? (error.method = "createAlert") : null; - next(error); - } -}; - -/** - * 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.message || "Validation Error"; - error.service = SERVICE_NAME; - next(error); - return; - } - - try { - const alerts = await req.db.getAlertsByUserId(req.params.userId); - return res.status(200).json({ - success: true, - msg: successMessages.ALERT_GET_BY_USER, - data: alerts, - }); - } catch (error) { - error.service = SERVICE_NAME; - next(error); - } -}; - -const getAlertsByMonitorId = async (req, res, next) => { - try { - await getAlertsByMonitorIdParamValidation.validateAsync(req.params); - } catch (error) { - error.status = 422; - error.message = - error.details?.[0]?.message || error.message || "Validation Error"; - error.service = SERVICE_NAME; - next(error); - return; - } - - try { - const alerts = await req.db.getAlertsByMonitorId(req.params.monitorId); - return res.status(200).json({ - success: true, - msg: successMessages.ALERT_GET_BY_MONITOR, - data: alerts, - }); - } catch (error) { - error.service = SERVICE_NAME; - next(error); - } -}; - -const getAlertById = async (req, res, next) => { - try { - await getAlertByIdParamValidation.validateAsync(req.params); - } catch (error) { - error.status = 422; - error.message = - error.details?.[0]?.message || error.message || "Validation Error"; - error.service = SERVICE_NAME; - next(error); - return; - } - - try { - const alert = await req.db.getAlertById(req.params.alertId); - return res.status(200).json({ - success: true, - msg: successMessages.ALERT_GET_BY_ID, - data: alert, - }); - } catch (error) { - error.service = SERVICE_NAME; - next(error); - } -}; - -const editAlert = async (req, res, next) => { - try { - await editAlertParamValidation.validateAsync(req.params); - await editAlertBodyValidation.validateAsync(req.body); - } catch (error) { - console.log(error); - error.status = 422; - error.message = - error.details?.[0]?.message || error.message || "Validation Error"; - error.service = SERVICE_NAME; - next(error); - return; - } - - try { - const alertData = { ...req.body }; - const alert = await req.db.editAlert(req.params.alertId, alertData); - return res.status(200).json({ - success: true, - msg: successMessages.ALERT_EDIT, - data: alert, - }); - } catch (error) { - error.service = SERVICE_NAME; - next(error); - } -}; -const deleteAlert = async (req, res, next) => { - try { - await deleteAlertParamValidation.validateAsync(req.params); - } catch (error) { - error.status = 422; - error.message = - error.details?.[0]?.message || error.message || "Validation Error"; - error.service = SERVICE_NAME; - next(error); - return; - } - - try { - const deleted = await req.db.deleteAlert(req.params.alertId); - return res.status(200).json({ - success: true, - msg: successMessages.ALERT_DELETE, - data: deleted, - }); - } catch (error) { - error.service = SERVICE_NAME; - next(error); - } -}; - -module.exports = { - createAlert, - getAlertsByUserId, - getAlertsByMonitorId, - getAlertById, - editAlert, - deleteAlert, -}; diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index ebc50f7da..c5fcff08c 100644 --- a/Server/db/mongo/MongoDB.js +++ b/Server/db/mongo/MongoDB.js @@ -100,20 +100,6 @@ const { updateChecksTTL, } = require("./modules/checkModule"); -//**************************************** -// Alerts -//**************************************** - -const { - createAlert, - getAlertsByUserId, - getAlertsByMonitorId, - getAlertById, - editAlert, - deleteAlert, - deleteAlertByMonitorId, -} = require("./modules/alertModule"); - //**************************************** // Maintenance Window //**************************************** @@ -163,13 +149,6 @@ module.exports = { deleteChecks, deleteChecksByTeamId, updateChecksTTL, - createAlert, - getAlertsByUserId, - getAlertsByMonitorId, - getAlertById, - editAlert, - deleteAlert, - deleteAlertByMonitorId, deleteMonitorsByUserId, createPageSpeedCheck, getPageSpeedChecks, diff --git a/Server/db/mongo/modules/alertModule.js b/Server/db/mongo/modules/alertModule.js deleted file mode 100644 index b9e65647b..000000000 --- a/Server/db/mongo/modules/alertModule.js +++ /dev/null @@ -1,140 +0,0 @@ -const Alert = require("../../../models/Alert"); - -/** - * Creates an Alert associated with a Monitor and User - * @async - * @param {Object} alertData - * @param {string} alertData.checkId - * @param {string} alert.monitorId - * @param {string} alert.userId - * @param {boolean} alert.status - * @param {string} alert.message - * @param {boolean} alert.notifiedStatus - * @param {boolean} alert.acknowledgeStatus - * @returns {>} - * @throws {Error} - */ -const createAlert = async (alertData) => { - try { - const alert = await new Alert({ ...alertData }).save(); - return alert; - } catch (error) { - throw error; - } -}; - -/** - * Gets all alerts a User has set - * @async - * @param {string} userId - * @returns {>} - * @throws {Error} - */ -const getAlertsByUserId = async (userId) => { - try { - const alerts = await Alert.find({ userId }); - return alerts; - } catch (error) { - throw error; - } -}; - -/** - * Gets all alerts a for an associated Monitor - * @async - * @param {string} monitorId - * @returns {>} - * @throws {Error} - */ -const getAlertsByMonitorId = async (monitorId) => { - try { - const alerts = await Alert.find({ monitorId }); - return alerts; - } catch (error) { - throw error; - } -}; - -/** - * Returns an alert with specified ID - * @async - * @param {string} alertId - * @returns {Promise} - * @throws {Error} - */ -const getAlertById = async (alertId) => { - try { - const alert = await Alert.findById(alertId); - return alert; - } catch (error) { - throw error; - } -}; - -/** - * Returns an edited alert with the specified ID - * @async - * @param {string} alertId - * @param {Object} alertData - * @param {string} alertData.checkId - * @param {string} alertData.monitorId - * @param {string} alertData.userId - * @param {boolean} alertData.status - * @param {string} alertData.message - * @param {boolean} alertData.notifiedStatus - * @param {boolean} alertData.acknowledgeStatus - * @param {Express.Response} res - * @returns {Promise>} - * @throws {Error} - */ -const editAlert = async (alertId, alertData) => { - try { - const editedAlert = await Alert.findByIdAndUpdate(alertId, alertData, { - new: true, - }); - return editedAlert; - } catch (error) { - throw error; - } -}; - -/** - * Deletes an alert with the specified ID - * @async - * @param {string} alertId - * @throws {Error} - */ -const deleteAlert = async (alertId) => { - try { - const result = await Alert.findByIdAndDelete(alertId); - return result; - } catch (error) { - throw error; - } -}; - -/** - * Deletes alerts by monitor ID. - * - * @param {string} monitorId - The ID of the monitor. - * @returns {Promise} A promise that resolves to the result of the delete operation. - * @throws {Error} If an error occurs while deleting the alerts. - */ -const deleteAlertByMonitorId = async (monitorId) => { - try { - const result = await Alert.deleteMany({ monitorId }); - return result; - } catch (error) { - throw error; - } -}; - -module.exports = { - createAlert, - getAlertsByUserId, - getAlertsByMonitorId, - getAlertById, - editAlert, - deleteAlert, - deleteAlertByMonitorId, -}; diff --git a/Server/index.js b/Server/index.js index 672aa4882..e665f21d2 100644 --- a/Server/index.js +++ b/Server/index.js @@ -14,7 +14,6 @@ const authRouter = require("./routes/authRoute"); const inviteRouter = require("./routes/inviteRoute"); const monitorRouter = require("./routes/monitorRoute"); const checkRouter = require("./routes/checkRoute"); -const alertRouter = require("./routes/alertRoute"); const pageSpeedCheckRouter = require("./routes/pageSpeedCheckRoute"); const maintenanceWindowRouter = require("./routes/maintenanceWindowRoute"); @@ -85,7 +84,6 @@ const startApp = async () => { app.use("/api/v1/invite", inviteRouter); app.use("/api/v1/monitors", verifyJWT, monitorRouter); app.use("/api/v1/checks", verifyJWT, checkRouter); - app.use("/api/v1/alerts", verifyJWT, alertRouter); app.use("/api/v1/pagespeed", verifyJWT, pageSpeedCheckRouter); app.use("/api/v1/maintenance-window", verifyJWT, maintenanceWindowRouter); //Temporary route for testing, remove later diff --git a/Server/models/Alert.js b/Server/models/Alert.js deleted file mode 100644 index 758a7eba2..000000000 --- a/Server/models/Alert.js +++ /dev/null @@ -1,38 +0,0 @@ -const mongoose = require('mongoose') - -const AlertSchema = mongoose.Schema( - { - checkId: { - type: mongoose.Schema.Types.ObjectId, - ref: 'Check', - immutable: true, - }, - monitorId: { - type: mongoose.Schema.Types.ObjectId, - ref: 'Monitor', - immutable: true, - }, - userId: { - type: mongoose.Schema.Types.ObjectId, - ref: 'User', - immutable: true, - }, - status: { - type: Boolean, - }, - message: { - type: String, - }, - notifiedStatus: { - type: Boolean, - }, - acknowledgeStatus: { - type: Boolean, - } - }, - { - timestamps: true, - } -); - -module.exports = mongoose.model('Alert', AlertSchema); diff --git a/Server/routes/alertRoute.js b/Server/routes/alertRoute.js deleted file mode 100644 index 434c0495a..000000000 --- a/Server/routes/alertRoute.js +++ /dev/null @@ -1,44 +0,0 @@ -const router = require("express").Router(); -const alertController = require("../controllers/alertController"); -const { verifyOwnership } = require("../middleware/verifyOwnership"); -const Alert = require("../models/Alert"); -const Monitor = require("../models/Monitor"); - -// Create alert -router.post( - "/:monitorId", - verifyOwnership(Monitor, "monitorId"), - alertController.createAlert -); -// Get all alerts for a user -router.get( - "/user/:userId", - verifyOwnership(Monitor, "monitorId"), - alertController.getAlertsByUserId -); -// Get all alerts for a monitor -router.get( - "/monitor/:monitorId", - verifyOwnership(Monitor, "monitorId"), - alertController.getAlertsByMonitorId -); -// Get a single alert -router.get( - "/:alertId", - verifyOwnership(Monitor, "monitorId"), - alertController.getAlertById -); -// Edit -router.put( - "/:alertId", - verifyOwnership(Monitor, "monitorId"), - alertController.editAlert -); -//Delete -router.delete( - "/:alertId", - verifyOwnership(Monitor, "monitorId"), - alertController.deleteAlert -); - -module.exports = router;