Skip to content

Commit 2e8ce50

Browse files
authored
Merge pull request #183 from simranb86/issue_4384_part_3_feedback
fix for issue #4384 feedback
2 parents 8449cba + 186cd45 commit 2e8ce50

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/services/challenges.js

+30-24
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ class ChallengesService {
146146
};
147147
const url = `${endpoint}?${qs.stringify(query)}`;
148148
const res = await this.private.apiV5.get(url).then(checkErrorV5);
149-
let myChallengesCount = 0;
149+
let myChallenges;
150150
if (typeof this.private.tokenV3 !== 'undefined') {
151151
const { userId } = decodeToken(this.private.tokenV3);
152-
myChallengesCount = await this.private.apiV5.get(`/resources/${userId}/challenges`)
153-
.then(checkErrorV5).then(userChallenges => userChallenges.headers.get('x-total'));
152+
myChallenges = await this.private.apiV5.get(`/resources/${userId}/challenges`)
153+
.then(checkErrorV5).then(userChallenges => userChallenges);
154154
}
155155
return {
156156
challenges: res.result || [],
157157
totalCount: res.headers.get('x-total'),
158158
meta: {
159159
allChallengesCount: res.headers.get('x-total'),
160-
myChallengesCount,
160+
myChallengesCount: (myChallenges && myChallenges.headers.get('x-total')) || 0,
161161
ongoingChallengesCount: 0,
162162
openChallengesCount: 0,
163163
totalCount: res.headers.get('x-total'),
@@ -448,28 +448,34 @@ class ChallengesService {
448448
* @param {String} userId User id whose challenges we want to fetch.
449449
* @return {Promise} Resolves to the api response.
450450
*/
451-
getUserChallenges(userId, filters, params) {
451+
async getUserChallenges(userId, filters, params) {
452452
const userFilters = _.cloneDeep(filters);
453453
ChallengesService.updateFiltersParamsForGettingMemberChallenges(userFilters, params);
454-
return this.private.apiV5.get(`/resources/${userId}/challenges`)
455-
.then(checkErrorV5).then((userChallenges) => {
456-
const query = {
457-
...params,
458-
...userFilters,
459-
ids: userChallenges.result,
460-
};
461-
const endpoint = '/challenges';
462-
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
463-
464-
return this.private.apiV5.get(url)
465-
.then(checkErrorV5).then((res) => {
466-
res.result.forEach(item => normalizeChallenge(item, userId));
467-
const newResponse = {};
468-
newResponse.challenges = res.result;
469-
newResponse.totalCount = res.result.length;
470-
return newResponse;
471-
});
472-
});
454+
455+
const userChallenges = await this.private.apiV5.get(`/resources/${userId}/challenges`)
456+
.then(checkErrorV5).then(res => res);
457+
458+
let chResponse = null;
459+
if (userChallenges.result && userChallenges.result.length > 0) {
460+
const query = {
461+
...params,
462+
...userFilters,
463+
ids: userChallenges.result,
464+
};
465+
const endpoint = '/challenges';
466+
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
467+
chResponse = await this.private.apiV5.get(url)
468+
.then(checkErrorV5).then((res) => {
469+
res.result.forEach(item => normalizeChallenge(item, userId));
470+
return res;
471+
});
472+
}
473+
474+
const chResult = (chResponse && chResponse.result) || [];
475+
return {
476+
challenges: chResult,
477+
totalCount: chResult.length,
478+
};
473479
}
474480

475481
/**

0 commit comments

Comments
 (0)