@@ -20,24 +20,23 @@ export const ORDER_BY = {
20
20
/**
21
21
* Normalizes a regular challenge details object received from the backend APIs.
22
22
* NOTE: It is possible, that this normalization is not necessary after we
23
- * have moved to Topcoder API v3 , but it is kept for now to minimize a risk of
23
+ * have moved to Topcoder API v4 , but it is kept for now to minimize a risk of
24
24
* breaking anything.
25
25
* @todo Why this one is exported? It should be only used internally!
26
26
* @param {Object } v4 Challenge object received from the /v4/challenges/{id}
27
27
* endpoint.
28
28
* @param {Object } v4Filtered Challenge object received from the
29
29
* /v4/challenges?filter=id={id} endpoint.
30
- * @param {Object } v3User Challenge object received from the
31
- * /v3/ /members/{username}/challenges?filter=id={id} endpoint.
32
- * If action was fired for authenticated visitor, v3_user will contain
30
+ * @param {Object } v4User Challenge object received from the
31
+ * /v4 /members/{username}/challenges?filter=id={id} endpoint.
32
+ * If action was fired for authenticated visitor, v4_user will contain
33
33
* details fetched specifically for the user (thus may include additional
34
- * data comparing to the standard API v3 response for the challenge details,
35
- * stored in v3_filtered).
36
- * @param {Object } v2 Challenge object received from the /v2/{type}/challenges/{id} endpoint.
34
+ * data comparing to the standard API v4 response for the challenge details,
35
+ * stored in v4_filtered).
37
36
* @param {String } username Optional.
38
37
* @return {Object } Normalized challenge object.
39
38
*/
40
- export function normalizeChallengeDetails ( v4 , v4Filtered , v3User , username ) {
39
+ export function normalizeChallengeDetails ( v4 , v4Filtered , v4User , username ) {
41
40
// Normalize exising data to make it consistent with the rest of the code
42
41
const challenge = {
43
42
...v4 ,
@@ -91,6 +90,7 @@ export function normalizeChallengeDetails(v4, v4Filtered, v3User, username) {
91
90
registrants : v4 . registrants || [ ] ,
92
91
} ;
93
92
93
+ /* It's unclear if these normalization steps are still required for V4 */
94
94
// Fill missing data from v3_filtered
95
95
if ( v4Filtered ) {
96
96
const groups = { } ;
@@ -101,7 +101,6 @@ export function normalizeChallengeDetails(v4, v4Filtered, v3User, username) {
101
101
}
102
102
103
103
// Normalize name convention for subtrack
104
- // const newsubTrack = normalizeNameConventionForSubtrack(v4Filtered.subTrack);
105
104
_ . defaults ( challenge , {
106
105
componentId : v4Filtered . componentId ,
107
106
contestId : v4Filtered . contestId ,
@@ -131,9 +130,9 @@ export function normalizeChallengeDetails(v4, v4Filtered, v3User, username) {
131
130
}
132
131
133
132
// Fill missing data from v3_user
134
- if ( v3User ) {
133
+ if ( v4User ) {
135
134
_ . defaults ( challenge , {
136
- userDetails : v3User . userDetails ,
135
+ userDetails : v4User . userDetails ,
137
136
} ) ;
138
137
}
139
138
@@ -231,27 +230,23 @@ class ChallengesService {
231
230
/**
232
231
* Private function being re-used in all methods related to getting
233
232
* challenges. It handles query-related arguments in the uniform way:
234
- * @param {String } endpoint API V3 endpoint, where the request will be send.
233
+ * @param {String } endpoint API V4 endpoint, where the request will be send.
235
234
* @param {Object } filters Optional. A map of filters to pass as `filter`
236
235
* query parameter (this function takes care to stringify it properly).
237
236
* @param {Object } params Optional. A map of any other parameters beside
238
237
* `filter`.
239
- * @param {Boolean } useV4 Optional. Whether a call to v4 API should be used.
240
238
*/
241
239
const getChallenges = async (
242
240
endpoint ,
243
241
filters = { } ,
244
242
params = { } ,
245
- useV4 = false ,
246
243
) => {
247
244
const query = {
248
245
filter : qs . stringify ( filters , { encode : false } ) ,
249
246
...params ,
250
247
} ;
251
248
const url = `${ endpoint } ?${ qs . stringify ( query ) } ` ;
252
- const res = useV4
253
- ? await this . private . apiV4 . get ( url ) . then ( checkError )
254
- : await this . private . api . get ( url ) . then ( checkError ) ;
249
+ const res = await this . private . apiV4 . get ( url ) . then ( checkError ) ;
255
250
return {
256
251
challenges : res . content || [ ] ,
257
252
totalCount : res . metadata . totalCount ,
@@ -352,17 +347,17 @@ class ChallengesService {
352
347
const challengeV4 = await this . private . apiV4 . get ( `/challenges/${ challengeId } ` )
353
348
. then ( checkError ) . then ( res => res . content ) ;
354
349
355
- const challengeV4Filtered = await this . private . getChallenges ( '/challenges/' , { id : challengeId } , { } , true )
350
+ const challengeV4Filtered = await this . private . getChallenges ( '/challenges/' , { id : challengeId } )
356
351
. then ( res => res . challenges [ 0 ] ) ;
357
352
358
353
const username = this . private . tokenV3 && decodeToken ( this . private . tokenV3 ) . handle ;
359
- const challengeV3User = username && await this . getUserChallenges ( username , { id : challengeId } )
360
- . then ( res => res . challenges [ 0 ] ) ;
354
+ const challengeV4User = username && await this . getUserChallenges ( username , { id : challengeId } )
355
+ . then ( res => res . challenges [ 0 ] ) . catch ( ( ) => null ) ;
361
356
362
357
const challenge = normalizeChallengeDetails (
363
358
challengeV4 ,
364
359
challengeV4Filtered ,
365
- challengeV3User ,
360
+ challengeV4User ,
366
361
username ,
367
362
) ;
368
363
@@ -406,7 +401,7 @@ class ChallengesService {
406
401
* @return {Promise } Resolves to the api response.
407
402
*/
408
403
getChallenges ( filters , params ) {
409
- return this . private . getChallenges ( '/challenges/' , filters , params , true )
404
+ return this . private . getChallenges ( '/challenges/' , filters , params )
410
405
. then ( ( res ) => {
411
406
res . challenges . forEach ( item => normalizeChallenge ( item ) ) ;
412
407
return res ;
@@ -459,7 +454,7 @@ class ChallengesService {
459
454
*/
460
455
async register ( challengeId ) {
461
456
const endpoint = `/challenges/${ challengeId } /register` ;
462
- const res = await this . private . api . postJson ( endpoint ) ;
457
+ const res = await this . private . apiV4 . postJson ( endpoint ) ;
463
458
if ( ! res . ok ) throw new Error ( res . statusText ) ;
464
459
return res . json ( ) ;
465
460
}
@@ -471,7 +466,7 @@ class ChallengesService {
471
466
*/
472
467
async unregister ( challengeId ) {
473
468
const endpoint = `/challenges/${ challengeId } /unregister` ;
474
- const res = await this . private . api . post ( endpoint ) ;
469
+ const res = await this . private . apiV4 . post ( endpoint ) ;
475
470
if ( ! res . ok ) throw new Error ( res . statusText ) ;
476
471
return res . json ( ) ;
477
472
}
0 commit comments