Added alert DB operations

This commit is contained in:
Alex Holliday committed 2024-05-29 12:12:56 -07:00
1 parent 247153a133
commit 879d2337b6
2 files changed
+117 -67

No files matched your search

+65 -19
View File
@@ -11,6 +11,7 @@ const {
editAlertBodyValidation,
deleteAlertParamValidation,
} = require("../validation/joi");
const { get } = require("mongoose");
const SERVICE_NAME = "alerts";
@@ -35,7 +36,11 @@ const createAlert = async (req, res, next) => {
}
try {
req.db.createAlert(req, res);
const alertData = { ...req.body };
const alert = await req.db.createAlert(alertData);
return res
.status(200)
.json({ success: true, msg: "Alert created", data: alert });
} catch (error) {
error.service = SERVICE_NAME;
next(error);
@@ -62,7 +67,10 @@ const getAlertsByUserId = async (req, res, next) => {
}
try {
req.db.getAlertsByUserId(req, res);
const alerts = await req.db.getAlertsByUserId(req.params.userId);
return res
.status(200)
.json({ success: true, msg: "Got alerts", data: alerts });
} catch (error) {
error.service = SERVICE_NAME;
next(error);
@@ -72,29 +80,30 @@ const getAlertsByUserId = async (req, res, next) => {
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;
}
try {
const alerts = await req.db.getAlertsByMonitorId(req.params.monitorId);
return res.status(200).json({
success: true,
msg: "Got alerts by Monitor",
data: alerts,
});
} catch (error) {
error.service = SERVICE_NAME;
next(error);
}
};
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;
@@ -102,24 +111,49 @@ const getAlertById = async (req, res, next) => {
next(error);
return;
}
try {
const alert = await req.db.getAlertById(req.params.alertId);
return res.status(200).json({
success: true,
msg: "Got Alert By alertID",
data: alert,
});
} catch (error) {
error.service = SERVICE_NAME;
next(error);
}
};
const editAlert = async (req, res, next) => {
try {
await editAlertParamValidation(req.params);
await editAlertBodyValidation(req.body);
res.status(200).json({ success: true, msg: "Edit alert" });
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.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: "Edited alert",
data: alert,
});
} catch (error) {
error.service = SERVICE_NAME;
next(error);
}
};
const deleteAlert = async (req, res, next) => {
try {
await deleteAlertParamValidation(req.params);
res.status(200).json({ success: true, msg: "Delete alert" });
await deleteAlertParamValidation.validateAsync(req.params);
} catch (error) {
error.status = 422;
error.message = error.details[0].message;
@@ -127,6 +161,18 @@ const deleteAlert = async (req, res, next) => {
next(error);
return;
}
try {
const deleted = await req.db.deleteAlert(req.params.alertId);
return res.status(200).json({
success: true,
msg: "Deleted alert",
data: deleted,
});
} catch (error) {
error.service = SERVICE_NAME;
next(error);
}
};
module.exports = {