Skip to content

Commit fbc6220

Browse files
authored
Merge pull request #181 from simranb86/issue_4384
fix for Issue #4384
2 parents b34816f + 011284b commit fbc6220

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/services/challenges.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,17 @@ class ChallengesService {
403403
* @param {Object} params Optional.
404404
* @return {Promise} Resolves to the api response.
405405
*/
406-
getChallenges(filters, params) {
406+
async getChallenges(filters, params) {
407+
const memberId = this.private.tokenV3 ? decodeToken(this.private.tokenV3).userId : null;
408+
let userChallenges = [];
409+
if (memberId) {
410+
userChallenges = await this.private.apiV5.get(`/resources/${memberId}/challenges`)
411+
.then(checkErrorV5).then(res => res.result);
412+
}
407413
return this.private.getChallenges('/challenges/', filters, params)
408414
.then((res) => {
409-
res.challenges.forEach(item => normalizeChallenge(item));
415+
res.challenges.forEach(item => normalizeChallenge(item,
416+
userChallenges.includes(item.id) ? memberId : null));
410417
return res;
411418
});
412419
}
@@ -433,25 +440,29 @@ class ChallengesService {
433440
/**
434441
* Gets challenges of the specified user.
435442
* @param {String} userId User id whose challenges we want to fetch.
436-
* @param {Object} filters Optional.
437-
* @param {Number} params Optional.
438443
* @return {Promise} Resolves to the api response.
439444
*/
440445
getUserChallenges(userId, filters, params) {
441446
const userFilters = _.cloneDeep(filters);
442447
ChallengesService.updateFiltersParamsForGettingMemberChallenges(userFilters, params);
443-
const query = {
444-
...params,
445-
...userFilters,
446-
memberId: userId,
447-
};
448-
const endpoint = '/challenges';
449-
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
450-
451-
return this.private.apiV5.get(url)
452-
.then((res) => {
453-
res.challenges.forEach(item => normalizeChallenge(item, userId));
454-
return res;
448+
return this.private.apiV5.get(`/resources/${userId}/challenges`)
449+
.then(checkErrorV5).then((userChallenges) => {
450+
const query = {
451+
...params,
452+
...userFilters,
453+
ids: userChallenges.result,
454+
};
455+
const endpoint = '/challenges';
456+
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
457+
458+
return this.private.apiV5.get(url)
459+
.then(checkErrorV5).then((res) => {
460+
res.result.forEach(item => normalizeChallenge(item, userId));
461+
const newResponse = {};
462+
newResponse.challenges = res.result;
463+
newResponse.totalCount = res.result.length;
464+
return newResponse;
465+
});
455466
});
456467
}
457468

0 commit comments

Comments
 (0)