Skip to content

Commit b72d035

Browse files
committed
feat: add support to fetch memberGroups
1 parent e713264 commit b72d035

File tree

8 files changed

+69
-12
lines changed

8 files changed

+69
-12
lines changed

__tests__/__snapshots__/index.js.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`Library interface test 1`] = `
44
Object {
55
"actions": Object {
66
"auth": Object {
7-
"getMemberGroups": [Function],
7+
"getAuthenticatedMemberGroups": [Function],
88
"loadProfile": [Function],
99
"setTcTokenV2": [Function],
1010
"setTcTokenV3": [Function],
@@ -54,6 +54,7 @@ Object {
5454
"dropGroups": [Function],
5555
"getGroupsDone": [Function],
5656
"getGroupsInit": [Function],
57+
"getMemberGroups": [Function],
5758
},
5859
"looker": Object {
5960
"getLookerDone": [Function],

__tests__/reducers/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const photoURL = 'http://url';
88
const mockActions = {
99
auth: {
1010
loadProfile: mockAction('LOAD_PROFILE', Promise.resolve('Profile')),
11-
getMemberGroups: mockAction('GET_MEMBER_GROUPS', Promise.resolve(['Group'])),
11+
getAuthenticatedMemberGroups: mockAction('GET_AUTHENTICATED_MEMBER_GROUPS', Promise.resolve(['Group'])),
1212
setTcTokenV2: mockAction('SET_TC_TOKEN_V2', 'Token V2'),
1313
setTcTokenV3: mockAction('SET_TC_TOKEN_V3', 'Token V3'),
1414
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"lint:js": "./node_modules/.bin/eslint --ext .js,.jsx .",
3232
"test": "npm run lint && npm run jest"
3333
},
34-
"version": "1000.27.15",
34+
"version": "1000.27.16",
3535
"dependencies": {
3636
"auth0-js": "^6.8.4",
3737
"config": "^3.2.0",

src/actions/auth.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createActions } from 'redux-actions';
77
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
88
import { getApiV3, getApiV5 } from '../services/api';
99
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
10+
import { getService } from '../services/groups';
1011

1112
/**
1213
* Helper method that checks for HTTP error response v5 and throws Error in this case.
@@ -77,19 +78,17 @@ function setTcTokenV3(tokenV3) {
7778
* @param {*} tokenV3 the member's token
7879
* @returns
7980
*/
80-
async function getMemberGroups(tokenV3) {
81+
async function getAuthenticatedMemberGroups(tokenV3) {
8182
if (!tokenV3) return Promise.resolve([]);
8283
const user = decodeToken(tokenV3);
83-
const apiV5 = getApiV5(tokenV3);
84-
const res = await apiV5.get(`/groups/memberGroups/${user.userId}`).then(checkErrorV5).then(r => r.result || []);
85-
return res;
84+
return getService(tokenV3).getMemberGroups(user.userId);
8685
}
8786

8887
export default createActions({
8988
AUTH: {
9089
LOAD_PROFILE: loadProfileDone,
9190
SET_TC_TOKEN_V2: setTcTokenV2,
9291
SET_TC_TOKEN_V3: setTcTokenV3,
93-
GET_MEMBER_GROUPS: getMemberGroups,
92+
GET_AUTHENTICATED_MEMBER_GROUPS: getAuthenticatedMemberGroups,
9493
},
9594
});

src/actions/groups.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,21 @@ function getGroupsDone(groupIds, tokenV3) {
4242
return getService(tokenV3).getGroupMap(groupIds);
4343
}
4444

45+
/**
46+
* Get groups that a member belong to
47+
* @param {*} userId the member's userId
48+
* @param {*} tokenV3 the logged in users token
49+
* @returns
50+
*/
51+
function getMemberGroups(userId, tokenV3) {
52+
return getService(tokenV3).getMemberGroups(userId);
53+
}
54+
4555
export default createActions({
4656
GROUPS: {
4757
DROP_GROUPS: dropGroups,
4858
GET_GROUPS_INIT: getGroupsInit,
4959
GET_GROUPS_DONE: getGroupsDone,
60+
GET_MEMBER_GROUPS: getMemberGroups,
5061
},
5162
});

src/reducers/auth.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { redux } from 'topcoder-react-utils';
1818
import actions from '../actions/auth';
1919
import profileActions from '../actions/profile';
2020

21-
function onGetMemberGroups(state, action) {
21+
function onGetAuthenticatedMemberGroups(state, action) {
2222
return { ...state, memberGroups: action.payload };
2323
}
2424

@@ -43,7 +43,7 @@ function onProfileLoaded(state, action) {
4343
*/
4444
function create(initialState) {
4545
return redux.handleActions({
46-
[actions.auth.getMemberGroups]: onGetMemberGroups,
46+
[actions.auth.getAuthenticatedMemberGroups]: onGetAuthenticatedMemberGroups,
4747
[actions.auth.loadProfile]: onProfileLoaded,
4848
[actions.auth.setTcTokenV2]: (state, action) => ({
4949
...state,
@@ -134,10 +134,10 @@ export async function factory(options = {}) {
134134
if (state.tokenV3) {
135135
state.user = decodeToken(state.tokenV3);
136136
let a = actions.auth.loadProfile(state.tokenV3);
137-
let g = actions.auth.getMemberGroups(state.tokenV3);
137+
let g = actions.auth.getAuthenticatedMemberGroups(state.tokenV3);
138138
a = await redux.resolveAction(a);
139139
g = await redux.resolveAction(g);
140-
return create(onGetMemberGroups(onProfileLoaded(state, a), g));
140+
return create(onGetAuthenticatedMemberGroups(onProfileLoaded(state, a), g));
141141
}
142142
return create(state);
143143
}

src/reducers/groups.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import _ from 'lodash';
2424
import { handleActions } from 'redux-actions';
2525
import actions from '../actions/groups';
26+
// import { getApiResponsePayload } from '../utils/tc';
2627

2728
/**
2829
* Private. Given two user group maps, it adds to "dst" the root group from
@@ -84,6 +85,10 @@ function onGetGroupsDone(state, action) {
8485
return { ...state, groups, loading };
8586
}
8687

88+
function onGetMemberGroups(state, action) {
89+
return { ...state, memberGroups: action.payload };
90+
}
91+
8792

8893
/**
8994
* Creates a new Groups reducer with the specified initial state.
@@ -96,9 +101,11 @@ function create(initialState) {
96101
[a.dropGroups]: onDropGroups,
97102
[a.getGroupsInit]: onGetGroupsInit,
98103
[a.getGroupsDone]: onGetGroupsDone,
104+
[a.getMemberGroups]: onGetMemberGroups,
99105
}, _.defaults(initialState ? _.clone(initialState) : {}, {
100106
groups: {},
101107
loading: {},
108+
memberGroups: [],
102109
}));
103110
}
104111

src/services/groups.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import _ from 'lodash';
2020
import { config } from 'topcoder-react-utils';
2121
import logger from '../utils/logger';
2222
import { getApi } from './api';
23+
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
2324

2425
/* The value of USER_GROUP_MAXAGE constant converted to [ms]. */
2526
const USER_GROUP_MAXAGE = config.USER_GROUP_MAXAGE * 1000;
@@ -142,6 +143,29 @@ function handleApiResponse(response) {
142143
return response.json();
143144
}
144145

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+
145169
/**
146170
* Private. Merges given user group (possibly a tree of user groups) into
147171
* groups map. This function intended only for internal use inside this module,
@@ -354,6 +378,21 @@ class GroupService {
354378
getTokenV3() {
355379
return this.private.tokenV3;
356380
}
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+
}
357396
}
358397

359398
let lastInstance = null;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy