Merge pull request #2593 from overleaf/ta-affiliation-licence-check

Check Licence on Affiliations Rather Than Institutions

GitOrigin-RevId: 7effe7f564ff953e60ed77bcdf92f3cb177d4aee
This commit is contained in:
Simon Detheridge 2020-02-18 11:36:53 +00:00 committed by Copybot
parent 19266bbc8a
commit b812109cb7
6 changed files with 54 additions and 27 deletions

View file

@ -53,16 +53,16 @@ module.exports = InstitutionsFeatures = {
if (callback == null) {
callback = function(error, hasLicence) {}
}
return InstitutionsGetter.getConfirmedInstitutions(userId, function(
return InstitutionsGetter.getConfirmedAffiliations(userId, function(
error,
institutions
affiliations
) {
if (error != null) {
return callback(error)
}
const hasLicence = institutions.some(
institution => institution.licence && institution.licence !== 'free'
const hasLicence = affiliations.some(
affiliation => affiliation.licence && affiliation.licence !== 'free'
)
return callback(null, hasLicence)

View file

@ -20,7 +20,7 @@ const UserMembershipEntityConfigs = require('../UserMembership/UserMembershipEnt
const logger = require('logger-sharelatex')
module.exports = InstitutionsGetter = {
getConfirmedInstitutions(userId, callback) {
getConfirmedAffiliations(userId, callback) {
if (callback == null) {
callback = function(error, institutions) {}
}
@ -29,7 +29,7 @@ module.exports = InstitutionsGetter = {
return callback(error)
}
const confirmedInstitutions = emailsData
const confirmedAffiliations = emailsData
.filter(
emailData =>
emailData.confirmedAt != null &&
@ -40,15 +40,33 @@ module.exports = InstitutionsGetter = {
x => x.confirmed
)
)
.map(
emailData =>
emailData.affiliation != null
? emailData.affiliation.institution
.map(emailData => emailData.affiliation)
return callback(null, confirmedAffiliations)
})
},
getConfirmedInstitutions(userId, callback) {
if (callback == null) {
callback = function(error, institutions) {}
}
InstitutionsGetter.getConfirmedAffiliations(
userId,
(error, confirmedAffiliations) => {
if (error != null) {
return callback(error)
}
const confirmedInstitutions = confirmedAffiliations.map(
confirmedAffiliation =>
confirmedAffiliation != null
? confirmedAffiliation.institution
: undefined
)
return callback(null, confirmedInstitutions)
})
return callback(null, confirmedInstitutions)
}
)
},
getManagedInstitutions(user_id, callback) {

View file

@ -155,8 +155,14 @@ var decorateFullEmails = (defaultEmail, emailsData, affiliationsData) =>
aff => aff.email === emailData.email
)
if (affiliation) {
const { institution, inferred, role, department } = affiliation
emailData.affiliation = { institution, inferred, role, department }
const { institution, inferred, role, department, licence } = affiliation
emailData.affiliation = {
institution,
inferred,
role,
department,
licence
}
} else {
emailsData.affiliation = null
}

View file

@ -156,7 +156,8 @@ describe('FeatureUpdater.refreshFeatures', function() {
this.email = this.user.emails[0].email
return (this.affiliationData = {
email: this.email,
institution: { licence: 'pro_plus', confirmed: true }
licence: 'pro_plus',
institution: { confirmed: true }
})
})

View file

@ -22,7 +22,7 @@ const modulePath = require('path').join(
describe('InstitutionsFeatures', function() {
beforeEach(function() {
this.InstitutionsGetter = { getConfirmedInstitutions: sinon.stub() }
this.InstitutionsGetter = { getConfirmedAffiliations: sinon.stub() }
this.PlansLocator = { findLocalPlanInSettings: sinon.stub() }
this.institutionPlanCode = 'institution_plan_code'
this.InstitutionsFeatures = SandboxedModule.require(modulePath, {
@ -47,7 +47,7 @@ describe('InstitutionsFeatures', function() {
describe('hasLicence', function() {
it('should handle error', function(done) {
this.InstitutionsGetter.getConfirmedInstitutions.yields(new Error('Nope'))
this.InstitutionsGetter.getConfirmedAffiliations.yields(new Error('Nope'))
return this.InstitutionsFeatures.hasLicence(
this.userId,
(error, hasLicence) => {
@ -58,10 +58,10 @@ describe('InstitutionsFeatures', function() {
})
it('should return false if user has no confirmed affiliations', function(done) {
const institutions = []
this.InstitutionsGetter.getConfirmedInstitutions.yields(
const affiliations = []
this.InstitutionsGetter.getConfirmedAffiliations.yields(
null,
institutions
affiliations
)
return this.InstitutionsFeatures.hasLicence(
this.userId,
@ -74,10 +74,10 @@ describe('InstitutionsFeatures', function() {
})
it('should return false if user has no paid affiliations', function(done) {
const institutions = [{ licence: 'free' }]
this.InstitutionsGetter.getConfirmedInstitutions.yields(
const affiliations = [{ licence: 'free' }]
this.InstitutionsGetter.getConfirmedAffiliations.yields(
null,
institutions
affiliations
)
return this.InstitutionsFeatures.hasLicence(
this.userId,
@ -90,15 +90,15 @@ describe('InstitutionsFeatures', function() {
})
it('should return true if user has confirmed paid affiliation', function(done) {
const institutions = [
const affiliations = [
{ licence: 'pro_plus' },
{ licence: 'free' },
{ licence: 'pro' },
{ licence: null }
]
this.InstitutionsGetter.getConfirmedInstitutions.yields(
this.InstitutionsGetter.getConfirmedAffiliations.yields(
null,
institutions
affiliations
)
return this.InstitutionsFeatures.hasLicence(
this.userId,

View file

@ -143,6 +143,7 @@ describe('UserGetter', function() {
role: 'Prof',
department: 'Maths',
inferred: false,
licence: 'pro_plus',
institution: { name: 'University Name', isUniversity: true }
}
]
@ -159,7 +160,8 @@ describe('UserGetter', function() {
institution: affiliationsData[0].institution,
inferred: affiliationsData[0].inferred,
department: affiliationsData[0].department,
role: affiliationsData[0].role
role: affiliationsData[0].role,
licence: affiliationsData[0].licence
}
},
{