From fc4bd94a6e5ff6a81ef8ff6d2c232abedc2fdaf7 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Thu, 18 Feb 2016 10:06:21 +0000 Subject: [PATCH] don't create notification if user is already part of group --- .../Subscription/SubscriptionLocator.coffee | 2 +- .../coffee/Features/User/UserHandler.coffee | 20 ++++-- .../coffee/User/UserHandlerTests.coffee | 69 +++++++++++++++++++ 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 services/web/test/UnitTests/coffee/User/UserHandlerTests.coffee diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee index 08f5507b7f..9285f8e575 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee @@ -22,4 +22,4 @@ module.exports = Subscription.findOne _id:subscription_id, callback getSubscriptionByMemberIdAndId: (user_id, subscription_id, callback)-> - Subscription.findOne member_ids: user_id, _id:subscription_id, callback + Subscription.findOne member_ids: user_id, _id:subscription_id, {_id:1}, callback diff --git a/services/web/app/coffee/Features/User/UserHandler.coffee b/services/web/app/coffee/Features/User/UserHandler.coffee index 68001d0f77..2fb232bef9 100644 --- a/services/web/app/coffee/Features/User/UserHandler.coffee +++ b/services/web/app/coffee/Features/User/UserHandler.coffee @@ -1,12 +1,20 @@ SubscriptionDomainHandler = require("../Subscription/SubscriptionDomainHandler") NotificationsBuilder = require("../Notifications/NotificationsBuilder") +SubscriptionGroupHandler = require("../Subscription/SubscriptionGroupHandler") +module.exports = UserHandler = -module.exports = + _populateGroupLicenceInvite: (user, callback)-> + licence = SubscriptionDomainHandler.getLicenceUserCanJoin user + if !licence? + return callback() + + SubscriptionGroupHandler.isUserPartOfGroup user._id, licence.subscription_id, (err, alreadyPartOfGroup)-> + if err? or alreadyPartOfGroup + return callback(err) + else + NotificationsBuilder.groupPlan(user, licence).create(callback) setupLoginData: (user, callback = ->)-> - licence = SubscriptionDomainHandler.getLicenceUserCanJoin user - if licence? - NotificationsBuilder.groupPlan(user, licence).create(callback) - else - return callback() + _populateGroupLicenceInvite user, callback + diff --git a/services/web/test/UnitTests/coffee/User/UserHandlerTests.coffee b/services/web/test/UnitTests/coffee/User/UserHandlerTests.coffee new file mode 100644 index 0000000000..8e463c69eb --- /dev/null +++ b/services/web/test/UnitTests/coffee/User/UserHandlerTests.coffee @@ -0,0 +1,69 @@ +sinon = require('sinon') +chai = require('chai') +should = chai.should() +modulePath = "../../../../app/js/Features/User/UserHandler.js" +SandboxedModule = require('sandboxed-module') + +describe "UserHandler", -> + + beforeEach -> + @user = + _id:"12390i" + email: "bob@bob.com" + remove: sinon.stub().callsArgWith(0) + + @licence = + subscription_id: 12323434 + @SubscriptionDomainHandler = + getLicenceUserCanJoin: sinon.stub() + + @SubscriptionGroupHandler = + isUserPartOfGroup:sinon.stub() + @createStub = sinon.stub().callsArgWith(0) + @NotificationsBuilder = + groupPlan:sinon.stub().returns({create:@createStub}) + + @UserHandler = SandboxedModule.require modulePath, requires: + "logger-sharelatex": @logger = { log: sinon.stub() } + "../Notifications/NotificationsBuilder":@NotificationsBuilder + "../Subscription/SubscriptionDomainHandler":@SubscriptionDomainHandler + "../Subscription/SubscriptionGroupHandler":@SubscriptionGroupHandler + + describe "_populateGroupLicenceInvite", -> + + describe "no licence", -> + beforeEach (done)-> + @SubscriptionDomainHandler.getLicenceUserCanJoin.returns() + @UserHandler._populateGroupLicenceInvite @user, done + + it "should not call NotificationsBuilder", (done)-> + @NotificationsBuilder.groupPlan.called.should.equal false + done() + + it "should not call isUserPartOfGroup", (done)-> + @SubscriptionGroupHandler.isUserPartOfGroup.called.should.equal false + done() + + describe "with matching licence user is not in", -> + + beforeEach (done)-> + @SubscriptionDomainHandler.getLicenceUserCanJoin.returns(@licence) + @SubscriptionGroupHandler.isUserPartOfGroup.callsArgWith(2, null, false) + @UserHandler._populateGroupLicenceInvite @user, done + + it "should create notifcation", (done)-> + @NotificationsBuilder.groupPlan.calledWith(@user, @licence).should.equal true + done() + + + + describe "with matching licence user is already in", -> + + beforeEach (done)-> + @SubscriptionDomainHandler.getLicenceUserCanJoin.returns(@licence) + @SubscriptionGroupHandler.isUserPartOfGroup.callsArgWith(2, null, true) + @UserHandler._populateGroupLicenceInvite @user, done + + it "should create notifcation", (done)-> + @NotificationsBuilder.groupPlan.called.should.equal false + done() \ No newline at end of file