From a50bdaf5cc8985535b81145c5f97474d6f5a689e Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 7 Mar 2016 15:32:04 +0000 Subject: [PATCH] Refactor LimitationsManager to use CollaboratorsHandler --- .../Subscription/LimitationsManager.coffee | 8 ++------ .../Subscription/LimitationsManagerTests.coffee | 15 ++------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee b/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee index d323c19a9d..e2f633e185 100644 --- a/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee +++ b/services/web/app/coffee/Features/Subscription/LimitationsManager.coffee @@ -3,6 +3,7 @@ Project = require("../../models/Project").Project User = require("../../models/User").User SubscriptionLocator = require("./SubscriptionLocator") Settings = require("settings-sharelatex") +CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler") module.exports = @@ -13,16 +14,11 @@ module.exports = callback null, owner.features.collaborators else callback null, Settings.defaultPlanCode.collaborators - - currentNumberOfCollaboratorsInProject: (project_id, callback) -> - Project.findById project_id, 'collaberator_refs readOnly_refs', (error, project) -> - return callback(error) if error? - callback null, (project.collaberator_refs.length + project.readOnly_refs.length) canAddXCollaborators: (project_id, x_collaborators, callback = (error, allowed)->) -> @allowedNumberOfCollaboratorsInProject project_id, (error, allowed_number) => return callback(error) if error? - @currentNumberOfCollaboratorsInProject project_id, (error, current_number) => + CollaboratorsHandler.getCollaboratorCount project_id, (error, current_number) => return callback(error) if error? if current_number + x_collaborators <= allowed_number or allowed_number < 0 callback null, true diff --git a/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee b/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee index df1edca280..581d5cd6da 100644 --- a/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Subscription/LimitationsManagerTests.coffee @@ -29,6 +29,7 @@ describe "LimitationsManager", -> '../../models/User' : User: @User './SubscriptionLocator':@SubscriptionLocator 'settings-sharelatex' : @Settings = {} + "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {} 'logger-sharelatex':log:-> describe "allowedNumberOfCollaboratorsInProject", -> @@ -51,22 +52,10 @@ describe "LimitationsManager", -> it "should return the number of collaborators the user is allowed", -> @callback.calledWith(null, @user.features.collaborators).should.equal true - - describe "currentNumberOfCollaboratorsInProject", -> - beforeEach -> - @project.collaberator_refs = ["one", "two"] - @project.readOnly_refs = ["three"] - @callback = sinon.stub() - @LimitationsManager.currentNumberOfCollaboratorsInProject(@project_id, @callback) - - it "should return the total number of collaborators", -> - @callback.calledWith(null, 3).should.equal true describe "canAddXCollaborators", -> beforeEach -> - sinon.stub @LimitationsManager, - "currentNumberOfCollaboratorsInProject", - (project_id, callback) => callback(null, @current_number) + @CollaboratorsHandler.getCollaboratorCount = (project_id, callback) => callback(null, @current_number) sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) => callback(null, @allowed_number)