Merge pull request #807 from sharelatex/ta-affiliations-segmentation

Create InstitutionsGetter
This commit is contained in:
Alasdair Smith 2018-08-13 09:45:50 +01:00 committed by GitHub
commit 5a0853d894
4 changed files with 77 additions and 30 deletions

View file

@ -1,4 +1,4 @@
UserGetter = require '../User/UserGetter' InstitutionsGetter = require './InstitutionsGetter'
PlansLocator = require '../Subscription/PlansLocator' PlansLocator = require '../Subscription/PlansLocator'
Settings = require 'settings-sharelatex' Settings = require 'settings-sharelatex'
logger = require 'logger-sharelatex' logger = require 'logger-sharelatex'
@ -13,11 +13,10 @@ module.exports = InstitutionsFeatures =
hasLicence: (userId, callback = (error, hasLicence) ->) -> hasLicence: (userId, callback = (error, hasLicence) ->) ->
UserGetter.getUserFullEmails userId, (error, emailsData) -> InstitutionsGetter.getConfirmedInstitutions userId, (error, institutions) ->
return callback error if error? return callback error if error?
affiliation = emailsData.find (emailData) -> hasLicence = institutions.some (institution) ->
licence = emailData.affiliation?.institution?.licence institution.licence and institution.licence != 'free'
emailData.confirmedAt? and licence? and licence != 'free'
callback(null, !!affiliation) callback(null, hasLicence)

View file

@ -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)

View file

@ -8,11 +8,11 @@ modulePath = require('path').join __dirname, '../../../../app/js/Features/Instit
describe 'InstitutionsFeatures', -> describe 'InstitutionsFeatures', ->
beforeEach -> beforeEach ->
@UserGetter = getUserFullEmails: sinon.stub() @InstitutionsGetter = getConfirmedInstitutions: sinon.stub()
@PlansLocator = findLocalPlanInSettings: sinon.stub() @PlansLocator = findLocalPlanInSettings: sinon.stub()
@institutionPlanCode = 'institution_plan_code' @institutionPlanCode = 'institution_plan_code'
@InstitutionsFeatures = SandboxedModule.require modulePath, requires: @InstitutionsFeatures = SandboxedModule.require modulePath, requires:
'../User/UserGetter': @UserGetter './InstitutionsGetter': @InstitutionsGetter
'../Subscription/PlansLocator': @PlansLocator '../Subscription/PlansLocator': @PlansLocator
'settings-sharelatex': institutionPlanCode: @institutionPlanCode 'settings-sharelatex': institutionPlanCode: @institutionPlanCode
'logger-sharelatex': 'logger-sharelatex':
@ -23,47 +23,37 @@ describe 'InstitutionsFeatures', ->
describe "hasLicence", -> describe "hasLicence", ->
it 'should handle error', (done)-> it 'should handle error', (done)->
@UserGetter.getUserFullEmails.yields(new Error('Nope')) @InstitutionsGetter.getConfirmedInstitutions.yields(new Error('Nope'))
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
expect(error).to.exist expect(error).to.exist
done() 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) -> it 'should return false if user has no confirmed affiliations', (done) ->
affiliations = [ institutions = []
{ confirmedAt: null, affiliation: institution: { licence: 'pro_plus' } } @InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
]
@UserGetter.getUserFullEmails.yields(null, affiliations)
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
expect(error).to.not.exist expect(error).to.not.exist
expect(hasLicence).to.be.false expect(hasLicence).to.be.false
done() done()
it 'should return false if user has no paid affiliations', (done) -> it 'should return false if user has no paid affiliations', (done) ->
affiliations = [ institutions = [
{ confirmedAt: new Date(), affiliation: institution: { licence: 'free' } } { licence: 'free' }
] ]
@UserGetter.getUserFullEmails.yields(null, affiliations) @InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
expect(error).to.not.exist expect(error).to.not.exist
expect(hasLicence).to.be.false expect(hasLicence).to.be.false
done() done()
it 'should return true if user has confirmed paid affiliation', (done)-> it 'should return true if user has confirmed paid affiliation', (done)->
affiliations = [ institutions = [
{ confirmedAt: new Date(), affiliation: institution: { licence: 'pro_plus' } } { licence: 'pro_plus' }
{ confirmedAt: new Date(), affiliation: institution: { licence: 'free' } } { licence: 'free' }
{ confirmedAt: null, affiliation: institution: { licence: 'pro' } } { licence: 'pro' }
{ confirmedAt: null, affiliation: institution: { licence: null } } { licence: null }
{ confirmedAt: new Date(), affiliation: institution: {} }
] ]
@UserGetter.getUserFullEmails.yields(null, affiliations) @InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) -> @InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
expect(error).to.not.exist expect(error).to.not.exist
expect(hasLicence).to.be.true expect(hasLicence).to.be.true

View file

@ -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()