From 05e390ef8927228b6548e8396d3ea2f359dd8f4d Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 11 Aug 2016 11:05:11 +0100 Subject: [PATCH] Add an optional expiry to notifications. --- .../app/coffee/Notifications.coffee | 4 ++ .../unit/coffee/NotificationsTests.coffee | 48 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/services/notifications/app/coffee/Notifications.coffee b/services/notifications/app/coffee/Notifications.coffee index bb752d35af..4238ae37e9 100644 --- a/services/notifications/app/coffee/Notifications.coffee +++ b/services/notifications/app/coffee/Notifications.coffee @@ -27,6 +27,10 @@ module.exports = key: notification.key messageOpts: notification.messageOpts templateKey: notification.templateKey + expires: notification.expires || false + # ttl index on `expiresFrom` field + if doc.expires + doc.expiresFrom = new Date() 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 46d21db649..c09867a39f 100644 --- a/services/notifications/test/unit/coffee/NotificationsTests.coffee +++ b/services/notifications/test/unit/coffee/NotificationsTests.coffee @@ -1,5 +1,6 @@ sinon = require('sinon') chai = require('chai') +expect = chai.should should = chai.should() modulePath = "../../../app/js/Notifications.js" SandboxedModule = require('sandboxed-module') @@ -44,12 +45,27 @@ describe 'Notifications Tests', -> done() describe 'addNotification', -> + beforeEach -> + @stubbedNotification = { + user_id: ObjectId(user_id), + key:"notification-key", + messageOpts:"some info", + templateKey:"template-key" + } + @expectedDocument = { + user_id: @stubbedNotification.user_id, + key:"notification-key", + messageOpts:"some info", + templateKey:"template-key", + expires: false + } + it 'should insert the notification into the collection', (done)-> @insertStub.callsArgWith(1, null) @countStub.callsArgWith(1, null, 0) @notifications.addNotification user_id, @stubbedNotification, (err)=> - @insertStub.calledWith(@stubbedNotification).should.equal true + @insertStub.calledWith(@expectedDocument).should.equal true done() it 'should fail insert of existing notification key', (done)-> @@ -57,9 +73,37 @@ describe 'Notifications Tests', -> @countStub.callsArgWith(1, null, 1) @notifications.addNotification user_id, @stubbedNotification, (err)=> - @insertStub.calledWith(@stubbedNotification).should.equal false + @insertStub.calledWith(@expectedDocument).should.equal false done() + describe 'when the notification is set to expire', () -> + beforeEach -> + @stubbedNotification = { + user_id: ObjectId(user_id), + key:"notification-key", + messageOpts:"some info", + templateKey:"template-key", + expires: true + } + @expectedDocument = { + user_id: @stubbedNotification.user_id, + key:"notification-key", + messageOpts:"some info", + templateKey:"template-key", + expires: true, + expiresFrom: new Date() + } + + it 'should add an `expiresFrom` Date field to the inserted notification', (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 + done() + describe 'removeNotificationId', -> it 'should mark the notification id as read', (done)-> @updateStub.callsArgWith(2, null)