Просмотр исходного кода

add the option to override exisiting notification

Henry Oswald 10 лет назад
Родитель
Сommit
7280262f92

+ 14 - 3
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)

+ 12 - 1
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 = {