diff --git a/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee b/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee index 5c91058146..4ce233406b 100644 --- a/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee +++ b/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee @@ -1,4 +1,4 @@ -UserGetter = require '../User/UserGetter' +InstitutionsGetter = require './InstitutionsGetter' PlansLocator = require '../Subscription/PlansLocator' Settings = require 'settings-sharelatex' logger = require 'logger-sharelatex' @@ -13,11 +13,10 @@ module.exports = InstitutionsFeatures = hasLicence: (userId, callback = (error, hasLicence) ->) -> - UserGetter.getUserFullEmails userId, (error, emailsData) -> + InstitutionsGetter.getConfirmedInstitutions userId, (error, institutions) -> return callback error if error? - affiliation = emailsData.find (emailData) -> - licence = emailData.affiliation?.institution?.licence - emailData.confirmedAt? and licence? and licence != 'free' + hasLicence = institutions.some (institution) -> + institution.licence and institution.licence != 'free' - callback(null, !!affiliation) + callback(null, hasLicence) diff --git a/services/web/app/coffee/Features/Institutions/InstitutionsGetter.coffee b/services/web/app/coffee/Features/Institutions/InstitutionsGetter.coffee new file mode 100644 index 0000000000..e5941dc777 --- /dev/null +++ b/services/web/app/coffee/Features/Institutions/InstitutionsGetter.coffee @@ -0,0 +1,14 @@ +UserGetter = require '../User/UserGetter' +logger = require 'logger-sharelatex' + +module.exports = InstitutionsGetter = + getConfirmedInstitutions: (userId, callback = (error, institutions) ->) -> + UserGetter.getUserFullEmails userId, (error, emailsData) -> + return callback error if error? + + confirmedInstitutions = emailsData.filter (emailData) -> + emailData.confirmedAt? and emailData.affiliation?.institution? + .map (emailData) -> + emailData.affiliation?.institution + + callback(null, confirmedInstitutions) diff --git a/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee b/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee index 2304f2e5b7..8811f72c35 100644 --- a/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee +++ b/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee @@ -8,11 +8,11 @@ modulePath = require('path').join __dirname, '../../../../app/js/Features/Instit describe 'InstitutionsFeatures', -> beforeEach -> - @UserGetter = getUserFullEmails: sinon.stub() + @InstitutionsGetter = getConfirmedInstitutions: sinon.stub() @PlansLocator = findLocalPlanInSettings: sinon.stub() @institutionPlanCode = 'institution_plan_code' @InstitutionsFeatures = SandboxedModule.require modulePath, requires: - '../User/UserGetter': @UserGetter + './InstitutionsGetter': @InstitutionsGetter '../Subscription/PlansLocator': @PlansLocator 'settings-sharelatex': institutionPlanCode: @institutionPlanCode 'logger-sharelatex': @@ -23,47 +23,37 @@ describe 'InstitutionsFeatures', -> describe "hasLicence", -> it 'should handle error', (done)-> - @UserGetter.getUserFullEmails.yields(new Error('Nope')) + @InstitutionsGetter.getConfirmedInstitutions.yields(new Error('Nope')) @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> expect(error).to.exist done() - it 'should return false if user has no affiliations', (done) -> - @UserGetter.getUserFullEmails.yields(null, []) - @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> - expect(error).to.not.exist - expect(hasLicence).to.be.false - done() - it 'should return false if user has no confirmed affiliations', (done) -> - affiliations = [ - { confirmedAt: null, affiliation: institution: { licence: 'pro_plus' } } - ] - @UserGetter.getUserFullEmails.yields(null, affiliations) + institutions = [] + @InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions) @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> expect(error).to.not.exist expect(hasLicence).to.be.false done() it 'should return false if user has no paid affiliations', (done) -> - affiliations = [ - { confirmedAt: new Date(), affiliation: institution: { licence: 'free' } } + institutions = [ + { licence: 'free' } ] - @UserGetter.getUserFullEmails.yields(null, affiliations) + @InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions) @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> expect(error).to.not.exist expect(hasLicence).to.be.false done() it 'should return true if user has confirmed paid affiliation', (done)-> - affiliations = [ - { confirmedAt: new Date(), affiliation: institution: { licence: 'pro_plus' } } - { confirmedAt: new Date(), affiliation: institution: { licence: 'free' } } - { confirmedAt: null, affiliation: institution: { licence: 'pro' } } - { confirmedAt: null, affiliation: institution: { licence: null } } - { confirmedAt: new Date(), affiliation: institution: {} } + institutions = [ + { licence: 'pro_plus' } + { licence: 'free' } + { licence: 'pro' } + { licence: null } ] - @UserGetter.getUserFullEmails.yields(null, affiliations) + @InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions) @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> expect(error).to.not.exist expect(hasLicence).to.be.true diff --git a/services/web/test/unit/coffee/Institutions/InstitutionsGetterTests.coffee b/services/web/test/unit/coffee/Institutions/InstitutionsGetterTests.coffee new file mode 100644 index 0000000000..62c57cdaa9 --- /dev/null +++ b/services/web/test/unit/coffee/Institutions/InstitutionsGetterTests.coffee @@ -0,0 +1,44 @@ +SandboxedModule = require('sandboxed-module') +require('chai').should() +expect = require('chai').expect +sinon = require('sinon') +modulePath = require('path').join __dirname, '../../../../app/js/Features/Institutions/InstitutionsGetter.js' + +describe 'InstitutionsGetter', -> + beforeEach -> + @UserGetter = getUserFullEmails: sinon.stub() + @InstitutionsGetter = SandboxedModule.require modulePath, requires: + '../User/UserGetter': @UserGetter + 'logger-sharelatex': + log:-> console.log(arguments) + err:-> + + @userId = '12345abcde' + + describe "getConfirmedInstitutions", -> + it 'filters unconfirmed emails', (done) -> + @userEmails = [ + { confirmedAt: null, affiliation: institution: { id: 123 } } + { confirmedAt: new Date(), affiliation: institution: { id: 456 } } + { confirmedAt: new Date(), affiliation: null } + { confirmedAt: new Date(), affiliation: institution: null } + ] + @UserGetter.getUserFullEmails.yields(null, @userEmails) + @InstitutionsGetter.getConfirmedInstitutions @userId, (error, institutions) -> + expect(error).to.not.exist + institutions.length.should.equal 1 + institutions[0].id.should.equal 456 + done() + + it 'should handle empty response', (done) -> + @UserGetter.getUserFullEmails.yields(null, []) + @InstitutionsGetter.getConfirmedInstitutions @userId, (error, institutions) -> + expect(error).to.not.exist + institutions.length.should.equal 0 + done() + + it 'should handle error', (done) -> + @UserGetter.getUserFullEmails.yields(new Error('Nope')) + @InstitutionsGetter.getConfirmedInstitutions @userId, (error, institutions) -> + expect(error).to.exist + done()