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')
logger = require('logger-sharelatex')
db = mongojs.db
ObjectId = mongojs.ObjectId
settings = require "settings-sharelatex"
request = require "request"
module.exports = UserGetter =
getUser: (query, projection, callback = (error, user) ->) ->
......@@ -30,11 +32,9 @@ module.exports = UserGetter =
return callback error if error?
return callback new Error('User not Found') unless user
fullEmails = user.emails.map (emailData) ->
emailData.default = emailData.email == user.email
emailData
callback null, fullEmails
getAffiliations userId, (error, affiliationsData) ->
return callback error if error?
callback null, decorateFullEmails(user.email, user.emails, affiliationsData)
getUserByMainEmail: (email, projection, callback = (error, user) ->) ->
email = email.trim()
......@@ -81,6 +81,35 @@ module.exports = UserGetter =
return callback(message: 'alread_exists') if user?
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',
'getUserEmail',
......
......@@ -42,6 +42,9 @@ module.exports = MockV1Api =
@exportParams = Object.assign({}, req.body)
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) =>
res.sendStatus 201
......
......@@ -20,11 +20,15 @@ describe "UserGetter", ->
@Mongo =
db: users: findOne: @findOne
ObjectId: (id) -> return id
settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@request = sinon.stub()
@UserGetter = SandboxedModule.require modulePath, requires:
"logger-sharelatex": log:->
"../../infrastructure/mongojs": @Mongo
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
'settings-sharelatex': settings
'request': @request
describe "getUser", ->
it "should get user", (done)->
......@@ -42,6 +46,9 @@ describe "UserGetter", ->
done()
describe "getUserFullEmails", -
beforeEach ->
@request.callsArgWith(1, null, { statusCode: 200 }, [])
it "should get user", (done)->
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @fakeUser)
projection = email: 1, emails: 1
......@@ -59,6 +66,33 @@ describe "UserGetter", ->
]
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", ->
it "query user by main email", (done)->
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