diff --git a/services/web/app/src/Features/Institutions/InstitutionsController.js b/services/web/app/src/Features/Institutions/InstitutionsController.js index a395cf5bef..c83d9993d6 100644 --- a/services/web/app/src/Features/Institutions/InstitutionsController.js +++ b/services/web/app/src/Features/Institutions/InstitutionsController.js @@ -19,7 +19,7 @@ module.exports = { function affiliateUsers(hostname, callback) { const reversedHostname = hostname.trim().split('').reverse().join('') - UserGetter.getUsersByHostname(hostname, { _id: 1 }, function (error, users) { + UserGetter.getInstitutionUsersByHostname(hostname, (error, users) => { if (error) { OError.tag(error, 'problem fetching users by hostname') return callback(error) @@ -29,11 +29,7 @@ function affiliateUsers(hostname, callback) { users, ASYNC_AFFILIATIONS_LIMIT, (user, innerCallback) => { - UserGetter.getUserFullEmails(user._id, (error, emails) => { - if (error) return innerCallback(error) - user.emails = emails - affiliateUserByReversedHostname(user, reversedHostname, innerCallback) - }) + affiliateUserByReversedHostname(user, reversedHostname, innerCallback) }, callback ) diff --git a/services/web/app/src/Features/User/UserGetter.js b/services/web/app/src/Features/User/UserGetter.js index e54e84b9fa..621f265cb0 100644 --- a/services/web/app/src/Features/User/UserGetter.js +++ b/services/web/app/src/Features/User/UserGetter.js @@ -196,6 +196,28 @@ const UserGetter = { db.users.find(query, { projection }).toArray(callback) }, + getInstitutionUsersByHostname(hostname, callback) { + const projection = { + _id: 1, + email: 1, + emails: 1, + samlIdentifiers: 1, + } + UserGetter.getUsersByHostname(hostname, projection, (err, users) => { + if (err) return callback(err) + + users.forEach(user => { + user.emails = decorateFullEmails( + user.email, + user.emails, + [], + user.samlIdentifiers || [] + ) + }) + callback(null, users) + }) + }, + getUsers(query, projection, callback) { try { query = normalizeMultiQuery(query) diff --git a/services/web/test/unit/src/Institutions/InstitutionsControllerTests.js b/services/web/test/unit/src/Institutions/InstitutionsControllerTests.js index a4a822ecdb..13169a8e91 100644 --- a/services/web/test/unit/src/Institutions/InstitutionsControllerTests.js +++ b/services/web/test/unit/src/Institutions/InstitutionsControllerTests.js @@ -59,27 +59,22 @@ describe('InstitutionsController', function () { }, ] - this.getUsersByHostname = sinon.stub().callsArgWith( - 2, - null, - [this.stubbedUser1, this.stubbedUser2].map(user => { - return { _id: user._id } - }) - ) + this.getInstitutionUsersByHostname = sinon.stub().yields(null, [ + { + _id: this.stubbedUser1._id, + emails: this.stubbedUser1DecoratedEmails, + }, + { + _id: this.stubbedUser2._id, + emails: this.stubbedUser2DecoratedEmails, + }, + ]) this.addAffiliation = sinon.stub().callsArgWith(3, null) this.refreshFeatures = sinon.stub().yields(null) - this.getUserFullEmails = sinon.stub() - this.getUserFullEmails - .withArgs(this.stubbedUser1._id) - .yields(null, this.stubbedUser1DecoratedEmails) - this.getUserFullEmails - .withArgs(this.stubbedUser2._id) - .yields(null, this.stubbedUser2DecoratedEmails) this.InstitutionsController = SandboxedModule.require(modulePath, { requires: { '../User/UserGetter': { - getUsersByHostname: this.getUsersByHostname, - getUserFullEmails: this.getUserFullEmails, + getInstitutionUsersByHostname: this.getInstitutionUsersByHostname, }, '../Institutions/InstitutionsAPI': { addAffiliation: this.addAffiliation, @@ -103,7 +98,7 @@ describe('InstitutionsController', function () { it('should add affiliations for matching users', function (done) { this.res.sendStatus = code => { code.should.equal(200) - this.getUsersByHostname.calledOnce.should.equal(true) + this.getInstitutionUsersByHostname.calledOnce.should.equal(true) this.addAffiliation.calledThrice.should.equal(true) this.addAffiliation .calledWithMatch( @@ -147,7 +142,7 @@ describe('InstitutionsController', function () { this.addAffiliation.onCall(2).callsArgWith(3, new Error('error')) this.next = error => { expect(error).to.exist - this.getUsersByHostname.calledOnce.should.equal(true) + this.getInstitutionUsersByHostname.calledOnce.should.equal(true) return done() } return this.InstitutionsController.confirmDomain(