From 7280262f9279bd52aee7f7293b1f2bed2ce5115c Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Wed, 17 Aug 2016 10:52:35 +0100 Subject: [PATCH] add the option to override exisiting notification --- .../app/coffee/Notifications.coffee | 17 ++++++++++++++--- .../test/unit/coffee/NotificationsTests.coffee | 13 ++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/services/notifications/app/coffee/Notifications.coffee b/services/notifications/app/coffee/Notifications.coffee index e8edae5aab..3c7bcd70c8 100644 --- a/services/notifications/app/coffee/Notifications.coffee +++ b/services/notifications/app/coffee/Notifications.coffee @@ -13,14 +13,25 @@ module.exports = db.notifications.find query, (err, notifications)-> callback err, notifications - addNotification: (user_id, notification, callback)-> + + _checkExistingNotifcationAndOverride : (user_id, notification, callback)-> + self = @ query = user_id: ObjectId(user_id) key: notification.key db.notifications.count query, (err, number)-> - if number > 0 + if number > 0 and !notification.override logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user" - callback number + return callback(number) + else if number > 0 and notification.override + self.removeNotificationKey user_id, notification.key, callback + else + callback() + + addNotification: (user_id, notification, callback)-> + @_checkExistingNotifcationAndOverride user_id, notification, (err)-> + if err? + callback err else doc = user_id: ObjectId(user_id) diff --git a/services/notifications/test/unit/coffee/NotificationsTests.coffee b/services/notifications/test/unit/coffee/NotificationsTests.coffee index c09c67bb1d..d1c9c6d8e1 100644 --- a/services/notifications/test/unit/coffee/NotificationsTests.coffee +++ b/services/notifications/test/unit/coffee/NotificationsTests.coffee @@ -72,11 +72,22 @@ describe 'Notifications Tests', -> it 'should fail insert of existing notification key', (done)-> @insertStub.callsArgWith(1, null) @countStub.callsArgWith(1, null, 1) - + @notifications.removeNotificationKey = sinon.stub() @notifications.addNotification user_id, @stubbedNotification, (err)=> @insertStub.calledWith(@expectedDocument).should.equal false done() + describe "when key already exists but override is passed", (done)-> + + it "should delete the old key and insert the new one", -> + @insertStub.callsArgWith(1, null) + @countStub.callsArgWith(1, null, 1) + @stubbedNotification.override = true + @notifications.addNotification user_id, @stubbedNotification, (err)=> + assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument) + @notifications.removeNotificationKey.calledWith(user_id, @stubbedNotification.key).should.equal true + done() + describe 'when the notification is set to expire', () -> beforeEach -> @stubbedNotification = {