Skip to content

Issue-4587 : Update action loadProfileDone() to use v3 API #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions __tests__/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const MOCK_GROUPS_REQ_URL = 'https://api.topcoder-dev.com/v5/groups?memberId=12345&membershipType=user';
const MOCK_PROFILE_REQ_URL = 'https://api.topcoder-dev.com/v5/members/username12345';
const MOCK_PROFILE_REQ_URL = 'https://api.topcoder-dev.com/v3/members/username12345';

jest.mock('isomorphic-fetch', () => jest.fn(url => Promise.resolve({
ok: true,
json: () => {
let content;
switch (url) {
case MOCK_GROUPS_REQ_URL: content = ['Group1', 'Group2']; break;
case MOCK_PROFILE_REQ_URL: content = [{ userId: 12345 }]; break;
case MOCK_GROUPS_REQ_URL:
content = ['Group1', 'Group2'];
break;
case MOCK_PROFILE_REQ_URL:
content = { result: { content: { userId: 12345 }, status: 200 } };
break;
default: throw new Error('Unexpected URL!');
}
return content;
Expand Down
14 changes: 7 additions & 7 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

import { createActions } from 'redux-actions';
import { decodeToken } from 'tc-accounts';
import { getApi } from '../services/api';
import { getApiV3, getApiV5 } from '../services/api';

/**
* @static
* @desc Creates an action that loads Topcoder user profile from v5 API.
* @desc Creates an action that loads Topcoder user profile from v3 API.
* @param {String} userTokenV3 v3 authentication token.
* @return {Action}
*/
function loadProfileDone(userTokenV3) {
if (!userTokenV3) return Promise.resolve(null);
const user = decodeToken(userTokenV3);
const api = getApi('V5', userTokenV3);
const apiV3 = getApiV3(userTokenV3);
const apiV5 = getApiV5(userTokenV3);
return Promise.all([
api.get(`/members/${user.handle}`)
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
.then(res => (res.message ? new Error(res.message) : res[0])),
api.get(`/groups?memberId=${user.userId}&membershipType=user`)
apiV3.get(`/members/${user.handle}`)
.then(res => res.json()).then(res => (res.result.status === 200 ? res.result.content : {})),
apiV5.get(`/groups?memberId=${user.userId}&membershipType=user`)
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
.then(res => (res.message ? new Error(res.message) : res)),
]).then(([profile, groups]) => ({ ...profile, groups }));
Expand Down