From de0589b0518b3cb836ef54b21bfb3151bab1e7a1 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Fri, 5 Feb 2016 14:13:38 +0000 Subject: [PATCH] added notifications calls for sending to api and mark as read not creating it yet --- .../Notifications/NotificationsBuilder.coffee | 16 +++++++++++ .../Notifications/NotificationsHandler.coffee | 21 ++++++++++++++ .../SubscriptionDomainHandler.coffee | 14 ++++++---- .../NotificationsHandlerTests.coffee | 28 +++++++++++++++++-- 4 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee diff --git a/services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee b/services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee new file mode 100644 index 0000000000..c68e265920 --- /dev/null +++ b/services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee @@ -0,0 +1,16 @@ + +NotificationsHandler = require("./NotificationsHandler") + +module.exports = + + groupPlan: (user, licence)-> + key : "join-sub-#{licence.subscription_id}" + + create: (callback = ->)-> + messageOpts = + groupName: licence.name + subscription_id: licence.subscription_id + NotificationsHandler.createNotification user._id, key, "joinSubscriptionInvite", messageOpts, callback + + read: (callback = ->)-> + NotificationsHandler.markAsReadWithKey user._id, key, callback diff --git a/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee b/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee index ef20351c90..d621040f69 100644 --- a/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee +++ b/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee @@ -21,6 +21,27 @@ module.exports = unreadNotifications = [] callback(null, unreadNotifications) + createNotification: (user_id, key, templateKey, messageOpts, callback)-> + opts = + uri: "#{settings.apis.notifications.url}/user/#{user_id}" + timeout: 1000 + json: { + key:key + messageOpts:messageOpts + templateKey:templateKey + } + request.post opts, callback + + markAsReadWithKey: (user_id, key, callback)-> + opts = + uri: "#{settings.apis.notifications.url}/user/#{user_id}" + timeout: 1000 + json: { + key:key + } + request.del opts, callback + + markAsRead: (user_id, notification_id, callback)-> opts = uri: "#{settings.apis.notifications.url}/user/#{user_id}/notification/#{notification_id}" diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionDomainHandler.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionDomainHandler.coffee index b57c3492f6..0e655c45fc 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionDomainHandler.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionDomainHandler.coffee @@ -1,3 +1,4 @@ +NotificationsBuilder = require("../Notifications/NotificationsBuilder") async = require("async") _ = require("underscore") settings = require("settings-sharelatex") @@ -7,17 +8,18 @@ _s = require("underscore.string") module.exports = SubscriptionDomainHandler = - getLicenceUserCanJoin: (user, callback)-> + getLicenceUserCanJoin: (user)-> licence = SubscriptionDomainHandler._findDomainLicence(user.email) - if licence? - callback null, licence - else - callback() + return licence attemptToJoinGroup: (user, callback)-> licence = SubscriptionDomainHandler._findDomainLicence(user.email) if licence? and user.emailVerified - SubscriptionGroupHandler.addUserToGroup licence.adminUser_id, user.email, callback + SubscriptionGroupHandler.addUserToGroup licence.adminUser_id, user.email, (err)-> + if err? + logger.err err:err, "error adding user to group" + return callback(err) + NotificationsBuilder.groupPlan(user, licence).read() else callback "user not verified" diff --git a/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee b/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee index d134753efa..d63d1ca4a1 100644 --- a/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee @@ -42,7 +42,31 @@ describe 'NotificationsHandler', -> unreadNotifications.length.should.equal 0 describe "markAsRead", -> + beforeEach -> + @key = "some key here" + it 'should send a delete request when a delete has been received to mark a notification', (done)-> - @handler.markAsRead user_id, notification_id, => - @request.del.calledWith({uri:"#{notificationUrl}/user/#{user_id}/notification/#{notification_id}", timeout:1000}).should.equal true + @handler.markAsReadWithKey user_id, @key, => + opts = + uri: "#{notificationUrl}/user/#{user_id}" + json: + key:@key + timeout:1000 + @request.del.calledWith(opts).should.equal true done() + + + describe "createNotification", -> + beforeEach -> + @key = "some key here" + @messageOpts = {value:12344} + @templateKey = "renderThisHtml" + + it "should post the message over", (done)-> + @handler.createNotification user_id, @key, @templateKey, @messageOpts, => + args = @request.post.args[0][0] + args.uri.should.equal "#{notificationUrl}/user/#{user_id}" + args.timeout.should.equal 1000 + expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts} + assert.deepEqual(args.json, expectedJson) + done() \ No newline at end of file