@@ -8,10 +8,11 @@ import _ from 'lodash';
8
8
import moment from 'moment' ;
9
9
import qs from 'qs' ;
10
10
import { decodeToken } from 'tc-accounts' ;
11
+ import { config } from 'topcoder-react-utils' ;
11
12
import logger from '../utils/logger' ;
12
13
import { setErrorIcon , ERROR_ICON_TYPES } from '../utils/errors' ;
13
14
import { COMPETITION_TRACKS , getApiResponsePayload } from '../utils/tc' ;
14
- import { getApi } from './api' ;
15
+ import { getTcM2mToken , getApi , proxyApi } from './api' ;
15
16
import { getService as getMembersService } from './members' ;
16
17
17
18
export const ORDER_BY = {
@@ -147,7 +148,7 @@ class ChallengesService {
147
148
const url = `${ endpoint } ?${ qs . stringify ( query ) } ` ;
148
149
const res = await this . private . apiV5 . get ( url ) . then ( checkErrorV5 ) ;
149
150
let myChallenges ;
150
- if ( typeof this . private . tokenV3 !== 'undefined' ) {
151
+ if ( this . private . tokenV3 ) {
151
152
const { userId } = decodeToken ( this . private . tokenV3 ) ;
152
153
myChallenges = await this . private . apiV5 . get ( `/resources/${ userId } /challenges` )
153
154
. then ( checkErrorV5 ) . then ( userChallenges => userChallenges ) ;
@@ -198,6 +199,8 @@ class ChallengesService {
198
199
apiV5 : getApi ( 'V5' , tokenV3 ) ,
199
200
apiV2 : getApi ( 'V2' , tokenV2 ) ,
200
201
apiV3 : getApi ( 'V3' , tokenV3 ) ,
202
+ getTcM2mToken,
203
+ proxyApi,
201
204
getChallenges,
202
205
getMemberChallenges,
203
206
tokenV2,
@@ -318,34 +321,52 @@ class ChallengesService {
318
321
319
322
/**
320
323
* Gets challenge details from Topcoder API.
321
- * NOTE: This function also uses API v2 and other endpoints for now, due
324
+ * NOTE: This function also uses other endpoints for now, due
322
325
* to some information is missing or
323
326
* incorrect in the main endpoint. This may change in the future.
324
327
* @param {Number|String } challengeId
325
328
* @return {Promise } Resolves to the challenge object.
326
329
*/
327
330
async getChallengeDetails ( challengeId ) {
331
+ let challenge = { } ;
328
332
let isLegacyChallenge = false ;
329
- const filters = { } ;
330
333
// condition based on ROUTE used for Review Opportunities, change if needed
331
- if ( challengeId . length >= 5 && challengeId . length <= 8 ) {
334
+ if ( / ^ [ \d ] { 5 , 8 } $ / . test ( challengeId ) ) {
332
335
isLegacyChallenge = true ;
333
- filters . legacyId = challengeId ;
336
+ challenge = await this . private . getChallenges ( '/challenges/' , { legacyId : challengeId } )
337
+ . then ( res => res . challenges [ 0 ] ) ;
334
338
} else {
335
- filters . id = challengeId ;
339
+ challenge = await this . private . getChallenges ( `/challenges/${ challengeId } ` )
340
+ . then ( res => res . challenges ) ;
336
341
}
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 ;
342
+
343
+ /**
344
+ * TODO: Currenlty using legacyId until submissions_api fix issue with UUID
345
+ */
346
+ const submissions = await this . getChallengeSubmissions ( challenge . legacyId ) ;
347
+ challenge . submissions = submissions ;
348
+
349
+ const registrants = await this . getChallengeRegistrants ( challenge . id ) ;
350
+ // Add submission date to registrants
351
+ registrants . forEach ( ( r , i ) => {
352
+ const submission = submissions . find ( s => s . memberId === Number ( r . memberId ) ) ;
353
+ if ( submission ) {
354
+ registrants [ i ] . submissionDate = submission . created ;
355
+ }
356
+ } ) ;
357
+ challenge . registrants = registrants ;
358
+
359
+ challenge . isLegacyChallenge = isLegacyChallenge ;
360
+
361
+ challenge . events = _ . map ( challenge . events , e => ( {
362
+ eventName : e . key ,
363
+ eventId : e . id ,
364
+ description : e . name ,
365
+ } ) ) ;
366
+
367
+ challenge . fetchedWithAuth = Boolean ( this . private . apiV5 . private . token ) ;
368
+
369
+ return challenge ;
349
370
}
350
371
351
372
/**
@@ -354,11 +375,31 @@ class ChallengesService {
354
375
* @return {Promise } Resolves to the challenge registrants array.
355
376
*/
356
377
async getChallengeRegistrants ( challengeId ) {
357
- const registrants = await this . private . apiV5 . get ( `/resources/challengeId=${ challengeId } ` )
358
- . then ( checkError ) . then ( res => res ) ;
378
+ const roleId = await this . getResourceRoleId ( 'Submitter' ) ;
379
+ const params = {
380
+ challengeId,
381
+ roleId,
382
+ } ;
383
+ const url = `${ config . API . V5 } /resources?${ qs . stringify ( params ) } ` ;
384
+ const registrants = await this . private . proxyApi ( url ) ;
359
385
return registrants || [ ] ;
360
386
}
361
387
388
+ /**
389
+ * Gets challenge submissions from Topcoder API.
390
+ * @param {Number|String } challengeId
391
+ * @return {Promise } Resolves to the challenge registrants array.
392
+ */
393
+ async getChallengeSubmissions ( challengeId ) {
394
+ const params = {
395
+ challengeId,
396
+ perPage : 100 ,
397
+ } ;
398
+ const url = `${ config . API . V5 } /submissions?${ qs . stringify ( params ) } ` ;
399
+ const submissions = await this . private . proxyApi ( url ) ;
400
+ return submissions || [ ] ;
401
+ }
402
+
362
403
/**
363
404
* Gets possible challenge types.
364
405
* @return {Promise } Resolves to the array of subtrack names.
@@ -523,14 +564,15 @@ class ChallengesService {
523
564
name : roleName ,
524
565
isActive : true ,
525
566
} ;
526
- const roles = await this . private . apiV5 . get ( `/resource-roles?${ qs . stringify ( params ) } ` )
527
- . then ( checkErrorV5 ) . then ( res => res ) ;
528
567
529
- if ( _ . isEmpty ( roles . result ) ) {
568
+ const url = `${ config . API . V5 } /resource-roles?${ qs . stringify ( params ) } ` ;
569
+ const roles = await this . private . proxyApi ( url ) ;
570
+
571
+ if ( _ . isEmpty ( roles ) ) {
530
572
throw new Error ( 'Resource Role not found!' ) ;
531
573
}
532
574
533
- return roles . result [ 0 ] . id ;
575
+ return roles [ 0 ] . id ;
534
576
}
535
577
536
578
/**
@@ -564,7 +606,7 @@ class ChallengesService {
564
606
memberHandle : user . handle ,
565
607
roleId,
566
608
} ;
567
- const res = await this . private . apiV5 . delete ( '/resources' , params ) ;
609
+ const res = await this . private . apiV5 . delete ( '/resources' , JSON . stringify ( params ) ) ;
568
610
if ( ! res . ok ) throw new Error ( res . statusText ) ;
569
611
return res . json ( ) ;
570
612
}
0 commit comments