diff --git a/services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee b/services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee index b9f7500809..ffb71f70e6 100644 --- a/services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee +++ b/services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee @@ -12,7 +12,7 @@ module.exports = groupName: licence.name subscription_id: licence.subscription_id logger.log user_id:user._id, key:key, "creating notification key for user" - NotificationsHandler.createNotification user._id, @key, "notification_group_invite", messageOpts, callback + NotificationsHandler.createNotification user._id, @key, "notification_group_invite", messageOpts, null, callback read: (callback = ->)-> NotificationsHandler.markAsReadWithKey user._id, @key, callback @@ -26,6 +26,6 @@ module.exports = projectId: project._id.toString() token: invite.token logger.log {user_id: user._id, project_id: project._id, invite_id: invite._id, key: @key}, "creating project invite notification for user" - NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, callback + NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, invite.expires, callback read: (callback=()->) -> NotificationsHandler.markAsReadByKeyOnly @key, callback diff --git a/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee b/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee index 55ed4e8a9a..9fc21659c9 100644 --- a/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee +++ b/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee @@ -29,16 +29,19 @@ module.exports = unreadNotifications = [] callback(null, unreadNotifications) - createNotification: (user_id, key, templateKey, messageOpts, callback)-> + createNotification: (user_id, key, templateKey, messageOpts, expiryDateTime, callback)-> + payload = { + key:key + messageOpts:messageOpts + templateKey:templateKey + } + if expiryDateTime? + payload.expires = expiryDateTime opts = uri: "#{settings.apis.notifications?.url}/user/#{user_id}" timeout: oneSecond method:"POST" - json: { - key:key - messageOpts:messageOpts - templateKey:templateKey - } + json: payload logger.log opts:opts, "creating notification for user" makeRequest opts, callback diff --git a/services/web/app/coffee/models/ProjectInvite.coffee b/services/web/app/coffee/models/ProjectInvite.coffee index 2e3309397b..9b9e0cb350 100644 --- a/services/web/app/coffee/models/ProjectInvite.coffee +++ b/services/web/app/coffee/models/ProjectInvite.coffee @@ -6,7 +6,13 @@ Schema = mongoose.Schema ObjectId = Schema.ObjectId -THIRTY_DAYS_IN_SECONDS = 60 * 60 * 24 * 30 +EXPIRY_IN_SECONDS = 60 * 60 * 24 * 30 + +ExpiryDate = () -> + timestamp = new Date() + timestamp.setSeconds(timestamp.getSeconds() + EXPIRY_IN_SECONDS) + return timestamp + ProjectInviteSchema = new Schema( @@ -16,7 +22,8 @@ ProjectInviteSchema = new Schema( sendingUserId: ObjectId projectId: ObjectId privileges: String - createdAt: {type: Date, default: Date.now, index: {expireAfterSeconds: THIRTY_DAYS_IN_SECONDS}} + createdAt: {type: Date, default: Date.now} + expires: {type: Date, default: ExpiryDate, index: {expireAfterSeconds: 10}} }, { collection: 'projectInvites' @@ -29,7 +36,7 @@ conn = mongoose.createConnection(Settings.mongo.url, server: poolSize: Settings. ProjectInvite = conn.model('ProjectInvite', ProjectInviteSchema) - mongoose.model 'ProjectInvite', ProjectInviteSchema exports.ProjectInvite = ProjectInvite exports.ProjectInviteSchema = ProjectInviteSchema +exports.EXPIRY_IN_SECONDS = EXPIRY_IN_SECONDS diff --git a/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee b/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee index 3554836fa5..9629bca8c7 100644 --- a/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee @@ -60,9 +60,10 @@ describe 'NotificationsHandler', -> @key = "some key here" @messageOpts = {value:12344} @templateKey = "renderThisHtml" + @expiry = null it "should post the message over", (done)-> - @handler.createNotification user_id, @key, @templateKey, @messageOpts, => + @handler.createNotification user_id, @key, @templateKey, @messageOpts, @expiry, => args = @request.args[0][0] args.uri.should.equal "#{notificationUrl}/user/#{user_id}" args.timeout.should.equal 1000 @@ -70,6 +71,22 @@ describe 'NotificationsHandler', -> assert.deepEqual(args.json, expectedJson) done() + describe 'when expiry date is supplied', -> + beforeEach -> + @key = "some key here" + @messageOpts = {value:12344} + @templateKey = "renderThisHtml" + @expiry = new Date() + + it 'should post the message over with expiry field', (done) -> + @handler.createNotification user_id, @key, @templateKey, @messageOpts, @expiry, => + args = @request.args[0][0] + args.uri.should.equal "#{notificationUrl}/user/#{user_id}" + args.timeout.should.equal 1000 + expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts, expires: @expiry} + assert.deepEqual(args.json, expectedJson) + done() + describe "markAsReadByKeyOnly", -> beforeEach -> @key = "some key here"