Skip to content

Commit 1aaf110

Browse files
committedJun 6, 2020
fix for issue #4384
1 parent ac74ffb commit 1aaf110

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed
 

‎src/services/challenges.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,19 @@ class ChallengesService {
146146
};
147147
const url = `${endpoint}?${qs.stringify(query)}`;
148148
const res = await this.private.apiV5.get(url).then(checkErrorV5);
149+
150+
const memberId = decodeToken(this.private.tokenV3).userId;
151+
let myChallenges = {};
152+
if (memberId) { // if token then check myChallenges count
153+
myChallenges = await this.private.apiV5.get(`/resources/${memberId}/challenges`).then(checkErrorV5);
154+
}
155+
149156
return {
150157
challenges: res.result || [],
151158
totalCount: res.headers.get('x-total'),
152159
meta: {
153160
allChallengesCount: res.headers.get('x-total'),
154-
myChallengesCount: 0,
161+
myChallengesCount: (myChallenges.result && myChallenges.result.length) || 0,
155162
ongoingChallengesCount: 0,
156163
openChallengesCount: 0,
157164
totalCount: res.headers.get('x-total'),
@@ -406,10 +413,17 @@ class ChallengesService {
406413
* @param {Object} params Optional.
407414
* @return {Promise} Resolves to the api response.
408415
*/
409-
getChallenges(filters, params) {
416+
async getChallenges(filters, params) {
417+
const memberId = this.private.tokenV3 ? decodeToken(this.private.tokenV3).userId : null;
418+
let userChallenges = [];
419+
if (memberId) {
420+
userChallenges = await this.private.apiV5.get(`/resources/${memberId}/challenges`)
421+
.then(checkErrorV5).then(res => res.result);
422+
}
410423
return this.private.getChallenges('/challenges/', filters, params)
411424
.then((res) => {
412-
res.challenges.forEach(item => normalizeChallenge(item));
425+
res.challenges.forEach(item => normalizeChallenge(item,
426+
userChallenges.includes(item.id) ? memberId : null));
413427
return res;
414428
});
415429
}
@@ -440,21 +454,19 @@ class ChallengesService {
440454
* @param {Number} params Optional.
441455
* @return {Promise} Resolves to the api response.
442456
*/
443-
getUserChallenges(userId, filters, params) {
444-
const userFilters = _.cloneDeep(filters);
445-
ChallengesService.updateFiltersParamsForGettingMemberChallenges(userFilters, params);
446-
const query = {
447-
...params,
448-
...userFilters,
449-
memberId: userId,
450-
};
451-
const endpoint = '/challenges';
452-
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
453-
454-
return this.private.apiV5.get(url)
455-
.then((res) => {
456-
res.challenges.forEach(item => normalizeChallenge(item, userId));
457-
return res;
457+
getUserChallenges(userId) {
458+
return this.private.apiV5.get(`/resources/${userId}/challenges`)
459+
.then(checkErrorV5).then((userChallenges) => {
460+
const param = { ids: userChallenges.result };
461+
const endpoint = `/challenges?${qs.stringify(param)}`;
462+
return this.private.apiV5.get(endpoint)
463+
.then(checkErrorV5).then((res) => {
464+
res.result.forEach(item => normalizeChallenge(item, userId));
465+
const newResponse = {};
466+
newResponse.challenges = res.result;
467+
newResponse.totalCount = res.result.length;
468+
return newResponse;
469+
});
458470
});
459471
}
460472

0 commit comments

Comments
 (0)