Skip to content

Commit 156381b

Browse files
committed
Updates getUserChallenges to use v4.
1 parent 31e84ed commit 156381b

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Object {
238238
"default": [Function],
239239
"getApiV2": [Function],
240240
"getApiV3": [Function],
241+
"getApiV4": [Function],
241242
},
242243
"billing": Object {
243244
"default": [Function],
@@ -251,7 +252,6 @@ Object {
251252
"getService": [Function],
252253
"normalizeChallenge": [Function],
253254
"normalizeChallengeDetails": [Function],
254-
"normalizeMarathonMatch": [Function],
255255
},
256256
"communities": Object {
257257
"default": undefined,

src/services/challenges.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@ export const ORDER_BY = {
2020
/**
2121
* Normalizes a regular challenge details object received from the backend APIs.
2222
* 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
2424
* breaking anything.
2525
* @todo Why this one is exported? It should be only used internally!
2626
* @param {Object} v4 Challenge object received from the /v4/challenges/{id}
2727
* endpoint.
2828
* @param {Object} v4Filtered Challenge object received from the
2929
* /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
3333
* 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).
3736
* @param {String} username Optional.
3837
* @return {Object} Normalized challenge object.
3938
*/
40-
export function normalizeChallengeDetails(v4, v4Filtered, v3User, username) {
39+
export function normalizeChallengeDetails(v4, v4Filtered, v4User, username) {
4140
// Normalize exising data to make it consistent with the rest of the code
4241
const challenge = {
4342
...v4,
@@ -91,6 +90,7 @@ export function normalizeChallengeDetails(v4, v4Filtered, v3User, username) {
9190
registrants: v4.registrants || [],
9291
};
9392

93+
/* It's unclear if these normalization steps are still required for V4 */
9494
// Fill missing data from v3_filtered
9595
if (v4Filtered) {
9696
const groups = {};
@@ -101,7 +101,6 @@ export function normalizeChallengeDetails(v4, v4Filtered, v3User, username) {
101101
}
102102

103103
// Normalize name convention for subtrack
104-
// const newsubTrack = normalizeNameConventionForSubtrack(v4Filtered.subTrack);
105104
_.defaults(challenge, {
106105
componentId: v4Filtered.componentId,
107106
contestId: v4Filtered.contestId,
@@ -131,9 +130,9 @@ export function normalizeChallengeDetails(v4, v4Filtered, v3User, username) {
131130
}
132131

133132
// Fill missing data from v3_user
134-
if (v3User) {
133+
if (v4User) {
135134
_.defaults(challenge, {
136-
userDetails: v3User.userDetails,
135+
userDetails: v4User.userDetails,
137136
});
138137
}
139138

@@ -231,27 +230,23 @@ class ChallengesService {
231230
/**
232231
* Private function being re-used in all methods related to getting
233232
* 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.
235234
* @param {Object} filters Optional. A map of filters to pass as `filter`
236235
* query parameter (this function takes care to stringify it properly).
237236
* @param {Object} params Optional. A map of any other parameters beside
238237
* `filter`.
239-
* @param {Boolean} useV4 Optional. Whether a call to v4 API should be used.
240238
*/
241239
const getChallenges = async (
242240
endpoint,
243241
filters = {},
244242
params = {},
245-
useV4 = false,
246243
) => {
247244
const query = {
248245
filter: qs.stringify(filters, { encode: false }),
249246
...params,
250247
};
251248
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);
255250
return {
256251
challenges: res.content || [],
257252
totalCount: res.metadata.totalCount,
@@ -352,17 +347,17 @@ class ChallengesService {
352347
const challengeV4 = await this.private.apiV4.get(`/challenges/${challengeId}`)
353348
.then(checkError).then(res => res.content);
354349

355-
const challengeV4Filtered = await this.private.getChallenges('/challenges/', { id: challengeId }, {}, true)
350+
const challengeV4Filtered = await this.private.getChallenges('/challenges/', { id: challengeId })
356351
.then(res => res.challenges[0]);
357352

358353
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);
361356

362357
const challenge = normalizeChallengeDetails(
363358
challengeV4,
364359
challengeV4Filtered,
365-
challengeV3User,
360+
challengeV4User,
366361
username,
367362
);
368363

@@ -406,7 +401,7 @@ class ChallengesService {
406401
* @return {Promise} Resolves to the api response.
407402
*/
408403
getChallenges(filters, params) {
409-
return this.private.getChallenges('/challenges/', filters, params, true)
404+
return this.private.getChallenges('/challenges/', filters, params)
410405
.then((res) => {
411406
res.challenges.forEach(item => normalizeChallenge(item));
412407
return res;
@@ -459,7 +454,7 @@ class ChallengesService {
459454
*/
460455
async register(challengeId) {
461456
const endpoint = `/challenges/${challengeId}/register`;
462-
const res = await this.private.api.postJson(endpoint);
457+
const res = await this.private.apiV4.postJson(endpoint);
463458
if (!res.ok) throw new Error(res.statusText);
464459
return res.json();
465460
}
@@ -471,7 +466,7 @@ class ChallengesService {
471466
*/
472467
async unregister(challengeId) {
473468
const endpoint = `/challenges/${challengeId}/unregister`;
474-
const res = await this.private.api.post(endpoint);
469+
const res = await this.private.apiV4.post(endpoint);
475470
if (!res.ok) throw new Error(res.statusText);
476471
return res.json();
477472
}

0 commit comments

Comments
 (0)