From fc78bfe86ca1b3e54881c09a75f3bcf097e05ca4 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 11 Aug 2016 10:01:21 +0100 Subject: [PATCH] Add an endpoint to remove a notification by its key alone --- services/notifications/app.coffee | 1 + .../app/coffee/Notifications.coffee | 17 +++++++++++----- .../app/coffee/NotificationsController.coffee | 7 +++++++ .../coffee/NotificationsControllerTest.coffee | 20 ++++++++++++++----- .../unit/coffee/NotificationsTests.coffee | 20 +++++++++++++++---- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/services/notifications/app.coffee b/services/notifications/app.coffee index 8b78c4b8f8..7008c814b0 100644 --- a/services/notifications/app.coffee +++ b/services/notifications/app.coffee @@ -24,6 +24,7 @@ app.post '/user/:user_id', controller.addNotification app.get '/user/:user_id', controller.getUserNotifications app.del '/user/:user_id/notification/:notification_id', controller.removeNotificationId app.del '/user/:user_id', controller.removeNotificationKey +app.del '/notification/key/:key', controller.removeNotificationByKeyOnly app.get '/status', (req, res)-> res.send('notifications sharelatex up') diff --git a/services/notifications/app/coffee/Notifications.coffee b/services/notifications/app/coffee/Notifications.coffee index f9a6adc86a..bb752d35af 100644 --- a/services/notifications/app/coffee/Notifications.coffee +++ b/services/notifications/app/coffee/Notifications.coffee @@ -22,7 +22,7 @@ module.exports = logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user" callback number else - doc = + doc = user_id: ObjectId(user_id) key: notification.key messageOpts: notification.messageOpts @@ -30,17 +30,24 @@ module.exports = db.notifications.insert(doc, callback) removeNotificationId: (user_id, notification_id, callback)-> - searchOps = + searchOps = user_id:ObjectId(user_id) _id:ObjectId(notification_id) - updateOperation = + updateOperation = "$unset": {templateKey:true, messageOpts: true} db.notifications.update searchOps, updateOperation, callback removeNotificationKey: (user_id, notification_key, callback)-> - searchOps = + searchOps = user_id:ObjectId(user_id) key: notification_key - updateOperation = + updateOperation = + "$unset": {templateKey:true} + db.notifications.update searchOps, updateOperation, callback + + removeNotificationByKeyOnly: (notification_key, callback)-> + searchOps = + key: notification_key + updateOperation = "$unset": {templateKey:true} db.notifications.update searchOps, updateOperation, callback diff --git a/services/notifications/app/coffee/NotificationsController.coffee b/services/notifications/app/coffee/NotificationsController.coffee index 6ee7c68cbc..416cdeccc0 100644 --- a/services/notifications/app/coffee/NotificationsController.coffee +++ b/services/notifications/app/coffee/NotificationsController.coffee @@ -30,3 +30,10 @@ module.exports = metrics.inc "removeNotificationKey" Notifications.removeNotificationKey req.params.user_id, req.body.key, (err, notifications)-> res.send() + + removeNotificationByKeyOnly: (req, res)-> + notification_key = req.params.key + logger.log {notification_key}, "mark notification as read by key only" + metrics.inc "removeNotificationKey" + Notifications.removeNotificationByKeyOnly notification_key, (err, notifications)-> + res.send() diff --git a/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee b/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee index 286abdf00e..c7c8fdd987 100644 --- a/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee +++ b/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee @@ -24,7 +24,7 @@ describe 'Notifications Controller', -> describe "getUserNotifications", -> it 'should ask the notifications for the users notifications', (done)-> @notifications.getUserNotifications = sinon.stub().callsArgWith(1, null, @stubbedNotification) - req = + req = params: user_id: user_id @controller.getUserNotifications req, json:(result)=> @@ -35,7 +35,7 @@ describe 'Notifications Controller', -> describe "addNotification", -> it "should tell the notifications to add the notification for the user", (done)-> @notifications.addNotification = sinon.stub().callsArgWith(2) - req = + req = params: user_id: user_id body: @stubbedNotification @@ -46,7 +46,7 @@ describe 'Notifications Controller', -> describe "removeNotificationId", -> it "should tell the notifications to mark the notification Id as read", (done)-> @notifications.removeNotificationId = sinon.stub().callsArgWith(2) - req = + req = params: user_id: user_id notification_id: notification_id @@ -57,10 +57,20 @@ describe 'Notifications Controller', -> describe "removeNotificationKey", -> it "should tell the notifications to mark the notification Key as read", (done)-> @notifications.removeNotificationKey = sinon.stub().callsArgWith(2) - req = + req = params: user_id: user_id body: {key: notification_key} @controller.removeNotificationKey req, send:(result)=> @notifications.removeNotificationKey.calledWith(user_id, notification_key).should.equal true - done() \ No newline at end of file + done() + + describe "removeNotificationByKeyOnly", -> + it "should tell the notifications to mark the notification Key as read", (done)-> + @notifications.removeNotificationByKeyOnly = sinon.stub().callsArgWith(1) + req = + params: + key: notification_key + @controller.removeNotificationByKeyOnly req, send:(result)=> + @notifications.removeNotificationByKeyOnly.calledWith(notification_key).should.equal true + done() diff --git a/services/notifications/test/unit/coffee/NotificationsTests.coffee b/services/notifications/test/unit/coffee/NotificationsTests.coffee index 47f31a3d06..46d21db649 100644 --- a/services/notifications/test/unit/coffee/NotificationsTests.coffee +++ b/services/notifications/test/unit/coffee/NotificationsTests.coffee @@ -65,10 +65,10 @@ describe 'Notifications Tests', -> @updateStub.callsArgWith(2, null) @notifications.removeNotificationId user_id, notification_id, (err)=> - searchOps = + searchOps = user_id:ObjectId(user_id) _id:ObjectId(notification_id) - updateOperation = + updateOperation = "$unset": {templateKey:true, messageOpts:true} @updateStub.calledWith(searchOps, updateOperation).should.equal true done() @@ -78,10 +78,22 @@ describe 'Notifications Tests', -> @updateStub.callsArgWith(2, null) @notifications.removeNotificationKey user_id, notification_key, (err)=> - searchOps = + searchOps = user_id:ObjectId(user_id) key: notification_key - updateOperation = + updateOperation = + "$unset": {templateKey:true} + @updateStub.calledWith(searchOps, updateOperation).should.equal true + done() + + describe 'removeNotificationByKeyOnly', -> + it 'should mark the notification key as read', (done)-> + @updateStub.callsArgWith(2, null) + + @notifications.removeNotificationByKeyOnly notification_key, (err)=> + searchOps = + key: notification_key + updateOperation = "$unset": {templateKey:true} @updateStub.calledWith(searchOps, updateOperation).should.equal true done()