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

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', ->
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

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