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) { if (callback == null) {
callback = function(error, hasLicence) {} callback = function(error, hasLicence) {}
} }
return InstitutionsGetter.getConfirmedInstitutions(userId, function( return InstitutionsGetter.getConfirmedAffiliations(userId, function(
error, error,
institutions affiliations
) { ) {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
const hasLicence = institutions.some( const hasLicence = affiliations.some(
institution => institution.licence && institution.licence !== 'free' affiliation => affiliation.licence && affiliation.licence !== 'free'
) )
return callback(null, hasLicence) return callback(null, hasLicence)

View file

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

View file

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

View file

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

View file

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