Skip to content

Commit e3dea78

Browse files
authored
make sure user exist in both v3 api and v5 api (#19)
1 parent e9fb3d2 commit e3dea78

File tree

5 files changed

+266
-11
lines changed

5 files changed

+266
-11
lines changed

config/default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports = {
1717
TC_API: process.env.TC_API || 'https://api.topcoder-dev.com/v5',
1818
ORG_ID: process.env.ORG_ID || '36ed815b-3da1-49f1-a043-aaed0a4e81ad',
1919

20+
TOPCODER_USERS_API: process.env.TOPCODER_USERS_API || 'https://api.topcoder-dev.com/v3/users',
21+
2022
DATABASE_URL: process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost:5432/postgres',
2123
DB_SCHEMA_NAME: process.env.DB_SCHEMA_NAME || 'bookings',
2224
PROJECT_API_URL: process.env.PROJECT_API_URL || 'https://api.topcoder-dev.com',

package-lock.json

Lines changed: 166 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"rewire": "^5.0.0",
3838
"sequelize": "^6.3.5",
3939
"superagent": "^6.1.0",
40-
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6.5",
40+
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6",
4141
"util": "^0.12.3",
4242
"uuid": "^8.3.1",
4343
"winston": "^3.3.3"

src/common/helper.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const m2mAuth = require('tc-core-library-js').auth.m2m
2323
// const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))
2424
const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'AUTH0_PROXY_SERVER_URL']))
2525

26+
const topcoderM2M = m2mAuth({
27+
AUTH0_AUDIENCE: config.AUTH0_AUDIENCE_FOR_BUS_API,
28+
..._.pick(config, ['AUTH0_URL', 'TOKEN_CACHE_TIME', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'AUTH0_PROXY_SERVER_URL'])
29+
})
30+
2631
let busApiClient
2732

2833
/**
@@ -202,6 +207,14 @@ const getM2Mtoken = async () => {
202207
return await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
203208
}
204209

210+
/*
211+
* Function to get M2M token to access topcoder resources(e.g. /v3/users)
212+
* @returns {Promise}
213+
*/
214+
const getTopcoderM2MToken = async () => {
215+
return await topcoderM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
216+
}
217+
205218
/**
206219
* Function to encode query string
207220
* @param {Object} queryObj the query object
@@ -307,6 +320,27 @@ async function getProjects (token) {
307320
})
308321
}
309322

323+
/**
324+
* Get topcoder user by id from /v3/users.
325+
*
326+
* @param {String} userId the legacy user id
327+
* @returns {Object} the user
328+
*/
329+
async function getTopcoderUserById (userId) {
330+
const token = await getTopcoderM2MToken()
331+
const res = await request
332+
.get(config.TOPCODER_USERS_API)
333+
.query({ filter: `id=${userId}` })
334+
.set('Authorization', `Bearer ${token}`)
335+
.set('Accept', 'application/json')
336+
localLogger.debug({ context: 'getTopcoderUserById', message: `response body: ${JSON.stringify(res.body)}` })
337+
const user = _.get(res.body, 'result.content[0]')
338+
if (!user) {
339+
throw new errors.NotFoundError(`userId: ${userId} "user" not found from ${config.TOPCODER_USERS_API}`)
340+
}
341+
return user
342+
}
343+
310344
/**
311345
* Function to get users
312346
* @param {String} token the user request token
@@ -323,6 +357,39 @@ async function getUserById (token, userId) {
323357
return _.pick(res.body, ['id', 'handle', 'firstName', 'lastName'])
324358
}
325359

360+
/**
361+
* Function to create user in ubhan
362+
* @param {Object} data the user data
363+
* @returns the request result
364+
*/
365+
async function createUbhanUser ({ handle, firstName, lastName }) {
366+
const token = await getM2Mtoken()
367+
const res = await request
368+
.post(`${config.TC_API}/users`)
369+
.set('Authorization', `Bearer ${token}`)
370+
.set('Content-Type', 'application/json')
371+
.set('Accept', 'application/json')
372+
.send({ handle, firstName, lastName })
373+
localLogger.debug({ context: 'createUbhanUser', message: `response body: ${JSON.stringify(res.body)}` })
374+
return _.pick(res.body, ['id'])
375+
}
376+
377+
/**
378+
* Function to create external profile for a ubhan user
379+
* @param {String} userId the user id(with uuid format)
380+
* @param {Object} data the profile data
381+
*/
382+
async function createUserExternalProfile (userId, { organizationId, externalId }) {
383+
const token = await getM2Mtoken()
384+
const res = await request
385+
.post(`${config.TC_API}/users/${userId}/externalProfiles`)
386+
.set('Authorization', `Bearer ${token}`)
387+
.set('Content-Type', 'application/json')
388+
.set('Accept', 'application/json')
389+
.send({ organizationId, externalId: String(externalId) })
390+
localLogger.debug({ context: 'createUserExternalProfile', message: `response body: ${JSON.stringify(res.body)}` })
391+
}
392+
326393
/**
327394
* Function to get members
328395
* @param {String} token the user request token
@@ -411,7 +478,10 @@ module.exports = {
411478
getBusApiClient,
412479
isDocumentMissingException,
413480
getProjects,
481+
getTopcoderUserById,
414482
getUserById,
483+
createUbhanUser,
484+
createUserExternalProfile,
415485
getMembers,
416486
getProjectById,
417487
getSkillById,

0 commit comments

Comments
 (0)