@@ -146,12 +146,19 @@ class ChallengesService {
146
146
} ;
147
147
const url = `${ endpoint } ?${ qs . stringify ( query ) } ` ;
148
148
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
+
149
156
return {
150
157
challenges : res . result || [ ] ,
151
158
totalCount : res . headers . get ( 'x-total' ) ,
152
159
meta : {
153
160
allChallengesCount : res . headers . get ( 'x-total' ) ,
154
- myChallengesCount : 0 ,
161
+ myChallengesCount : ( myChallenges . result && myChallenges . result . length ) || 0 ,
155
162
ongoingChallengesCount : 0 ,
156
163
openChallengesCount : 0 ,
157
164
totalCount : res . headers . get ( 'x-total' ) ,
@@ -406,10 +413,17 @@ class ChallengesService {
406
413
* @param {Object } params Optional.
407
414
* @return {Promise } Resolves to the api response.
408
415
*/
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
+ }
410
423
return this . private . getChallenges ( '/challenges/' , filters , params )
411
424
. then ( ( res ) => {
412
- res . challenges . forEach ( item => normalizeChallenge ( item ) ) ;
425
+ res . challenges . forEach ( item => normalizeChallenge ( item ,
426
+ userChallenges . includes ( item . id ) ? memberId : null ) ) ;
413
427
return res ;
414
428
} ) ;
415
429
}
@@ -440,21 +454,19 @@ class ChallengesService {
440
454
* @param {Number } params Optional.
441
455
* @return {Promise } Resolves to the api response.
442
456
*/
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
+ } ) ;
458
470
} ) ;
459
471
}
460
472
0 commit comments