From a7c93aee3935e7971bd6f54d52fdffbd65b37312 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 30 Jul 2024 13:19:37 -0700 Subject: [PATCH 1/3] Add DB operations for notifications --- Server/db/mongo/modules/notificationModule.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Server/db/mongo/modules/notificationModule.js diff --git a/Server/db/mongo/modules/notificationModule.js b/Server/db/mongo/modules/notificationModule.js new file mode 100644 index 000000000..7ed2d2064 --- /dev/null +++ b/Server/db/mongo/modules/notificationModule.js @@ -0,0 +1,41 @@ +const Notification = require("../../../models/Notification"); +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} The created notification. + * @throws Will throw an error if the notification cannot be created. + */ +const createNotification = async (notificationData) => { + try { + const notification = await new Notification({ ...notificationData }).save(); + return notification; + } catch (error) { + throw error; + } +}; + +/** + * Retrieves notifications by monitor ID. + * @param {mongoose.Types.ObjectId} monitorId - The ID of the monitor. + * @returns {Promise>} An array of notifications. + * @throws Will throw an error if the notifications cannot be retrieved. + */ +const getNotificationsByMonitorId = async (monitorId) => { + try { + const notifications = await Notification.find({ monitorId }); + return notifications; + } catch (error) { + throw error; + } +}; + +module.exports = { + createNotification, + getNotificationsByMonitorId, +}; From 03ee67cfca51a680fcc7ff741872d59729d7ffc4 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 30 Jul 2024 13:23:14 -0700 Subject: [PATCH 2/3] Add methods to MongoDB --- Server/controllers/monitorController.js | 1 + Server/db/mongo/MongoDB.js | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 5e3899005..c22bf50c3 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -158,6 +158,7 @@ const deleteMonitor = async (req, res, next) => { await req.jobQueue.deleteJob(monitor); await req.db.deleteChecks(monitor._id); await req.db.deleteAlertByMonitorId(monitor._id); + await req.db.deleteNotificationByMonitorId(monitor._id); /** * TODO * We should remove all checks and alerts associated with this monitor diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index de2b3466e..c03e2db1a 100644 --- a/Server/db/mongo/MongoDB.js +++ b/Server/db/mongo/MongoDB.js @@ -120,6 +120,11 @@ const { deleteMaintenanceWindowByUserId, } = require("./modules/maintenaceWindowModule"); +const { + createNotification, + getNotificationsByMonitorId, +} = require("./modules/notificationModule"); + module.exports = { connect, insertUser, @@ -162,4 +167,6 @@ module.exports = { deleteMaintenaceWindowById, deleteMaintenanceWindowByMonitorId, deleteMaintenanceWindowByUserId, + createNotification, + getNotificationsByMonitorId, }; From 1664bc09eaa9e99b6c5a889a348bdccf05505f6b Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 30 Jul 2024 13:25:56 -0700 Subject: [PATCH 3/3] Add delete method --- Server/db/mongo/MongoDB.js | 6 ++---- Server/db/mongo/modules/notificationModule.js | 11 ++++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index c03e2db1a..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, @@ -123,6 +121,7 @@ const { const { createNotification, getNotificationsByMonitorId, + deleteNotificationsByMonitorId, } = require("./modules/notificationModule"); module.exports = { @@ -141,8 +140,6 @@ module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId, - getMonitorByIdForIncidents, - getMonitorsByUserIdForIncidents, createMonitor, deleteMonitor, deleteAllMonitors, @@ -169,4 +166,5 @@ module.exports = { deleteMaintenanceWindowByUserId, createNotification, getNotificationsByMonitorId, + deleteNotificationsByMonitorId, }; diff --git a/Server/db/mongo/modules/notificationModule.js b/Server/db/mongo/modules/notificationModule.js index 7ed2d2064..f5c8708f4 100644 --- a/Server/db/mongo/modules/notificationModule.js +++ b/Server/db/mongo/modules/notificationModule.js @@ -1,5 +1,4 @@ const Notification = require("../../../models/Notification"); -const Notification = require("../../../models/Notification"); /** * Creates a new notification. @@ -35,7 +34,17 @@ const getNotificationsByMonitorId = async (monitorId) => { } }; +const deleteNotificationsByMonitorId = async (monitorId) => { + try { + const result = await Notification.deleteMany({ monitorId }); + return result.deletedCount; + } catch (error) { + throw error; + } +}; + module.exports = { createNotification, getNotificationsByMonitorId, + deleteNotificationsByMonitorId, };