@@ -147,18 +147,18 @@ class ChallengesService {
147
147
} ;
148
148
const url = `${ endpoint } ?${ qs . stringify ( query ) } ` ;
149
149
const res = await this . private . apiV5 . get ( url ) . then ( checkErrorV5 ) ;
150
- let myChallengesCount = 0 ;
150
+ let myChallenges ;
151
151
if ( this . private . tokenV3 ) {
152
152
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 ) ;
155
155
}
156
156
return {
157
157
challenges : res . result || [ ] ,
158
158
totalCount : res . headers . get ( 'x-total' ) ,
159
159
meta : {
160
160
allChallengesCount : res . headers . get ( 'x-total' ) ,
161
- myChallengesCount,
161
+ myChallengesCount : ( myChallenges && myChallenges . headers . get ( 'x-total' ) ) || 0 ,
162
162
ongoingChallengesCount : 0 ,
163
163
openChallengesCount : 0 ,
164
164
totalCount : res . headers . get ( 'x-total' ) ,
@@ -489,28 +489,34 @@ class ChallengesService {
489
489
* @param {String } userId User id whose challenges we want to fetch.
490
490
* @return {Promise } Resolves to the api response.
491
491
*/
492
- getUserChallenges ( userId , filters , params ) {
492
+ async getUserChallenges ( userId , filters , params ) {
493
493
const userFilters = _ . cloneDeep ( filters ) ;
494
494
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
+ } ;
514
520
}
515
521
516
522
/**
0 commit comments