Skip to content

make sure user exist in both v3 api and v5 api #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = {
TC_API: process.env.TC_API || 'https://api.topcoder-dev.com/v5',
ORG_ID: process.env.ORG_ID || '36ed815b-3da1-49f1-a043-aaed0a4e81ad',

TOPCODER_USERS_API: process.env.TOPCODER_USERS_API || 'https://api.topcoder-dev.com/v3/users',

DATABASE_URL: process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost:5432/postgres',
DB_SCHEMA_NAME: process.env.DB_SCHEMA_NAME || 'bookings',
PROJECT_API_URL: process.env.PROJECT_API_URL || 'https://api.topcoder-dev.com',
Expand Down
173 changes: 166 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"rewire": "^5.0.0",
"sequelize": "^6.3.5",
"superagent": "^6.1.0",
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6.5",
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6",
"util": "^0.12.3",
"uuid": "^8.3.1",
"winston": "^3.3.3"
Expand Down
70 changes: 70 additions & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const m2mAuth = require('tc-core-library-js').auth.m2m
// const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))
const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'AUTH0_PROXY_SERVER_URL']))

const topcoderM2M = m2mAuth({
AUTH0_AUDIENCE: config.AUTH0_AUDIENCE_FOR_BUS_API,
..._.pick(config, ['AUTH0_URL', 'TOKEN_CACHE_TIME', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'AUTH0_PROXY_SERVER_URL'])
})

let busApiClient

/**
Expand Down Expand Up @@ -202,6 +207,14 @@ const getM2Mtoken = async () => {
return await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}

/*
* Function to get M2M token to access topcoder resources(e.g. /v3/users)
* @returns {Promise}
*/
const getTopcoderM2MToken = async () => {
return await topcoderM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}

/**
* Function to encode query string
* @param {Object} queryObj the query object
Expand Down Expand Up @@ -307,6 +320,27 @@ async function getProjects (token) {
})
}

/**
* Get topcoder user by id from /v3/users.
*
* @param {String} userId the legacy user id
* @returns {Object} the user
*/
async function getTopcoderUserById (userId) {
const token = await getTopcoderM2MToken()
const res = await request
.get(config.TOPCODER_USERS_API)
.query({ filter: `id=${userId}` })
.set('Authorization', `Bearer ${token}`)
.set('Accept', 'application/json')
localLogger.debug({ context: 'getTopcoderUserById', message: `response body: ${JSON.stringify(res.body)}` })
const user = _.get(res.body, 'result.content[0]')
if (!user) {
throw new errors.NotFoundError(`userId: ${userId} "user" not found from ${config.TOPCODER_USERS_API}`)
}
return user
}

/**
* Function to get users
* @param {String} token the user request token
Expand All @@ -323,6 +357,39 @@ async function getUserById (token, userId) {
return _.pick(res.body, ['id', 'handle', 'firstName', 'lastName'])
}

/**
* Function to create user in ubhan
* @param {Object} data the user data
* @returns the request result
*/
async function createUbhanUser ({ handle, firstName, lastName }) {
const token = await getM2Mtoken()
const res = await request
.post(`${config.TC_API}/users`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({ handle, firstName, lastName })
localLogger.debug({ context: 'createUbhanUser', message: `response body: ${JSON.stringify(res.body)}` })
return _.pick(res.body, ['id'])
}

/**
* Function to create external profile for a ubhan user
* @param {String} userId the user id(with uuid format)
* @param {Object} data the profile data
*/
async function createUserExternalProfile (userId, { organizationId, externalId }) {
const token = await getM2Mtoken()
const res = await request
.post(`${config.TC_API}/users/${userId}/externalProfiles`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({ organizationId, externalId: String(externalId) })
localLogger.debug({ context: 'createUserExternalProfile', message: `response body: ${JSON.stringify(res.body)}` })
}

/**
* Function to get members
* @param {String} token the user request token
Expand Down Expand Up @@ -410,7 +477,10 @@ module.exports = {
getBusApiClient,
isDocumentMissingException,
getProjects,
getTopcoderUserById,
getUserById,
createUbhanUser,
createUserExternalProfile,
getMembers,
getProjectById,
getSkillById,
Expand Down
Loading