add the option to override exisiting notification

This commit is contained in:
Henry Oswald 2016-08-17 10:52:35 +01:00
parent 0d9e0000ae
commit 7280262f92
2 changed files with 26 additions and 4 deletions

View file

@ -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)

View file

@ -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 = {