add mark key endpoint

This commit is contained in:
Henrique Dias 2016-02-05 07:38:32 -02:00
parent 6291a8d2cf
commit c3514fd453
6 changed files with 61 additions and 17 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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