From c3514fd4532723496e4a2911aead686d1b1726d1 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 5 Feb 2016 07:38:32 -0200 Subject: [PATCH] add mark key endpoint --- services/notifications/app.coffee | 3 ++- .../app/coffee/HealthCheckController.coffee | 6 ++++- .../app/coffee/Notifications.coffee | 10 ++++++- .../app/coffee/NotificationsController.coffee | 14 +++++++--- .../coffee/NotificationsControllerTest.coffee | 26 ++++++++++++++----- .../unit/coffee/NotificationsTests.coffee | 19 +++++++++++--- 6 files changed, 61 insertions(+), 17 deletions(-) diff --git a/services/notifications/app.coffee b/services/notifications/app.coffee index e831015aba..dc6262cfbe 100644 --- a/services/notifications/app.coffee +++ b/services/notifications/app.coffee @@ -22,7 +22,8 @@ app.configure ()-> app.post '/user/:user_id', controller.addNotification app.get '/user/:user_id', controller.getUserNotifications -app.del '/user/:user_id/notification/:notification_id', controller.removeNotification +app.del '/user/:user_id/notification/:notification_id', controller.removeNotificationId +app.del '/user/:user_id', controller.removeNotificationKey app.get '/status', (req, res)-> res.send('notifications sharelatex up') diff --git a/services/notifications/app/coffee/HealthCheckController.coffee b/services/notifications/app/coffee/HealthCheckController.coffee index b41dc0290a..7c78ea37a6 100644 --- a/services/notifications/app/coffee/HealthCheckController.coffee +++ b/services/notifications/app/coffee/HealthCheckController.coffee @@ -41,6 +41,10 @@ module.exports = return callback(err) else notification_id = body[1][0]._id + notification_key = body[1][0].key opts = getOpts("/notification/#{notification_id}") request.del opts, (err, res, body)-> - db.notifications.remove {_id:ObjectId(notification_id)}, callback + opts = getOpts("") + opts.json = {key: notification_key} + request.del opts, (err, res, body)-> + db.notifications.remove {_id:ObjectId(notification_id)}, callback diff --git a/services/notifications/app/coffee/Notifications.coffee b/services/notifications/app/coffee/Notifications.coffee index 3ae01e8150..a24c8662a5 100644 --- a/services/notifications/app/coffee/Notifications.coffee +++ b/services/notifications/app/coffee/Notifications.coffee @@ -29,10 +29,18 @@ module.exports = templateKey: notification.templateKey db.notifications.insert(doc, callback) - removeNotification: (user_id, notification_id, callback)-> + removeNotificationId: (user_id, notification_id, callback)-> searchOps = user_id:ObjectId(user_id) _id:ObjectId(notification_id) updateOperation = "$unset": {templateKey:true} db.notifications.update searchOps, updateOperation, callback + + removeNotificationKey: (user_id, notification_key, callback)-> + searchOps = + user_id:ObjectId(user_id) + 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 2633451b3b..6ee7c68cbc 100644 --- a/services/notifications/app/coffee/NotificationsController.coffee +++ b/services/notifications/app/coffee/NotificationsController.coffee @@ -19,8 +19,14 @@ module.exports = else res.send() - removeNotification: (req, res)-> - logger.log user_id: req.params.user_id, notification_id: req.params.notification_id, "mark notification as read" - metrics.inc "removeNotification" - Notifications.removeNotification req.params.user_id, req.params.notification_id, (err, notifications)-> + removeNotificationId: (req, res)-> + logger.log user_id: req.params.user_id, notification_id: req.params.notification_id, "mark id notification as read" + metrics.inc "removeNotificationId" + Notifications.removeNotificationId req.params.user_id, req.params.notification_id, (err, notifications)-> + res.send() + + removeNotificationKey: (req, res)-> + logger.log user_id: req.params.user_id, notification_key: req.body.key, "mark key notification as read" + metrics.inc "removeNotificationKey" + Notifications.removeNotificationKey req.params.user_id, req.body.key, (err, notifications)-> res.send() diff --git a/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee b/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee index d8349c8a1f..286abdf00e 100644 --- a/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee +++ b/services/notifications/test/unit/coffee/NotificationsControllerTest.coffee @@ -7,8 +7,9 @@ assert = require('assert') user_id = "51dc93e6fb625a261300003b" notification_id = "fb625a26f09d" +notification_key = "my-notification-key" -describe 'Notifications controller', -> +describe 'Notifications Controller', -> beforeEach -> self = @ @notifications = {} @@ -18,7 +19,7 @@ describe 'Notifications controller', -> 'metrics-sharelatex': inc: sinon.stub() - @stubbedNotification = [{key:"notification-key", messageOpts:"some info", templateKey:"template-key"}] + @stubbedNotification = [{key: notification_key, messageOpts:"some info", templateKey:"template-key"}] describe "getUserNotifications", -> it 'should ask the notifications for the users notifications', (done)-> @@ -42,13 +43,24 @@ describe 'Notifications controller', -> @notifications.addNotification.calledWith(user_id, @stubbedNotification).should.equal true done() - describe "removeNotification", -> - it "should tell the notifications to mark the notification as read", (done)-> - @notifications.removeNotification = sinon.stub().callsArgWith(2) + describe "removeNotificationId", -> + it "should tell the notifications to mark the notification Id as read", (done)-> + @notifications.removeNotificationId = sinon.stub().callsArgWith(2) req = params: user_id: user_id notification_id: notification_id - @controller.removeNotification req, send:(result)=> - @notifications.removeNotification.calledWith(user_id, notification_id).should.equal true + @controller.removeNotificationId req, send:(result)=> + @notifications.removeNotificationId.calledWith(user_id, notification_id).should.equal true done() + + describe "removeNotificationKey", -> + it "should tell the notifications to mark the notification Key as read", (done)-> + @notifications.removeNotificationKey = sinon.stub().callsArgWith(2) + 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 diff --git a/services/notifications/test/unit/coffee/NotificationsTests.coffee b/services/notifications/test/unit/coffee/NotificationsTests.coffee index aa1755dbf1..191324df1d 100644 --- a/services/notifications/test/unit/coffee/NotificationsTests.coffee +++ b/services/notifications/test/unit/coffee/NotificationsTests.coffee @@ -10,7 +10,7 @@ user_id = "51dc93e6fb625a261300003b" notification_id = "fb625a26f09d" notification_key = "notification-key" -describe 'creating a user', -> +describe 'Notifications Tests', -> beforeEach -> self = @ @findStub = sinon.stub() @@ -60,11 +60,11 @@ describe 'creating a user', -> @insertStub.calledWith(@stubbedNotification).should.equal false done() - describe 'removeNotification', -> + describe 'removeNotificationId', -> it 'should mark the notification id as read', (done)-> @updateStub.callsArgWith(2, null) - @notifications.removeNotification user_id, notification_id, (err)=> + @notifications.removeNotificationId user_id, notification_id, (err)=> searchOps = user_id:ObjectId(user_id) _id:ObjectId(notification_id) @@ -72,3 +72,16 @@ describe 'creating a user', -> "$unset": {templateKey:true} @updateStub.calledWith(searchOps, updateOperation).should.equal true done() + + describe 'removeNotificationKey', -> + it 'should mark the notification key as read', (done)-> + @updateStub.callsArgWith(2, null) + + @notifications.removeNotificationKey user_id, notification_key, (err)=> + searchOps = + user_id:ObjectId(user_id) + key: notification_key + updateOperation = + "$unset": {templateKey:true} + @updateStub.calledWith(searchOps, updateOperation).should.equal true + done() \ No newline at end of file