Merge branch 'fix/remove-unused-alert-module' into feat/swagger-ui
This commit is contained in:
commit
4ed7d426e8
6 files changed
-437
No files matched your search
@@ -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<Express.Response>}
|
|
||||||
* @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<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.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,
|
|
||||||
};
|
|
||||||
@@ -100,20 +100,6 @@ const {
|
|||||||
updateChecksTTL,
|
updateChecksTTL,
|
||||||
} = require("./modules/checkModule");
|
} = require("./modules/checkModule");
|
||||||
|
|
||||||
//****************************************
|
|
||||||
// Alerts
|
|
||||||
//****************************************
|
|
||||||
|
|
||||||
const {
|
|
||||||
createAlert,
|
|
||||||
getAlertsByUserId,
|
|
||||||
getAlertsByMonitorId,
|
|
||||||
getAlertById,
|
|
||||||
editAlert,
|
|
||||||
deleteAlert,
|
|
||||||
deleteAlertByMonitorId,
|
|
||||||
} = require("./modules/alertModule");
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Maintenance Window
|
// Maintenance Window
|
||||||
//****************************************
|
//****************************************
|
||||||
@@ -163,13 +149,6 @@ module.exports = {
|
|||||||
deleteChecks,
|
deleteChecks,
|
||||||
deleteChecksByTeamId,
|
deleteChecksByTeamId,
|
||||||
updateChecksTTL,
|
updateChecksTTL,
|
||||||
createAlert,
|
|
||||||
getAlertsByUserId,
|
|
||||||
getAlertsByMonitorId,
|
|
||||||
getAlertById,
|
|
||||||
editAlert,
|
|
||||||
deleteAlert,
|
|
||||||
deleteAlertByMonitorId,
|
|
||||||
deleteMonitorsByUserId,
|
deleteMonitorsByUserId,
|
||||||
createPageSpeedCheck,
|
createPageSpeedCheck,
|
||||||
getPageSpeedChecks,
|
getPageSpeedChecks,
|
||||||
|
|||||||
@@ -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 {<Promise<Alert>>}
|
|
||||||
* @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 {<Promise<Array<Alert>>}
|
|
||||||
* @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 {<Promise<Array<Alert>>}
|
|
||||||
* @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<Alert>}
|
|
||||||
* @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<Alert>>}
|
|
||||||
* @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,
|
|
||||||
};
|
|
||||||
@@ -14,7 +14,6 @@ const authRouter = require("./routes/authRoute");
|
|||||||
const inviteRouter = require("./routes/inviteRoute");
|
const inviteRouter = require("./routes/inviteRoute");
|
||||||
const monitorRouter = require("./routes/monitorRoute");
|
const monitorRouter = require("./routes/monitorRoute");
|
||||||
const checkRouter = require("./routes/checkRoute");
|
const checkRouter = require("./routes/checkRoute");
|
||||||
const alertRouter = require("./routes/alertRoute");
|
|
||||||
const pageSpeedCheckRouter = require("./routes/pageSpeedCheckRoute");
|
const pageSpeedCheckRouter = require("./routes/pageSpeedCheckRoute");
|
||||||
const maintenanceWindowRouter = require("./routes/maintenanceWindowRoute");
|
const maintenanceWindowRouter = require("./routes/maintenanceWindowRoute");
|
||||||
|
|
||||||
@@ -85,7 +84,6 @@ const startApp = async () => {
|
|||||||
app.use("/api/v1/invite", inviteRouter);
|
app.use("/api/v1/invite", inviteRouter);
|
||||||
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
||||||
app.use("/api/v1/checks", verifyJWT, checkRouter);
|
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/pagespeed", verifyJWT, pageSpeedCheckRouter);
|
||||||
app.use("/api/v1/maintenance-window", verifyJWT, maintenanceWindowRouter);
|
app.use("/api/v1/maintenance-window", verifyJWT, maintenanceWindowRouter);
|
||||||
//Temporary route for testing, remove later
|
//Temporary route for testing, remove later
|
||||||
|
|||||||
@@ -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);
|
|
||||||
@@ -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;
|
|
||||||
Reference in new issue
Block a user