Skip to content

Commit 6c96a2f

Browse files
Merge branch 'integration-v5-challenge-api' into issue-4408
2 parents e5bbfac + 63d7e1e commit 6c96a2f

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"lint:js": "./node_modules/.bin/eslint --ext .js,.jsx .",
3232
"test": "npm run lint && npm run jest"
3333
},
34-
"version": "1000.19.9",
34+
"version": "1000.19.10",
3535
"dependencies": {
3636
"auth0-js": "^6.8.4",
3737
"config": "^3.2.0",

src/services/challenges.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,18 @@ class ChallengesService {
147147
};
148148
const url = `${endpoint}?${qs.stringify(query)}`;
149149
const res = await this.private.apiV5.get(url).then(checkErrorV5);
150-
let myChallengesCount = 0;
150+
let myChallenges;
151151
if (this.private.tokenV3) {
152152
const { userId } = decodeToken(this.private.tokenV3);
153-
myChallengesCount = await this.private.apiV5.get(`/resources/${userId}/challenges`)
154-
.then(checkErrorV5).then(userChallenges => userChallenges.headers.get('x-total'));
153+
myChallenges = await this.private.apiV5.get(`/resources/${userId}/challenges`)
154+
.then(checkErrorV5).then(userChallenges => userChallenges);
155155
}
156156
return {
157157
challenges: res.result || [],
158158
totalCount: res.headers.get('x-total'),
159159
meta: {
160160
allChallengesCount: res.headers.get('x-total'),
161-
myChallengesCount,
161+
myChallengesCount: (myChallenges && myChallenges.headers.get('x-total')) || 0,
162162
ongoingChallengesCount: 0,
163163
openChallengesCount: 0,
164164
totalCount: res.headers.get('x-total'),
@@ -489,28 +489,34 @@ class ChallengesService {
489489
* @param {String} userId User id whose challenges we want to fetch.
490490
* @return {Promise} Resolves to the api response.
491491
*/
492-
getUserChallenges(userId, filters, params) {
492+
async getUserChallenges(userId, filters, params) {
493493
const userFilters = _.cloneDeep(filters);
494494
ChallengesService.updateFiltersParamsForGettingMemberChallenges(userFilters, params);
495-
return this.private.apiV5.get(`/resources/${userId}/challenges`)
496-
.then(checkErrorV5).then((userChallenges) => {
497-
const query = {
498-
...params,
499-
...userFilters,
500-
ids: userChallenges.result,
501-
};
502-
const endpoint = '/challenges';
503-
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
504-
505-
return this.private.apiV5.get(url)
506-
.then(checkErrorV5).then((res) => {
507-
res.result.forEach(item => normalizeChallenge(item, userId));
508-
const newResponse = {};
509-
newResponse.challenges = res.result;
510-
newResponse.totalCount = res.result.length;
511-
return newResponse;
512-
});
513-
});
495+
496+
const userChallenges = await this.private.apiV5.get(`/resources/${userId}/challenges`)
497+
.then(checkErrorV5).then(res => res);
498+
499+
let chResponse = null;
500+
if (userChallenges.result && userChallenges.result.length > 0) {
501+
const query = {
502+
...params,
503+
...userFilters,
504+
ids: userChallenges.result,
505+
};
506+
const endpoint = '/challenges';
507+
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
508+
chResponse = await this.private.apiV5.get(url)
509+
.then(checkErrorV5).then((res) => {
510+
res.result.forEach(item => normalizeChallenge(item, userId));
511+
return res;
512+
});
513+
}
514+
515+
const chResult = (chResponse && chResponse.result) || [];
516+
return {
517+
challenges: chResult,
518+
totalCount: chResult.length,
519+
};
514520
}
515521

516522
/**

0 commit comments

Comments
 (0)