diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 8f7e21453..e05ed0389 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -234,7 +234,23 @@ const editMonitor = async (req, res, next) => { 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(monitorBeforeEdit); // Add the new job back to the queue diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index f82496551..6f795e35a 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -209,6 +209,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,