@@ -146,18 +146,18 @@ 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
- let myChallengesCount = 0 ;
149
+ let myChallenges ;
150
150
if ( typeof this . private . tokenV3 !== 'undefined' ) {
151
151
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 ) ;
154
154
}
155
155
return {
156
156
challenges : res . result || [ ] ,
157
157
totalCount : res . headers . get ( 'x-total' ) ,
158
158
meta : {
159
159
allChallengesCount : res . headers . get ( 'x-total' ) ,
160
- myChallengesCount,
160
+ myChallengesCount : ( myChallenges && myChallenges . headers . get ( 'x-total' ) ) || 0 ,
161
161
ongoingChallengesCount : 0 ,
162
162
openChallengesCount : 0 ,
163
163
totalCount : res . headers . get ( 'x-total' ) ,
@@ -448,28 +448,34 @@ class ChallengesService {
448
448
* @param {String } userId User id whose challenges we want to fetch.
449
449
* @return {Promise } Resolves to the api response.
450
450
*/
451
- getUserChallenges ( userId , filters , params ) {
451
+ async getUserChallenges ( userId , filters , params ) {
452
452
const userFilters = _ . cloneDeep ( filters ) ;
453
453
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
+ } ;
473
479
}
474
480
475
481
/**
0 commit comments