Skip to content
Snippets Groups Projects
Commit e7e0c1e1 authored by Jure's avatar Jure
Browse files

feat: fetch user details on orcid user signup

parent 1afdc7f4
No related branches found
No related tags found
No related merge requests found
const superagent = require('superagent')
const _ = require('lodash')
const logger = require('@pubsweet/logger')
const { Identity } = require('@pubsweet/models')
const apiRoot = 'https://api.sandbox.orcid.org/v2.1'
const apiRoot =
process.env.NODE_ENV === 'production'
? 'https://pub.orcid.org/v3.0'
: 'https://pub.sandbox.orcid.org/v3.0'
// request data from orcid API
const request = (user, endpoint) =>
const request = (identity, endpoint) =>
superagent
.get(`${apiRoot}/${user.orcid}/${endpoint}`)
.get(`${apiRoot}/${identity.identifier}/${endpoint}`)
.set('Accept', 'application/json')
.set('Authorization', `Bearer ${user.oauth.accessToken}`)
.set('Authorization', `Bearer ${identity.oauth.accessToken}`)
// convert API date object into Date
const toDate = date => {
......@@ -22,10 +26,15 @@ const toDate = date => {
}
module.exports = async user => {
const identity = await Identity.query().findOne({
userId: user.id,
type: 'orcid',
})
logger.debug('processing response from orcid api')
const [personResponse, employmentsResponse] = await Promise.all([
request(user, 'person'),
request(user, 'employments'),
request(identity, 'person'),
request(identity, 'employments'),
])
const firstName = _.get(personResponse, 'body.name.given-names.value')
......@@ -33,16 +42,18 @@ module.exports = async user => {
const email = _.get(personResponse, 'body.emails.email[0].email')
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.name
: null
const institution =
employments && employments.length
? // sort by most recently ended
employments
.sort((a, b) => toDate(a['end-date']) - toDate(b['end-date']))
.pop().organization.name
: null
logger.debug(`fetchUserDetails returning:
first: ${firstName},
last: ${lastName}
last: ${lastName},
email: ${email},
institution: ${institution}`)
return {
......
......@@ -2,7 +2,7 @@ const passport = require('passport')
const OrcidStrategy = require('passport-orcid')
const config = require('config')
const authentication = require('pubsweet-server/src/authentication')
const fetchUserDetails = require('./fetchUserDetails')
const CALLBACK_URL = '/auth/orcid/callback'
module.exports = app => {
......@@ -46,6 +46,13 @@ module.exports = app => {
isDefault: true,
},
}).saveGraph()
// Do another request to the ORCID API for aff/name
const userDetails = await fetchUserDetails(user)
user.defaultIdentity.name = `${userDetails.firstName} ${userDetails.lastName}`
user.defaultIdentity.aff = userDetails.institution
user.saveGraph()
}
} catch (err) {
done(err)
......
......@@ -203,14 +203,8 @@ const typeDefs = `
online: Boolean
}
type Name {
surname: String
givenNames: String
title: String
}
interface Identity {
name: Name
name: String
aff: String # JATS <aff>
email: String # JATS <aff>
type: String
......@@ -220,14 +214,14 @@ const typeDefs = `
# local identity (not from ORCID, etc.)
type LocalIdentity implements Identity {
name: Name
name: String
email: String
aff: String
type: String
}
type ExternalIdentity implements Identity {
name: Name
name: String
identifier: String
email: String
aff: String
......@@ -263,6 +257,16 @@ const typeDefs = `
username: String!
password: String!
}
# Common types
scalar DateTime
interface Object {
id: ID!
created: DateTime!
updated: DateTime
}
`
module.exports = { resolvers, typeDefs }
alter table identities
drop constraint identities_user_id_fkey,
add constraint sidentities_user_id_fkey
foreign key (user_id)
references users(id)
on delete cascade;
\ No newline at end of file
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