@@ -20,6 +20,7 @@ import _ from 'lodash';
20
20
import { config } from 'topcoder-react-utils' ;
21
21
import logger from '../utils/logger' ;
22
22
import { getApi } from './api' ;
23
+ import { setErrorIcon , ERROR_ICON_TYPES } from '../utils/errors' ;
23
24
24
25
/* The value of USER_GROUP_MAXAGE constant converted to [ms]. */
25
26
const USER_GROUP_MAXAGE = config . USER_GROUP_MAXAGE * 1000 ;
@@ -142,6 +143,29 @@ function handleApiResponse(response) {
142
143
return response . json ( ) ;
143
144
}
144
145
146
+ /**
147
+ * Helper method that checks for HTTP error response v5 and throws Error in this case.
148
+ * @param {Object } res HTTP response object
149
+ * @return {Object } API JSON response object
150
+ * @private
151
+ */
152
+ async function checkErrorV5 ( res ) {
153
+ if ( ! res . ok ) {
154
+ if ( res . status === 403 ) {
155
+ setErrorIcon ( ERROR_ICON_TYPES . API , 'Auth0' , res . statusText ) ;
156
+ }
157
+ throw new Error ( res . statusText ) ;
158
+ }
159
+ const jsonRes = ( await res . json ( ) ) ;
160
+ if ( jsonRes . message ) {
161
+ throw new Error ( res . message ) ;
162
+ }
163
+ return {
164
+ result : jsonRes ,
165
+ headers : res . headers ,
166
+ } ;
167
+ }
168
+
145
169
/**
146
170
* Private. Merges given user group (possibly a tree of user groups) into
147
171
* groups map. This function intended only for internal use inside this module,
@@ -354,6 +378,21 @@ class GroupService {
354
378
getTokenV3 ( ) {
355
379
return this . private . tokenV3 ;
356
380
}
381
+
382
+ /**
383
+ * Gets the corresponding user's groups information
384
+ * @param {* } userId the userId
385
+ * @returns
386
+ */
387
+ async getMemberGroups ( userId ) {
388
+ const url = `/groups/memberGroups/${ userId } ` ;
389
+ const response = await this . private . api . get ( url )
390
+ . then ( res => checkErrorV5 ( res ) )
391
+ . then ( r => r . result || [ ] )
392
+ . catch ( ( ) => [ ] ) ;
393
+
394
+ return response ;
395
+ }
357
396
}
358
397
359
398
let lastInstance = null ;
0 commit comments