@@ -330,11 +330,18 @@ function isDocumentMissingException (err) {
330
330
331
331
/**
332
332
* Function to get projects
333
- * @param {String } token the user request token
333
+ * @param {Object } currentUser the user who perform this operation
334
334
* @param {Object } criteria the search criteria
335
335
* @returns the request result
336
336
*/
337
- async function getProjects ( token , criteria = { } ) {
337
+ async function getProjects ( currentUser , criteria = { } ) {
338
+ let token
339
+ if ( currentUser . isBookingManager || currentUser . isMachine ) {
340
+ const m2mToken = await getM2Mtoken ( )
341
+ token = `Bearer ${ m2mToken } `
342
+ } else {
343
+ token = currentUser . jwtToken
344
+ }
338
345
const url = `${ config . TC_API } /projects?type=talent-as-a-service`
339
346
const res = await request
340
347
. get ( url )
@@ -377,14 +384,14 @@ async function getTopcoderUserById (userId) {
377
384
378
385
/**
379
386
* Function to get users
380
- * @param {String } token the user request token
381
387
* @param {String } userId the user id
382
388
* @returns the request result
383
389
*/
384
- async function getUserById ( token , userId , enrich ) {
390
+ async function getUserById ( userId , enrich ) {
391
+ const token = await getM2Mtoken ( )
385
392
const res = await request
386
393
. get ( `${ config . TC_API } /users/${ userId } ` + ( enrich ? '?enrich=true' : '' ) )
387
- . set ( 'Authorization' , token )
394
+ . set ( 'Authorization' , `Bearer ${ token } ` )
388
395
. set ( 'Content-Type' , 'application/json' )
389
396
. set ( 'Accept' , 'application/json' )
390
397
localLogger . debug ( { context : 'getUserById' , message : `response body: ${ JSON . stringify ( res . body ) } ` } )
@@ -433,19 +440,19 @@ async function createUserExternalProfile (userId, { organizationId, externalId }
433
440
434
441
/**
435
442
* Function to get members
436
- * @param {String } token the user request token
437
443
* @param {Array } handles the handle array
438
444
* @returns the request result
439
445
*/
440
- async function getMembers ( token , handles ) {
446
+ async function getMembers ( handles ) {
447
+ const token = await getM2Mtoken ( )
441
448
const handlesStr = _ . map ( handles , handle => {
442
449
return '%22' + handle . toLowerCase ( ) + '%22'
443
450
} ) . join ( ',' )
444
451
const url = `${ config . TC_API } /members?fields=userId,handleLower,photoURL&handlesLower=[${ handlesStr } ]`
445
452
446
453
const res = await request
447
454
. get ( url )
448
- . set ( 'Authorization' , token )
455
+ . set ( 'Authorization' , `Bearer ${ token } ` )
449
456
. set ( 'Content-Type' , 'application/json' )
450
457
. set ( 'Accept' , 'application/json' )
451
458
localLogger . debug ( { context : 'getMembers' , message : `response body: ${ JSON . stringify ( res . body ) } ` } )
@@ -454,31 +461,49 @@ async function getMembers (token, handles) {
454
461
455
462
/**
456
463
* Function to get project by id
457
- * @param {String } token the user request token
464
+ * @param {Object } currentUser the user who perform this operation
458
465
* @param {Number } id project id
459
466
* @returns the request result
460
467
*/
461
- async function getProjectById ( token , id ) {
468
+ async function getProjectById ( currentUser , id ) {
469
+ let token
470
+ if ( currentUser . isBookingManager || currentUser . isMachine ) {
471
+ const m2mToken = await getM2Mtoken ( )
472
+ token = `Bearer ${ m2mToken } `
473
+ } else {
474
+ token = currentUser . jwtToken
475
+ }
462
476
const url = `${ config . TC_API } /projects/${ id } `
463
- const res = await request
464
- . get ( url )
465
- . set ( 'Authorization' , token )
466
- . set ( 'Content-Type' , 'application/json' )
467
- . set ( 'Accept' , 'application/json' )
468
- localLogger . debug ( { context : 'getProjectById' , message : `response body: ${ JSON . stringify ( res . body ) } ` } )
469
- return _ . pick ( res . body , [ 'id' , 'name' ] )
477
+ try {
478
+ const res = await request
479
+ . get ( url )
480
+ . set ( 'Authorization' , token )
481
+ . set ( 'Content-Type' , 'application/json' )
482
+ . set ( 'Accept' , 'application/json' )
483
+ localLogger . debug ( { context : 'getProjectById' , message : `response body: ${ JSON . stringify ( res . body ) } ` } )
484
+ return _ . pick ( res . body , [ 'id' , 'name' ] )
485
+ } catch ( err ) {
486
+ console . log ( err )
487
+ if ( err . status === HttpStatus . FORBIDDEN ) {
488
+ throw new errors . UnauthorizedError ( `You are not allowed to access the project with id ${ id } ` )
489
+ }
490
+ if ( err . status === HttpStatus . NOT_FOUND ) {
491
+ throw new errors . NotFoundError ( `id: ${ id } project not found` )
492
+ }
493
+ throw err
494
+ }
470
495
}
471
496
472
497
/**
473
498
* Function to get skill by id
474
- * @param {String } token the user request token
475
499
* @param {String } skillId the skill Id
476
500
* @returns the request result
477
501
*/
478
- async function getSkillById ( token , skillId ) {
502
+ async function getSkillById ( skillId ) {
503
+ const token = await getM2Mtoken ( )
479
504
const res = await request
480
505
. get ( `${ config . TC_API } /skills/${ skillId } ` )
481
- . set ( 'Authorization' , token )
506
+ . set ( 'Authorization' , `Bearer ${ token } ` )
482
507
. set ( 'Content-Type' , 'application/json' )
483
508
. set ( 'Accept' , 'application/json' )
484
509
localLogger . debug ( { context : 'getSkillById' , message : `response body: ${ JSON . stringify ( res . body ) } ` } )
0 commit comments