From 8adf5ce463d1eec67e6b150e6b8d9b3788a8ae53 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Fri, 6 Jan 2023 13:53:16 +1300 Subject: [PATCH] Add user-specified DismissKey to Add Notification --- server/notifications.model.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/server/notifications.model.js b/server/notifications.model.js index d09b80ce6..0465822d5 100644 --- a/server/notifications.model.js +++ b/server/notifications.model.js @@ -30,15 +30,17 @@ NotificationSchema.statics.getByKey = function(key, fields=null){ }); }; -NotificationSchema.statics.addNotification = async function(title, text, startAt=new Date, stopAt=new Date){ +NotificationSchema.statics.addNotification = async function(dismissKey, text, title=null, startAt=new Date, stopAt=new Date){ const data = { - title : title, - text : text, - startAt : startAt, - stopAt : stopAt + dismissKey : dismissKey, + title : title, + text : text, + startAt : startAt, + stopAt : stopAt }; const newNotification = new Notification(data); - await newNotification.save(); + await newNotification.save() + .catch((err)=>{return err;}); return newNotification; }; @@ -46,6 +48,9 @@ NotificationSchema.statics.addNotification = async function(title, text, startAt NotificationSchema.statics.updateNotification = async function(dismissKey, title=null, text=null, startAt=null, stopAt=null){ if(!dismissKey) return 'No key!'; if(!title && !text && !startAt && !stopAt) return 'No data!'; + const filter = { + dismissKey : dismissKey + }; const data = { title : title, text : text, @@ -56,7 +61,7 @@ NotificationSchema.statics.updateNotification = async function(dismissKey, title if(value === null) delete data[key]; } - await Notification.updateOne(data) + await Notification.findOneAndUpdate(filter, data, { new: true }) .exec((err, notifications)=>{ if(err) return reject('Can not find notification'); return resolve(notifications);