Henrique Dias 10 лет назад
Родитель
Сommit
c3514fd453

+ 2 - 1
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')

+ 5 - 1
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

+ 9 - 1
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

+ 10 - 4
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()

+ 19 - 7
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()

+ 16 - 3
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()