@@ -23,6 +23,11 @@ const m2mAuth = require('tc-core-library-js').auth.m2m
23
23
// const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))
24
24
const m2m = m2mAuth ( _ . pick ( config , [ 'AUTH0_URL' , 'AUTH0_AUDIENCE' , 'AUTH0_CLIENT_ID' , 'AUTH0_CLIENT_SECRET' , 'AUTH0_PROXY_SERVER_URL' ] ) )
25
25
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
+
26
31
let busApiClient
27
32
28
33
/**
@@ -202,6 +207,14 @@ const getM2Mtoken = async () => {
202
207
return await m2m . getMachineToken ( config . AUTH0_CLIENT_ID , config . AUTH0_CLIENT_SECRET )
203
208
}
204
209
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
+
205
218
/**
206
219
* Function to encode query string
207
220
* @param {Object } queryObj the query object
@@ -307,6 +320,27 @@ async function getProjects (token) {
307
320
} )
308
321
}
309
322
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
+
310
344
/**
311
345
* Function to get users
312
346
* @param {String } token the user request token
@@ -323,6 +357,39 @@ async function getUserById (token, userId) {
323
357
return _ . pick ( res . body , [ 'id' , 'handle' , 'firstName' , 'lastName' ] )
324
358
}
325
359
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
+
326
393
/**
327
394
* Function to get members
328
395
* @param {String } token the user request token
@@ -411,7 +478,10 @@ module.exports = {
411
478
getBusApiClient,
412
479
isDocumentMissingException,
413
480
getProjects,
481
+ getTopcoderUserById,
414
482
getUserById,
483
+ createUbhanUser,
484
+ createUserExternalProfile,
415
485
getMembers,
416
486
getProjectById,
417
487
getSkillById,
0 commit comments