Skip to content
Snippets Groups Projects
Verified Commit e2b87dfc authored by Tim Alby's avatar Tim Alby Committed by David Mehren
Browse files

decorate emails list with affiliation data

parent 6c9ec2f3
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,8 @@ metrics = require('metrics-sharelatex') ...@@ -3,6 +3,8 @@ metrics = require('metrics-sharelatex')
logger = require('logger-sharelatex') logger = require('logger-sharelatex')
db = mongojs.db db = mongojs.db
ObjectId = mongojs.ObjectId ObjectId = mongojs.ObjectId
settings = require "settings-sharelatex"
request = require "request"
module.exports = UserGetter = module.exports = UserGetter =
getUser: (query, projection, callback = (error, user) ->) -> getUser: (query, projection, callback = (error, user) ->) ->
...@@ -30,11 +32,9 @@ module.exports = UserGetter = ...@@ -30,11 +32,9 @@ module.exports = UserGetter =
return callback error if error? return callback error if error?
return callback new Error('User not Found') unless user return callback new Error('User not Found') unless user
fullEmails = user.emails.map (emailData) -> getAffiliations userId, (error, affiliationsData) ->
emailData.default = emailData.email == user.email return callback error if error?
emailData callback null, decorateFullEmails(user.email, user.emails, affiliationsData)
callback null, fullEmails
getUserByMainEmail: (email, projection, callback = (error, user) ->) -> getUserByMainEmail: (email, projection, callback = (error, user) ->) ->
email = email.trim() email = email.trim()
...@@ -81,6 +81,35 @@ module.exports = UserGetter = ...@@ -81,6 +81,35 @@ module.exports = UserGetter =
return callback(message: 'alread_exists') if user? return callback(message: 'alread_exists') if user?
callback(error) callback(error)
decorateFullEmails = (defaultEmail, emailsData, affiliationsData) ->
emailsData.map (emailData) ->
emailData.default = emailData.email == defaultEmail
affiliation = affiliationsData.find (aff) -> aff.email == emailData.email
if affiliation?
{ institution, inferred, role, department } = affiliation
emailData.affiliation = { institution, inferred, role, department }
else
emailsData.affiliation = null
emailData
getAffiliations = (userId, callback = (error) ->) ->
return callback(null, []) unless settings?.apis?.v1?.url # service is not configured
request {
method: 'GET'
url: "#{settings.apis.v1.url}/api/v2/users/#{userId.toString()}/affiliations"
auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass }
json: true,
timeout: 20 * 1000
}, (error, response, body) ->
return callback(error) if error?
unless 200 <= response.statusCode < 300
errorMessage = "Couldn't get affiliations: #{response.statusCode}"
return callback(new Error(errorMessage))
callback(null, body)
[ [
'getUser', 'getUser',
'getUserEmail', 'getUserEmail',
......
...@@ -42,6 +42,9 @@ module.exports = MockV1Api = ...@@ -42,6 +42,9 @@ module.exports = MockV1Api =
@exportParams = Object.assign({}, req.body) @exportParams = Object.assign({}, req.body)
res.json exportId: @exportId res.json exportId: @exportId
app.get "/api/v2/users/:userId/affiliations", (req, res, next) =>
res.json []
app.post "/api/v2/users/:userId/affiliations", (req, res, next) => app.post "/api/v2/users/:userId/affiliations", (req, res, next) =>
res.sendStatus 201 res.sendStatus 201
......
...@@ -20,11 +20,15 @@ describe "UserGetter", -> ...@@ -20,11 +20,15 @@ describe "UserGetter", ->
@Mongo = @Mongo =
db: users: findOne: @findOne db: users: findOne: @findOne
ObjectId: (id) -> return id ObjectId: (id) -> return id
settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@request = sinon.stub()
@UserGetter = SandboxedModule.require modulePath, requires: @UserGetter = SandboxedModule.require modulePath, requires:
"logger-sharelatex": log:-> "logger-sharelatex": log:->
"../../infrastructure/mongojs": @Mongo "../../infrastructure/mongojs": @Mongo
"metrics-sharelatex": timeAsyncMethod: sinon.stub() "metrics-sharelatex": timeAsyncMethod: sinon.stub()
'settings-sharelatex': settings
'request': @request
describe "getUser", -> describe "getUser", ->
it "should get user", (done)-> it "should get user", (done)->
...@@ -42,6 +46,9 @@ describe "UserGetter", -> ...@@ -42,6 +46,9 @@ describe "UserGetter", ->
done() done()
describe "getUserFullEmails", - describe "getUserFullEmails", -
beforeEach ->
@request.callsArgWith(1, null, { statusCode: 200 }, [])
it "should get user", (done)-> it "should get user", (done)->
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @fakeUser) @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @fakeUser)
projection = email: 1, emails: 1 projection = email: 1, emails: 1
...@@ -59,6 +66,33 @@ describe "UserGetter", -> ...@@ -59,6 +66,33 @@ describe "UserGetter", ->
] ]
done() done()
it "should merge affiliation data", (done)->
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @fakeUser)
affiliationsData = [
{
email: 'email1@foo.bar'
role: 'Prof'
department: 'Maths'
inferred: false
institution: { name: 'University Name', isUniversity: true }
}
]
@request.callsArgWith(1, null, { statusCode: 200 }, affiliationsData)
@UserGetter.getUserFullEmails @fakeUser._id, (error, fullEmails) =>
assert.deepEqual fullEmails, [
{
email: 'email1@foo.bar'
default: false
affiliation:
institution: affiliationsData[0].institution
inferred: affiliationsData[0].inferred
department: affiliationsData[0].department
role: affiliationsData[0].role
}
{ email: 'email2@foo.bar', default: true }
]
done()
describe "getUserbyMainEmail", -> describe "getUserbyMainEmail", ->
it "query user by main email", (done)-> it "query user by main email", (done)->
email = 'hello@world.com' email = 'hello@world.com'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment