diff --git a/services/notifications/README.md b/services/notifications/README.md index 9db344dae4..4b2b4184b0 100644 --- a/services/notifications/README.md +++ b/services/notifications/README.md @@ -7,8 +7,8 @@ An API for managing user notifications in ShareLaTeX database indexes ================ -For notification expiry to work, a ttl index on `notifications.expiresFrom` must be created: +For notification expiry to work, a TTL index on `notifications.expires` must be created: ```javascript -db.notifications.createIndex({expiresFrom: 1}, {expireAfterSeconds: (60*60*24*30)}) +db.notifications.createIndex({expires: 1}, {expireAfterSeconds: 10}) ``` diff --git a/services/notifications/app/coffee/Notifications.coffee b/services/notifications/app/coffee/Notifications.coffee index a9053734b9..2a7e043f78 100644 --- a/services/notifications/app/coffee/Notifications.coffee +++ b/services/notifications/app/coffee/Notifications.coffee @@ -27,12 +27,12 @@ module.exports = key: notification.key messageOpts: notification.messageOpts templateKey: notification.templateKey - # ttl index on `expiresFrom` field - # in Mongo, TTL indexes only work on date fields, - # and ignore the document when that field is missing + # TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to + # a datetime in the future when the document should be automatically removed. + # in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing + # see `README.md` for instruction on creating TTL index if notification.expires? - doc.expires = notification.expires - doc.expiresFrom = new Date() + doc.expires = new Date(notification.expires) db.notifications.insert(doc, callback) removeNotificationId: (user_id, notification_id, callback)-> diff --git a/services/notifications/test/unit/coffee/NotificationsTests.coffee b/services/notifications/test/unit/coffee/NotificationsTests.coffee index 51215cb697..144b8407be 100644 --- a/services/notifications/test/unit/coffee/NotificationsTests.coffee +++ b/services/notifications/test/unit/coffee/NotificationsTests.coffee @@ -55,7 +55,7 @@ describe 'Notifications Tests', -> user_id: @stubbedNotification.user_id, key:"notification-key", messageOpts:"some info", - templateKey:"template-key", + templateKey:"template-key" } it 'should insert the notification into the collection', (done)-> @@ -81,25 +81,23 @@ describe 'Notifications Tests', -> key:"notification-key", messageOpts:"some info", templateKey:"template-key", - expires: true + expires: '2922-02-13T09:32:56.289Z' } @expectedDocument = { user_id: @stubbedNotification.user_id, key:"notification-key", messageOpts:"some info", templateKey:"template-key", - expires: true, - expiresFrom: new Date() + expires: new Date(@stubbedNotification.expires), } - it 'should add an `expiresFrom` Date field to the inserted notification', (done)-> + it 'should add an `expires` Date field to the document', (done)-> @insertStub.callsArgWith(1, null) @countStub.callsArgWith(1, null, 0) @notifications.addNotification user_id, @stubbedNotification, (err)=> @insertStub.callCount.should.equal 1 - Object.keys(@insertStub.lastCall.args[0]).should.deep.equal Object.keys(@expectedDocument) - @insertStub.firstCall.args[0].expiresFrom.should.be.instanceof Date + @insertStub.calledWith(@expectedDocument).should.equal true done() describe 'removeNotificationId', ->