diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee index 5616de886b..6fd9e4b1be 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee @@ -53,20 +53,31 @@ module.exports = CollaboratorsHandler = last_name: 1, signUpDate: 1 } + _loadMembers: (members, callback=(error, users) ->) -> + result = [] + async.mapLimit members, 3, + (member, cb) -> + UserGetter.getUserById member.id, CollaboratorsHandler.USER_PROJECTION, (error, user) -> + return cb(error) if error? + if user? + result.push { user: user, privilegeLevel: member.privilegeLevel } + cb() + (error) -> + return callback(error) if error? + callback null, result + getMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) -> CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members = []) -> return callback(error) if error? - result = [] - async.mapLimit members, 3, - (member, cb) -> - UserGetter.getUserById member.id, CollaboratorsHandler.USER_PROJECTION, (error, user) -> - return cb(error) if error? - if user? - result.push { user: user, privilegeLevel: member.privilegeLevel } - cb() - (error) -> - return callback(error) if error? - callback null, result + CollaboratorsHandler._loadMembers members, (error, users) -> + callback error, users + + getInvitedMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) -> + CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members = []) -> + return callback(error) if error? + members = members.filter((m) -> m.source == Sources.INVITE) + CollaboratorsHandler._loadMembers members, (error, users) -> + callback error, users getMemberIdPrivilegeLevel: (user_id, project_id, callback = (error, privilegeLevel) ->) -> # In future if the schema changes and getting all member ids is more expensive (multiple documents) diff --git a/services/web/app/coffee/Features/Editor/EditorHttpController.coffee b/services/web/app/coffee/Features/Editor/EditorHttpController.coffee index ed23b53b1a..4288ad4cfb 100644 --- a/services/web/app/coffee/Features/Editor/EditorHttpController.coffee +++ b/services/web/app/coffee/Features/Editor/EditorHttpController.coffee @@ -35,7 +35,7 @@ module.exports = EditorHttpController = ProjectGetter.getProjectWithoutDocLines project_id, (error, project) -> return callback(error) if error? return callback(new Error("not found")) if !project? - CollaboratorsHandler.getMembersWithPrivilegeLevels project, (error, members) -> + CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels project, (error, members) -> return callback(error) if error? UserGetter.getUser user_id, { isAdmin: true }, (error, user) -> return callback(error) if error? diff --git a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee index c447379b90..16df525d78 100644 --- a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee @@ -87,9 +87,9 @@ describe "CollaboratorsHandler", -> beforeEach -> @CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub() @CollaboratorHandler.getMemberIdsWithPrivilegeLevels.withArgs(@project_id).yields(null, [ - { id: "read-only-ref-1", privilegeLevel: "readOnly", source: 'invite' } + { id: "read-only-ref-1", privilegeLevel: "readOnly", source: 'token' } { id: "read-only-ref-2", privilegeLevel: "readOnly", source: 'invite' } - { id: "read-write-ref-1", privilegeLevel: "readAndWrite", source: 'invite' } + { id: "read-write-ref-1", privilegeLevel: "readAndWrite", source: 'token' } { id: "read-write-ref-2", privilegeLevel: "readAndWrite", source: 'invite' } { id: "doesnt-exist", privilegeLevel: "readAndWrite", source: 'invite' } ]) @@ -111,6 +111,32 @@ describe "CollaboratorsHandler", -> ]) .should.equal true + describe "getInvitedMembersWithPrivilegeLevels", -> + beforeEach -> + @CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub() + @CollaboratorHandler.getMemberIdsWithPrivilegeLevels.withArgs(@project_id).yields(null, [ + { id: "read-only-ref-1", privilegeLevel: "readOnly", source: 'token' } + { id: "read-only-ref-2", privilegeLevel: "readOnly", source: 'invite' } + { id: "read-write-ref-1", privilegeLevel: "readAndWrite", source: 'token' } + { id: "read-write-ref-2", privilegeLevel: "readAndWrite", source: 'invite' } + { id: "doesnt-exist", privilegeLevel: "readAndWrite", source: 'invite' } + ]) + @UserGetter.getUserById = sinon.stub() + @UserGetter.getUserById.withArgs("read-only-ref-1").yields(null, { _id: "read-only-ref-1" }) + @UserGetter.getUserById.withArgs("read-only-ref-2").yields(null, { _id: "read-only-ref-2" }) + @UserGetter.getUserById.withArgs("read-write-ref-1").yields(null, { _id: "read-write-ref-1" }) + @UserGetter.getUserById.withArgs("read-write-ref-2").yields(null, { _id: "read-write-ref-2" }) + @UserGetter.getUserById.withArgs("doesnt-exist").yields(null, null) + @CollaboratorHandler.getInvitedMembersWithPrivilegeLevels @project_id, @callback + + it "should return an array of invited members with their privilege levels", -> + @callback + .calledWith(null, [ + { user: { _id: "read-only-ref-2" }, privilegeLevel: "readOnly" } + { user: { _id: "read-write-ref-2" }, privilegeLevel: "readAndWrite" } + ]) + .should.equal true + describe "getMemberIdPrivilegeLevel", -> beforeEach -> @CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub() diff --git a/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee b/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee index 76079e07ab..73e0b80d18 100644 --- a/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee @@ -109,7 +109,7 @@ describe "EditorHttpController", -> ] @ProjectEditorHandler.buildProjectModelView = sinon.stub().returns(@projectModelView) @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project) - @CollaboratorsHandler.getMembersWithPrivilegeLevels = sinon.stub().callsArgWith(1, null, @members) + @CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels = sinon.stub().callsArgWith(1, null, @members) @CollaboratorsInviteHandler.getAllInvites = sinon.stub().callsArgWith(1, null, @invites) @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user) @@ -125,7 +125,7 @@ describe "EditorHttpController", -> .should.equal true it "should get the list of users in the project", -> - @CollaboratorsHandler.getMembersWithPrivilegeLevels + @CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels .calledWith(@project) .should.equal true