@@ -8,13 +8,12 @@ import _ from 'lodash';
8
8
import moment from 'moment' ;
9
9
import qs from 'qs' ;
10
10
import { decodeToken } from 'tc-accounts' ;
11
- import { isomorphy } from 'topcoder-react-utils' ;
11
+ import { config } from 'topcoder-react-utils' ;
12
12
import logger from '../utils/logger' ;
13
13
import { setErrorIcon , ERROR_ICON_TYPES } from '../utils/errors' ;
14
14
import { COMPETITION_TRACKS , getApiResponsePayload } from '../utils/tc' ;
15
- import { getTcM2mToken , getApi } from './api' ;
15
+ import { getTcM2mToken , getApi , proxyApi } from './api' ;
16
16
import { getService as getMembersService } from './members' ;
17
- import { getService as getSubmissionsService } from './submissions' ;
18
17
19
18
export const ORDER_BY = {
20
19
SUBMISSION_END_DATE : 'submissionEndDate' ,
@@ -185,12 +184,12 @@ class ChallengesService {
185
184
apiV2 : getApi ( 'V2' , tokenV2 ) ,
186
185
apiV3 : getApi ( 'V3' , tokenV3 ) ,
187
186
getTcM2mToken,
187
+ proxyApi,
188
188
getChallenges,
189
189
getMemberChallenges,
190
190
tokenV2,
191
191
tokenV3,
192
192
memberService : getMembersService ( ) ,
193
- submissionsServices : getSubmissionsService ( tokenV3 ) ,
194
193
} ;
195
194
}
196
195
@@ -306,7 +305,7 @@ class ChallengesService {
306
305
307
306
/**
308
307
* Gets challenge details from Topcoder API.
309
- * NOTE: This function also uses API v2 and other endpoints for now, due
308
+ * NOTE: This function also uses other endpoints for now, due
310
309
* to some information is missing or
311
310
* incorrect in the main endpoint. This may change in the future.
312
311
* @param {Number|String } challengeId
@@ -316,15 +315,21 @@ class ChallengesService {
316
315
const challenge = await this . private . getChallenges ( `/challenges/${ challengeId } ` )
317
316
. then ( res => res . challenges ) ;
318
317
319
- if ( isomorphy . isServerSide ( ) ) {
320
- const registrants = await this . getChallengeRegistrants ( challengeId ) ;
321
- challenge . registrants = registrants . result ;
322
- }
318
+ /**
319
+ * TODO: Currenlty using legacyId until submissions_api fix issue with UUID
320
+ */
321
+ const submissions = await this . getChallengeSubmissions ( challenge . legacyId ) ;
322
+ challenge . submissions = submissions ;
323
323
324
- const submissions = await this . private . submissionsServices . getSubmissions ( {
325
- challengeId : challenge . legacy . id ,
324
+ const registrants = await this . getChallengeRegistrants ( challengeId ) ;
325
+ // Add submission date to registrants
326
+ registrants . forEach ( ( r , i ) => {
327
+ const submission = submissions . find ( s => s . memberId === Number ( r . memberId ) ) ;
328
+ if ( submission ) {
329
+ registrants [ i ] . submissionDate = submission . created ;
330
+ }
326
331
} ) ;
327
- challenge . submissions = submissions ;
332
+ challenge . registrants = registrants ;
328
333
329
334
challenge . fetchedWithAuth = Boolean ( this . private . apiV5 . private . token ) ;
330
335
@@ -337,18 +342,31 @@ class ChallengesService {
337
342
* @return {Promise } Resolves to the challenge registrants array.
338
343
*/
339
344
async getChallengeRegistrants ( challengeId ) {
340
- const m2mToken = await this . private . getTcM2mToken ( ) ;
341
- const apiM2M = getApi ( 'V5' , m2mToken ) ;
342
345
const roleId = await this . getResourceRoleId ( 'Submitter' ) ;
343
346
const params = {
344
347
challengeId,
345
348
roleId,
346
349
} ;
347
- const registrants = await apiM2M . get ( ` /resources?${ qs . stringify ( params ) } `)
348
- . then ( checkErrorV5 ) . then ( res => res ) ;
350
+ const url = ` ${ config . API . V5 } /resources?${ qs . stringify ( params ) } `;
351
+ const registrants = await this . private . proxyApi ( url ) ;
349
352
return registrants || [ ] ;
350
353
}
351
354
355
+ /**
356
+ * Gets challenge submissions from Topcoder API.
357
+ * @param {Number|String } challengeId
358
+ * @return {Promise } Resolves to the challenge registrants array.
359
+ */
360
+ async getChallengeSubmissions ( challengeId ) {
361
+ const params = {
362
+ challengeId,
363
+ perPage : 100 ,
364
+ } ;
365
+ const url = `${ config . API . V5 } /submissions?${ qs . stringify ( params ) } ` ;
366
+ const submissions = await this . private . proxyApi ( url ) ;
367
+ return submissions || [ ] ;
368
+ }
369
+
352
370
/**
353
371
* Gets possible challenge types.
354
372
* @return {Promise } Resolves to the array of subtrack names.
@@ -496,23 +514,15 @@ class ChallengesService {
496
514
name : roleName ,
497
515
isActive : true ,
498
516
} ;
499
- let api = this . private . apiV5 ;
500
-
501
- // Check if user is authenticated
502
- if ( ! api . private . token && isomorphy . isServerSide ( ) ) {
503
- // if not, make call with m2m token
504
- const m2mToken = await this . private . getTcM2mToken ( ) ;
505
- api = getApi ( 'V5' , m2mToken ) ;
506
- }
507
517
508
- const roles = await api . get ( ` /resource-roles?${ qs . stringify ( params ) } `)
509
- . then ( checkErrorV5 ) . then ( res => res ) ;
518
+ const url = ` ${ config . API . V5 } /resource-roles?${ qs . stringify ( params ) } `;
519
+ const roles = await this . private . proxyApi ( url ) ;
510
520
511
- if ( _ . isEmpty ( roles . result ) ) {
521
+ if ( _ . isEmpty ( roles ) ) {
512
522
throw new Error ( 'Resource Role not found!' ) ;
513
523
}
514
524
515
- return roles . result [ 0 ] . id ;
525
+ return roles [ 0 ] . id ;
516
526
}
517
527
518
528
/**
0 commit comments