Commit 983f4e53 authored by Jennifer Spencer's avatar Jennifer Spencer
Browse files

refactor: add first and last name to orcid fetch request, move files to auth folder & write test

parent 68b43f4f
const superagent = require('superagent')
const _ = require('lodash')
const apiRoot = 'https://api.sandbox.orcid.org/v2.1'
......@@ -20,23 +21,26 @@ const toDate = date => {
}
module.exports = async user => {
const [emailResponse, empolymentsResponse] = await Promise.all([
request(user, 'email'),
const [personResponse, employmentsResponse] = await Promise.all([
request(user, 'person'),
request(user, 'employments'),
])
const emails = emailResponse.body.email
const email = emails.length ? emails[0].email : null
const firstName = _.get(personResponse, 'body.name.given-names.value')
const lastName = _.get(personResponse, 'body.name.family-name.value')
const email = _.get(personResponse, 'body.emails.email[0].email')
const employments = empolymentsResponse.body['employment-summary']
const employments = _.get(employmentsResponse, 'body.employment-summary')
const institution = employments.length
? // sort by most recently ended
employments
.sort((a, b) => toDate(a['end-date']) - toDate(b['end-date']))
.pop().organization
.pop().organization.name
: null
return {
firstName,
lastName,
email,
institution,
}
......
require('../../test/helpers/replay-setup')
const fetchDetails = require('./fetchDetails')
const fetchUserDetails = require('./fetchUserDetails')
describe('Fetch ORCID author details', () => {
it('gets email', async () => {
it('gets user details', async () => {
const user = {
orcid: '0000-0003-3146-0256',
oauth: { accessToken: 'f7617529-f46a-40b1-99f4-4181859783ca' },
}
const response = await fetchDetails(user)
console.log(`response`, response)
const response = await fetchUserDetails(user)
expect(response).toEqual({
firstName: 'Test',
lastName: 'User',
email: 'elife@mailinator.com',
institution: 'University of eLife',
})
})
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment