handleChange(event, "type", setChecks)}
- />
-
-
-
*/}
{errors["type"] ? (
@@ -297,36 +260,62 @@ const CreateMonitor = () => {
-
Incident notifications
- When there is an incident, notify users
+ When there is an incident, notify users.
-
+
When there is a new incident,
-
+ console.log("disabled")}
+ isDisabled={true}
+ />
notification.type === "email"
+ )}
+ value={user?.email}
+ onChange={(event) => handleChange(event)}
/>
- console.log("disabled")}
+ isDisabled={true}
/>
+ {monitor.notifications.some(
+ (notification) => notification.type === "emails"
+ ) ? (
+
+ console.log("disabled")}
+ />
+
+ You can separate multiple emails with a comma
+
+
+ ) : (
+ ""
+ )}
-
Advanced settings
@@ -335,10 +324,8 @@ const CreateMonitor = () => {
diff --git a/Client/src/Pages/Monitors/index.jsx b/Client/src/Pages/Monitors/index.jsx
index dee5c3eba..b020ac47b 100644
--- a/Client/src/Pages/Monitors/index.jsx
+++ b/Client/src/Pages/Monitors/index.jsx
@@ -111,7 +111,12 @@ const Monitors = () => {
),
},
{ id: idx + 2, data: },
- { id: idx + 3, data: monitor.type },
+ {
+ id: idx + 3,
+ data: (
+ {monitor.type}
+ ),
+ },
{ id: idx + 4, data: "TODO" },
],
};
diff --git a/Client/src/Pages/Settings/index.css b/Client/src/Pages/Settings/index.css
index 11a61b7d2..3f6ac8d7a 100644
--- a/Client/src/Pages/Settings/index.css
+++ b/Client/src/Pages/Settings/index.css
@@ -29,6 +29,7 @@
padding-bottom: 60px;
border: 1px solid var(--env-var-color-16);
border-radius: var(--env-var-radius-1);
+ background-color: var(--env-var-color-8);
}
.settings .config-box .MuiBox-root,
.settings .config-box .MuiStack-root {
@@ -39,4 +40,4 @@
display: flex;
flex-direction: column;
gap: var(--env-var-spacing-4);
-}
\ No newline at end of file
+}
diff --git a/Client/src/Pages/Settings/index.jsx b/Client/src/Pages/Settings/index.jsx
index 200830677..dcfe81318 100644
--- a/Client/src/Pages/Settings/index.jsx
+++ b/Client/src/Pages/Settings/index.jsx
@@ -110,7 +110,7 @@ const Settings = () => {
-
+
diff --git a/Client/src/assets/icons/discord-icon.svg b/Client/src/assets/icons/discord-icon.svg
new file mode 100644
index 000000000..c03e8e127
--- /dev/null
+++ b/Client/src/assets/icons/discord-icon.svg
@@ -0,0 +1,8 @@
+
+
+
\ No newline at end of file
diff --git a/Client/src/assets/icons/slack-icon.svg b/Client/src/assets/icons/slack-icon.svg
new file mode 100644
index 000000000..ff256fd4d
--- /dev/null
+++ b/Client/src/assets/icons/slack-icon.svg
@@ -0,0 +1,10 @@
+
+
+
\ No newline at end of file
diff --git a/Client/src/assets/icons/zapier-icon.svg b/Client/src/assets/icons/zapier-icon.svg
new file mode 100644
index 000000000..6ee0b5eca
--- /dev/null
+++ b/Client/src/assets/icons/zapier-icon.svg
@@ -0,0 +1,8 @@
+
+
+
\ No newline at end of file
diff --git a/Server/controllers/alertController.js b/Server/controllers/alertController.js
index cd2fa01ac..93d165469 100644
--- a/Server/controllers/alertController.js
+++ b/Server/controllers/alertController.js
@@ -27,7 +27,8 @@ const createAlert = async (req, res, next) => {
await createAlertBodyValidation.validateAsync(req.body);
} catch (error) {
error.status = 422;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
error.service = SERVICE_NAME;
next(error);
return;
@@ -58,7 +59,8 @@ const getAlertsByUserId = async (req, res, next) => {
await getAlertsByUserIdParamValidation.validateAsync(req.params);
} catch (error) {
error.status = 422;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
error.service = SERVICE_NAME;
next(error);
return;
@@ -82,7 +84,8 @@ const getAlertsByMonitorId = async (req, res, next) => {
await getAlertsByMonitorIdParamValidation.validateAsync(req.params);
} catch (error) {
error.status = 422;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
error.service = SERVICE_NAME;
next(error);
return;
@@ -106,7 +109,8 @@ const getAlertById = async (req, res, next) => {
await getAlertByIdParamValidation.validateAsync(req.params);
} catch (error) {
error.status = 422;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
error.service = SERVICE_NAME;
next(error);
return;
@@ -132,7 +136,8 @@ const editAlert = async (req, res, next) => {
} catch (error) {
console.log(error);
error.status = 422;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
error.service = SERVICE_NAME;
next(error);
return;
@@ -156,7 +161,8 @@ const deleteAlert = async (req, res, next) => {
await deleteAlertParamValidation.validateAsync(req.params);
} catch (error) {
error.status = 422;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
error.service = SERVICE_NAME;
next(error);
return;
diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js
index 88a7c7f71..6f34f71ce 100644
--- a/Server/controllers/authController.js
+++ b/Server/controllers/authController.js
@@ -54,7 +54,8 @@ const registerController = async (req, res, next) => {
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
@@ -115,14 +116,20 @@ const registerController = async (req, res, next) => {
*/
const loginController = async (req, res, next) => {
try {
- // Validate input
await loginValidation.validateAsync(req.body);
-
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+ try {
// Check if user exists
const user = await req.db.getUserByEmail(req, res);
// Compare password
-
const match = await user.comparePassword(req.body.password);
if (match !== true) {
throw new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
@@ -157,7 +164,8 @@ const userEditController = async (req, res, next) => {
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
@@ -216,8 +224,18 @@ const inviteController = async (req, res, next) => {
// Only admins can invite
const token = getTokenFromHeaders(req.headers);
const { role, firstname } = jwt.decode(token);
- await inviteRoleValidation.validateAsync({ roles: role });
- await inviteBodyValidation.validateAsync(req.body);
+ try {
+ await inviteRoleValidation.validateAsync({ roles: role });
+ await inviteBodyValidation.validateAsync(req.body);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+
const inviteToken = await req.db.requestInviteToken(req, res);
req.emailService.buildAndSendEmail(
"employeeActivationTemplate",
@@ -242,6 +260,16 @@ const inviteController = async (req, res, next) => {
const inviteVerifyController = async (req, res, next) => {
try {
await inviteVerifciationBodyValidation.validateAsync(req.body);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+
+ try {
const invite = await req.db.getInviteToken(req, res);
res
@@ -289,6 +317,16 @@ const checkAdminController = async (req, res) => {
const recoveryRequestController = async (req, res, next) => {
try {
await recoveryValidation.validateAsync(req.body);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+
+ try {
const user = await req.db.getUserByEmail(req, res);
if (user) {
const recoveryToken = await req.db.requestRecoveryToken(req, res);
@@ -328,6 +366,16 @@ const recoveryRequestController = async (req, res, next) => {
const validateRecoveryTokenController = async (req, res, next) => {
try {
await recoveryTokenValidation.validateAsync(req.body);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+
+ try {
await req.db.validateRecoveryToken(req, res);
// TODO Redirect user to reset password after validating token
return res.status(200).json({
@@ -354,6 +402,15 @@ const validateRecoveryTokenController = async (req, res, next) => {
const resetPasswordController = async (req, res, next) => {
try {
await newPasswordValidation.validateAsync(req.body);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+ try {
const user = await req.db.resetPassword(req, res);
const token = issueToken(user._doc);
res.status(200).json({
@@ -392,9 +449,18 @@ const deleteUserController = async (req, res, next) => {
};
// Validate user
- await deleteUserParamValidation.validateAsync(
- decodedTokenCastedAsRequest.body
- );
+ try {
+ await deleteUserParamValidation.validateAsync(
+ decodedTokenCastedAsRequest.body
+ );
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
// Check if the user exists
const user = await req.db.getUserByEmail(decodedTokenCastedAsRequest);
@@ -408,25 +474,20 @@ const deleteUserController = async (req, res, next) => {
);
if (monitors) {
- // 2. Delete jobs associated with each monitor
- for (const monitor of monitors) {
- await req.jobQueue.deleteJob(monitor);
- }
+ //2. Remove all jobs, delete checks and alerts
+ await Promise.all(
+ monitors.map(async (monitor) => {
+ await req.jobQueue.deleteJob(monitor);
+ await req.db.deleteChecks(monitor._id);
+ await req.db.deleteAlertByMonitorId(monitor._id);
+ await req.db.deleteNotificationsByMonitorId(monitor._id);
+ })
+ );
- // 3. Delete all checks associated with each monitor
- for (const monitor of monitors) {
- await req.db.deleteChecks(monitor._id);
- }
-
- // 4. Delete all alerts associated with each monitor
- for (const monitor of monitors) {
- await req.db.deleteAlertByMonitorId(monitor._id);
- }
-
- // 5. Delete each monitor
+ // 3. Delete each monitor
await req.db.deleteMonitorsByUserId(user._id);
- // 6. Delete the user by id
+ // 4. Delete the user by id
await req.db.deleteUser(decodedTokenCastedAsRequest);
return res.status(200).json({
diff --git a/Server/controllers/checkController.js b/Server/controllers/checkController.js
index 391ae2700..f3c85352f 100644
--- a/Server/controllers/checkController.js
+++ b/Server/controllers/checkController.js
@@ -14,7 +14,8 @@ const createCheck = async (req, res, next) => {
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
@@ -37,7 +38,8 @@ const getChecks = async (req, res, next) => {
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
@@ -59,20 +61,19 @@ const deleteChecks = async (req, res, next) => {
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
try {
const deletedCount = await req.db.deleteChecks(req.params.monitorId);
- return res
- .status(200)
- .json({
- success: true,
- msg: successMessages.CHECK_DELETE,
- data: { deletedCount },
- });
+ return res.status(200).json({
+ success: true,
+ msg: successMessages.CHECK_DELETE,
+ data: { deletedCount },
+ });
} catch (error) {
error.service = SERVICE_NAME;
next(error);
diff --git a/Server/controllers/maintenanceWindowController.js b/Server/controllers/maintenanceWindowController.js
index d94746fe3..5fe8ab7db 100644
--- a/Server/controllers/maintenanceWindowController.js
+++ b/Server/controllers/maintenanceWindowController.js
@@ -15,7 +15,8 @@ const createMaintenanceWindow = async (req, res, next) => {
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
@@ -48,7 +49,15 @@ const getMaintenanceWindowsByUserId = async (req, res, next) => {
await getMaintenanceWindowsByUserIdParamValidation.validateAsync(
req.params
);
-
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+ try {
const maintenanceWindows = await req.db.getMaintenanceWindowsByUserId(
req.params.userId
);
@@ -69,7 +78,16 @@ const getMaintenanceWindowsByMonitorId = async (req, res, next) => {
await getMaintenanceWindowsByMonitorIdParamValidation.validateAsync(
req.params
);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+ try {
const maintenanceWindows = await req.db.getMaintenanceWindowsByMonitorId(
req.params.monitorId
);
diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js
index 5e3899005..e05ed0389 100644
--- a/Server/controllers/monitorController.js
+++ b/Server/controllers/monitorController.js
@@ -2,14 +2,14 @@ const {
getMonitorByIdParamValidation,
getMonitorByIdQueryValidation,
getMonitorsByUserIdValidation,
- monitorValidation,
+ createMonitorBodyValidation,
editMonitorBodyValidation,
getMonitorsByUserIdQueryValidation,
} = require("../validation/joi");
const SERVICE_NAME = "monitorController";
const { errorMessages, successMessages } = require("../utils/messages");
-const { error } = require("winston");
+const { runInNewContext } = require("vm");
/**
* Returns all monitors
@@ -47,7 +47,8 @@ const getMonitorById = async (req, res, next) => {
await getMonitorByIdQueryValidation.validateAsync(req.query);
} catch (error) {
error.status = 422;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
@@ -83,6 +84,16 @@ const getMonitorsByUserId = async (req, res, next) => {
try {
await getMonitorsByUserIdValidation.validateAsync(req.params);
await getMonitorsByUserIdQueryValidation.validateAsync(req.query);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+
+ try {
const userId = req.params.userId;
const monitors = await req.db.getMonitorsByUserId(req, res);
@@ -108,17 +119,29 @@ const getMonitorsByUserId = async (req, res, next) => {
const createMonitor = async (req, res, next) => {
try {
- await monitorValidation.validateAsync(req.body);
+ await createMonitorBodyValidation.validateAsync(req.body);
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+
next(error);
return;
}
try {
+ const notifications = req.body.notifications;
const monitor = await req.db.createMonitor(req, res);
+
+ if (notifications && notifications.length !== 0) {
+ await Promise.all(
+ notifications.map(async (notification) => {
+ notification.monitorId = monitor._id;
+ await req.db.createNotification(notification);
+ })
+ );
+ }
// Add monitor to job queue
req.jobQueue.addJob(monitor._id, monitor);
return res.status(201).json({
@@ -143,21 +166,24 @@ const createMonitor = async (req, res, next) => {
const deleteMonitor = async (req, res, next) => {
try {
- await getMonitorByIdValidation.validateAsync(req.params);
+ await getMonitorByIdParamValidation.validateAsync(req.params);
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
try {
const monitor = await req.db.deleteMonitor(req, res, next);
- // Delete associated checks and alerts
+ // Delete associated checks,alerts,and notifications
await req.jobQueue.deleteJob(monitor);
await req.db.deleteChecks(monitor._id);
await req.db.deleteAlertByMonitorId(monitor._id);
+ await req.db.deleteNotificationsByMonitorId(monitor._id);
+
/**
* TODO
* We should remove all checks and alerts associated with this monitor
@@ -195,20 +221,38 @@ const deleteAllMonitors = async (req, res) => {
*/
const editMonitor = async (req, res, next) => {
try {
- await getMonitorByIdValidation.validateAsync(req.params);
+ await getMonitorByIdParamValidation.validateAsync(req.params);
await editMonitorBodyValidation.validateAsync(req.body);
} catch (error) {
error.status = 422;
error.service = SERVICE_NAME;
- error.message = error.details[0].message;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
next(error);
return;
}
try {
+ const monitorBeforeEdit = await req.db.getMonitorById(req, res);
+
+ // Get notifications from the request body
+ const notifications = req.body.notifications;
+
const editedMonitor = await req.db.editMonitor(req, res);
+
+ await req.db.deleteNotificationsByMonitorId(editedMonitor._id);
+
+ if (notifications && notifications.length !== 0) {
+ await Promise.all(
+ notifications.map(async (notification) => {
+ notification.monitorId = editedMonitor._id;
+ await req.db.createNotification(notification);
+ })
+ );
+ }
+
// Delete the old job(editedMonitor has the same ID as the old monitor)
- await req.jobQueue.deleteJob(editedMonitor);
+ await req.jobQueue.deleteJob(monitorBeforeEdit);
// Add the new job back to the queue
await req.jobQueue.addJob(editedMonitor._id, editedMonitor);
return res.status(200).json({
diff --git a/Server/controllers/pageSpeedCheckController.js b/Server/controllers/pageSpeedCheckController.js
index 7cc8696fc..eb290ad6e 100644
--- a/Server/controllers/pageSpeedCheckController.js
+++ b/Server/controllers/pageSpeedCheckController.js
@@ -1,4 +1,3 @@
-const PageSpeedCheck = require("../models/PageSpeedCheck");
const PageSpeedService = require("../service/pageSpeedService");
const { successMessages } = require("../utils/messages");
const pageSpeedService = new PageSpeedService();
@@ -22,12 +21,19 @@ const getPageSpeedChecks = async (req, res, next) => {
try {
// Validate monitorId parameter
await getPageSpeedCheckParamValidation.validateAsync(req.params);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+ try {
return res.status(200).json({ msg: "Hit getPageSpeedChecks" });
} catch (error) {
- if (error.isJoi) {
- return res.status(400).json({ error: error.details[0].message });
- }
+ error.service = SERVICE_NAME;
next(error);
}
};
@@ -42,12 +48,18 @@ const getPageSpeedChecks = async (req, res, next) => {
*/
const createPageSpeedCheck = async (req, res, next) => {
try {
- // Validate monitorId parameter
await createPageSpeedCheckParamValidation.validateAsync(req.params);
-
- // Validate request body
await createPageSpeedCheckBodyValidation.validateAsync(req.body);
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+ try {
const { monitorId } = req.params;
const { url } = req.body;
@@ -74,9 +86,6 @@ const createPageSpeedCheck = async (req, res, next) => {
data: newPageSpeedCheck,
});
} catch (error) {
- if (error.isJoi) {
- return res.status(400).json({ error: error.details[0].message });
- }
error.service = SERVICE_NAME;
next(error);
}
@@ -94,12 +103,17 @@ const deletePageSpeedCheck = async (req, res, next) => {
try {
// Validate monitorId parameter
await deletePageSpeedCheckParamValidation.validateAsync(req.params);
-
+ } catch (error) {
+ error.status = 422;
+ error.service = SERVICE_NAME;
+ error.message =
+ error.details?.[0]?.message || error.message || "Validation Error";
+ next(error);
+ return;
+ }
+ try {
return res.status(200).json({ msg: "Hit deletePageSpeedCheck" });
} catch (error) {
- if (error.isJoi) {
- return res.status(400).json({ error: error.details[0].message });
- }
error.service = SERVICE_NAME;
next(error);
}
diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js
index de2b3466e..2813a3c00 100644
--- a/Server/db/mongo/MongoDB.js
+++ b/Server/db/mongo/MongoDB.js
@@ -65,8 +65,6 @@ const {
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
- getMonitorByIdForIncidents,
- getMonitorsByUserIdForIncidents,
createMonitor,
deleteMonitor,
deleteAllMonitors,
@@ -120,6 +118,12 @@ const {
deleteMaintenanceWindowByUserId,
} = require("./modules/maintenaceWindowModule");
+const {
+ createNotification,
+ getNotificationsByMonitorId,
+ deleteNotificationsByMonitorId,
+} = require("./modules/notificationModule");
+
module.exports = {
connect,
insertUser,
@@ -136,8 +140,6 @@ module.exports = {
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
- getMonitorByIdForIncidents,
- getMonitorsByUserIdForIncidents,
createMonitor,
deleteMonitor,
deleteAllMonitors,
@@ -162,4 +164,7 @@ module.exports = {
deleteMaintenaceWindowById,
deleteMaintenanceWindowByMonitorId,
deleteMaintenanceWindowByUserId,
+ createNotification,
+ getNotificationsByMonitorId,
+ deleteNotificationsByMonitorId,
};
diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js
index a06ad9c05..9bca432c2 100644
--- a/Server/db/mongo/modules/monitorModule.js
+++ b/Server/db/mongo/modules/monitorModule.js
@@ -2,6 +2,7 @@ const Monitor = require("../../../models/Monitor");
const Check = require("../../../models/Check");
const PageSpeedCheck = require("../../../models/PageSpeedCheck");
const { errorMessages } = require("../../../utils/messages");
+const Notification = require("../../../models/Notification");
/**
* Get all monitors
@@ -30,7 +31,6 @@ const getAllMonitors = async (req, res) => {
*/
const getMonitorById = async (req, res) => {
try {
-
const { monitorId } = req.params;
let { status, limit, sortOrder } = req.query;
@@ -64,7 +64,8 @@ const getMonitorById = async (req, res) => {
createdAt: sortOrder,
})
.limit(limit);
- const monitorWithChecks = { ...monitor.toObject(), checks };
+ const notifications = await Notification.find({ monitorId: monitor._id });
+ const monitorWithChecks = { ...monitor.toObject(), checks, notifications };
return monitorWithChecks;
} catch (error) {
throw error;
@@ -120,7 +121,12 @@ const getMonitorsByUserId = async (req, res) => {
createdAt: sortOrder,
})
.limit(limit);
- return { ...monitor.toObject(), checks };
+
+ // Get notifications
+ const notifications = await Notification.find({
+ monitorId: monitor._id,
+ });
+ return { ...monitor.toObject(), checks, notifications };
})
);
return monitorsWithChecks;
@@ -140,6 +146,8 @@ const getMonitorsByUserId = async (req, res) => {
const createMonitor = async (req, res) => {
try {
const monitor = new Monitor({ ...req.body });
+ // Remove notifications fom monitor as they aren't needed here
+ monitor.notifications = undefined;
monitor.userId = req.user._id;
await monitor.save();
return monitor;
@@ -208,6 +216,8 @@ const deleteMonitorsByUserId = async (userId) => {
const editMonitor = async (req, res) => {
const candidateId = req.params.monitorId;
const candidateMonitor = req.body;
+ candidateMonitor.notifications = undefined;
+
try {
const editedMonitor = await Monitor.findByIdAndUpdate(
candidateId,
diff --git a/Server/db/mongo/modules/notificationModule.js b/Server/db/mongo/modules/notificationModule.js
new file mode 100644
index 000000000..f5c8708f4
--- /dev/null
+++ b/Server/db/mongo/modules/notificationModule.js
@@ -0,0 +1,50 @@
+const Notification = require("../../../models/Notification");
+
+/**
+ * Creates a new notification.
+ * @param {Object} notificationData - The data for the new notification.
+ * @param {mongoose.Types.ObjectId} notificationData.monitorId - The ID of the monitor.
+ * @param {string} notificationData.type - The type of the notification (e.g., "email", "sms").
+ * @param {string} [notificationData.address] - The address for the notification (if applicable).
+ * @param {string} [notificationData.phone] - The phone number for the notification (if applicable).
+ * @returns {Promise