@@ -11,7 +11,7 @@ import { decodeToken } from 'tc-accounts';
11
11
import logger from '../utils/logger' ;
12
12
import { setErrorIcon , ERROR_ICON_TYPES } from '../utils/errors' ;
13
13
import { COMPETITION_TRACKS , getApiResponsePayload } from '../utils/tc' ;
14
- import { getApi } from './api' ;
14
+ import { getApi , proxyApi } from './api' ;
15
15
import { getService as getMembersService } from './members' ;
16
16
17
17
export const ORDER_BY = {
@@ -200,6 +200,7 @@ class ChallengesService {
200
200
apiV3 : getApi ( 'V3' , tokenV3 ) ,
201
201
getChallenges,
202
202
getMemberChallenges,
203
+ proxyApi,
203
204
tokenV2,
204
205
tokenV3,
205
206
memberService : getMembersService ( ) ,
@@ -325,27 +326,32 @@ class ChallengesService {
325
326
* @return {Promise } Resolves to the challenge object.
326
327
*/
327
328
async getChallengeDetails ( challengeId ) {
329
+ let challenge = { } ;
328
330
let isLegacyChallenge = false ;
329
- const filters = { } ;
330
331
// condition based on ROUTE used for Review Opportunities, change if needed
331
- if ( challengeId . length >= 5 && challengeId . length <= 8 ) {
332
+ if ( / ^ [ \d ] { 5 , 8 } $ / . test ( challengeId ) ) {
332
333
isLegacyChallenge = true ;
333
- filters . legacyId = challengeId ;
334
+ challenge = await this . private . getChallenges ( '/challenges/' , { legacyId : challengeId } )
335
+ . then ( res => res . challenges [ 0 ] ) ;
334
336
} else {
335
- filters . id = challengeId ;
337
+ challenge = await this . private . getChallenges ( `/challenges/${ challengeId } ` )
338
+ . then ( res => res . challenges ) ;
336
339
}
337
- const challengeFiltered = await this . private . getChallenges ( '/challenges/' , filters )
338
- . then ( res => res . challenges [ 0 ] ) ;
339
-
340
- if ( challengeFiltered ) {
341
- challengeFiltered . isLegacyChallenge = isLegacyChallenge ;
342
- challengeFiltered . events = _ . map ( challengeFiltered . events , e => ( {
343
- eventName : e . key ,
344
- eventId : e . id ,
345
- description : e . name ,
346
- } ) ) ;
347
- }
348
- return challengeFiltered ;
340
+
341
+ const registrants = await this . getChallengeRegistrants ( challenge . id ) ;
342
+ challenge . registrants = registrants ;
343
+
344
+ challenge . isLegacyChallenge = isLegacyChallenge ;
345
+
346
+ challenge . events = _ . map ( challenge . events , e => ( {
347
+ eventName : e . key ,
348
+ eventId : e . id ,
349
+ description : e . name ,
350
+ } ) ) ;
351
+
352
+ challenge . fetchedWithAuth = Boolean ( this . private . apiV5 . private . token ) ;
353
+
354
+ return challenge ;
349
355
}
350
356
351
357
/**
@@ -354,8 +360,7 @@ class ChallengesService {
354
360
* @return {Promise } Resolves to the challenge registrants array.
355
361
*/
356
362
async getChallengeRegistrants ( challengeId ) {
357
- const registrants = await this . private . apiV5 . get ( `/resources/challengeId=${ challengeId } ` )
358
- . then ( checkError ) . then ( res => res ) ;
363
+ const registrants = await this . private . proxyApi ( `/challenges/${ challengeId } /registrants` ) ;
359
364
return registrants || [ ] ;
360
365
}
361
366
@@ -511,19 +516,17 @@ class ChallengesService {
511
516
* @param {String } roleName
512
517
* @return {Promise }
513
518
*/
514
- async getResourceRoleId ( roleName ) {
519
+ async getRoleId ( roleName ) {
515
520
const params = {
516
521
name : roleName ,
517
- isActive : true ,
518
522
} ;
519
- const roles = await this . private . apiV5 . get ( `/resource-roles?${ qs . stringify ( params ) } ` )
520
- . then ( checkErrorV5 ) . then ( res => res ) ;
523
+ const roles = await this . private . proxyApi ( `/challenges/roleId?${ qs . stringify ( params ) } ` ) ;
521
524
522
- if ( _ . isEmpty ( roles . result ) ) {
525
+ if ( _ . isEmpty ( roles ) ) {
523
526
throw new Error ( 'Resource Role not found!' ) ;
524
527
}
525
528
526
- return roles . result [ 0 ] . id ;
529
+ return roles [ 0 ] . id ;
527
530
}
528
531
529
532
/**
@@ -533,7 +536,7 @@ class ChallengesService {
533
536
*/
534
537
async register ( challengeId ) {
535
538
const user = decodeToken ( this . private . tokenV3 ) ;
536
- const roleId = await this . getResourceRoleId ( 'Submitter' ) ;
539
+ const roleId = await this . getRoleId ( 'Submitter' ) ;
537
540
const params = {
538
541
challengeId,
539
542
memberHandle : user . handle ,
@@ -551,13 +554,13 @@ class ChallengesService {
551
554
*/
552
555
async unregister ( challengeId ) {
553
556
const user = decodeToken ( this . private . tokenV3 ) ;
554
- const roleId = await this . getResourceRoleId ( 'Submitter' ) ;
557
+ const roleId = await this . getRoleId ( 'Submitter' ) ;
555
558
const params = {
556
559
challengeId,
557
560
memberHandle : user . handle ,
558
561
roleId,
559
562
} ;
560
- const res = await this . private . apiV5 . delete ( '/resources' , params ) ;
563
+ const res = await this . private . apiV5 . delete ( '/resources' , JSON . stringify ( params ) ) ;
561
564
if ( ! res . ok ) throw new Error ( res . statusText ) ;
562
565
return res . json ( ) ;
563
566
}
0 commit comments