Skip to content

Commit ed85b7d

Browse files
Merge branch 'develop' into support_for_job_description
2 parents e0ef678 + 8109cb2 commit ed85b7d

File tree

16 files changed

+178
-66
lines changed

16 files changed

+178
-66
lines changed

__tests__/__snapshots__/index.js.snap

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Object {
305305
"SUBMISSION_END_DATE": "submissionEndDate",
306306
},
307307
"default": undefined,
308+
"getFilterUrl": [Function],
308309
"getService": [Function],
309310
"normalizeChallenge": [Function],
310311
},
@@ -373,10 +374,16 @@ Object {
373374
"processMMSubmissions": [Function],
374375
},
375376
"tc": Object {
377+
"CHALLENGE_STATUS": Object {
378+
"ACTIVE": "Active",
379+
"CANCELLED": "Cancelled",
380+
"COMPLETED": "Completed",
381+
"DRAFT": "Draft",
382+
},
376383
"COMPETITION_TRACKS": Object {
377-
"DATA_SCIENCE": "Data Science",
378-
"DESIGN": "Design",
379-
"DEVELOP": "Development",
384+
"DES": "Design",
385+
"DEV": "Development",
386+
"DS": "Data Science",
380387
"QA": "Quality Assurance",
381388
},
382389
"OLD_COMPETITION_TRACKS": Object {

__tests__/reducers/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const mockActions = {
2020
jest.setMock(require.resolve('actions/auth'), mockActions);
2121
jest.setMock(require.resolve('actions/profile'), mockActions);
2222

23-
jest.setMock('tc-accounts', {
23+
jest.setMock('@topcoder-platform/tc-auth-lib', {
2424
decodeToken: () => 'User object',
2525
isTokenExpired: () => false,
2626
});

__tests__/reducers/challenge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const mockSmpActions = {
4444
};
4545
_.merge(actions, mockSmpActions);
4646

47-
jest.setMock('tc-accounts', {
47+
jest.setMock('@topcoder-platform/tc-auth-lib', {
4848
decodeToken: () => 'User object',
4949
isTokenExpired: () => false,
5050
});

config/jest/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global jest */
22
require('topcoder-react-utils/config/jest/setup');
33

4-
jest.setMock('tc-accounts', {
4+
jest.setMock('@topcoder-platform/tc-auth-lib', {
55
decodeToken: token => (token ? {
66
handle: 'username12345',
77
userId: '12345',

config/webpack/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = {
2222
'redux',
2323
'redux-actions',
2424
'isomorphic-fetch',
25-
'tc-accounts',
25+
'@topcoder-platform/tc-auth-lib',
2626
'to-capital-case',
2727
'topcoder-react-utils',
2828
'tc-core-library-js',

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
"react-redux": "^6.0.1",
4848
"redux": "^3.7.2",
4949
"redux-actions": "^2.4.0",
50-
"tc-accounts": "https://github.com/appirio-tech/accounts-app.git#dev",
5150
"tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.6",
5251
"to-capital-case": "^1.0.0",
53-
"topcoder-react-utils": "0.7.5"
52+
"topcoder-react-utils": "0.7.5",
53+
"@topcoder-platform/tc-auth-lib": "git+https://github.com/topcoder-platform/tc-auth-lib.git#1.0.1"
5454
},
5555
"devDependencies": {
5656
"autoprefixer": "^8.6.4",

src/actions/auth.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,32 @@
44
*/
55

66
import { createActions } from 'redux-actions';
7-
import { decodeToken } from 'tc-accounts';
7+
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
88
import { getApiV3, getApiV5 } from '../services/api';
9+
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
10+
11+
/**
12+
* Helper method that checks for HTTP error response v5 and throws Error in this case.
13+
* @param {Object} res HTTP response object
14+
* @return {Object} API JSON response object
15+
* @private
16+
*/
17+
async function checkErrorV5(res) {
18+
if (!res.ok) {
19+
if (res.status === 403) {
20+
setErrorIcon(ERROR_ICON_TYPES.API, 'Auth0', res.statusText);
21+
}
22+
throw new Error(res.statusText);
23+
}
24+
const jsonRes = (await res.json());
25+
if (jsonRes.message) {
26+
throw new Error(res.message);
27+
}
28+
return {
29+
result: jsonRes,
30+
headers: res.headers,
31+
};
32+
}
933

1034
/**
1135
* @static
@@ -22,8 +46,7 @@ function loadProfileDone(userTokenV3) {
2246
apiV3.get(`/members/${user.handle}`)
2347
.then(res => res.json()).then(res => (res.result.status === 200 ? res.result.content : {})),
2448
apiV5.get(`/groups?memberId=${user.userId}&membershipType=user`)
25-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
26-
.then(res => (res.message ? new Error(res.message) : res)),
49+
.then(checkErrorV5).then(res => res.result || []),
2750
]).then(([profile, groups]) => ({ ...profile, groups }));
2851
}
2952

src/actions/challenge.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import _ from 'lodash';
88
import { config } from 'topcoder-react-utils';
99
import { createActions } from 'redux-actions';
10-
import { decodeToken } from 'tc-accounts';
10+
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
1111
import { getService as getChallengesService } from '../services/challenges';
1212
import { getService as getSubmissionService } from '../services/submissions';
1313
import { getApi } from '../services/api';
@@ -248,7 +248,7 @@ function loadResultsInit(challengeId) {
248248
* @return {Action}
249249
*/
250250
function loadResultsDone(auth, challengeId, type) {
251-
return getApi('V2', auth.tokenV2)
251+
return getApi('V2')
252252
.fetch(`/${type}/challenges/result/${challengeId}`)
253253
.then(response => response.json())
254254
.then(response => ({
@@ -273,7 +273,7 @@ function fetchCheckpointsInit() {}
273273
*/
274274
function fetchCheckpointsDone(tokenV2, challengeId) {
275275
const endpoint = `/design/challenges/checkpoint/${challengeId}`;
276-
return getApi('V2', tokenV2).fetch(endpoint)
276+
return getApi('V2').fetch(endpoint)
277277
.then((response) => {
278278
if (response.status !== 200) {
279279
throw response.status;

src/reducers/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
import _ from 'lodash';
16-
import { decodeToken } from 'tc-accounts';
16+
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
1717
import { redux } from 'topcoder-react-utils';
1818
import actions from '../actions/auth';
1919
import profileActions from '../actions/profile';

src/reducers/challenge.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function onFetchCheckpointsDone(state, action) {
173173
loadingCheckpoints: false,
174174
};
175175
}
176-
if (state.details && state.details.legacyId === action.payload.challengeId) {
176+
if (state.details && `${state.details.legacyId}` === `${action.payload.challengeId}`) {
177177
return {
178178
...state,
179179
checkpoints: action.payload.checkpoints,
@@ -471,7 +471,7 @@ export function factory(options = {}) {
471471
const challengeDetails = _.get(res, 'payload', {});
472472
const track = _.get(challengeDetails, 'track', '');
473473
let checkpointsPromise = null;
474-
if (track === COMPETITION_TRACKS.DESIGN) {
474+
if (track === COMPETITION_TRACKS.DES) {
475475
const p = _.get(challengeDetails, 'phases', [])
476476
.filter(x => x.name === 'Checkpoint Review');
477477
if (p.length && !p[0].isOpen) {

src/reducers/direct.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import _ from 'lodash';
88
import { handleActions } from 'redux-actions';
9-
import { decodeToken } from 'tc-accounts';
9+
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
1010
import actions from '../actions/direct';
1111
import logger from '../utils/logger';
1212
import { fireErrorMessage } from '../utils/errors';

src/services/challenges.js

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,51 @@
77
import _ from 'lodash';
88
import moment from 'moment';
99
import qs from 'qs';
10-
import { decodeToken } from 'tc-accounts';
10+
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
1111
import logger from '../utils/logger';
1212
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
1313
import { COMPETITION_TRACKS, getApiResponsePayload } from '../utils/tc';
1414
import { getApi } from './api';
1515
import { getService as getMembersService } from './members';
1616
import { getService as getSubmissionsService } from './submissions';
1717

18+
export function getFilterUrl(backendFilter, frontFilter) {
19+
const ff = _.clone(frontFilter);
20+
// eslint-disable-next-line object-curly-newline
21+
const { tags, tracks, types, groups, events } = ff;
22+
delete ff.tags;
23+
delete ff.tracks;
24+
delete ff.types;
25+
delete ff.communityId;
26+
delete ff.groups;
27+
delete ff.events;
28+
29+
// console.log(ff);
30+
31+
let urlFilter = qs.stringify(_.reduce(ff, (result, value, key) => {
32+
// eslint-disable-next-line no-param-reassign
33+
if (value) result[key] = value;
34+
return result;
35+
}, {}));
36+
// console.log(urlFilter);
37+
38+
const ftags = _.map(tags, val => `tags[]=${val}`).join('&');
39+
const ftracks = _.map(_.reduce(tracks, (result, value, key) => {
40+
// eslint-disable-next-line no-unused-expressions
41+
tracks[key] && result.push(key);
42+
return result;
43+
}, []), val => `tracks[]=${val}`).join('&');
44+
const ftypes = _.map(types, val => `types[]=${val}`).join('&');
45+
const fgroups = _.map(groups, val => `groups[]=${val}`).join('&');
46+
const fevents = _.map(events, val => `events[]=${val}`).join('&');
47+
if (ftags.length > 0) urlFilter += `&${ftags}`;
48+
if (ftracks.length > 0) urlFilter += `&${ftracks}`;
49+
if (ftypes.length > 0) urlFilter += `&${ftypes}`;
50+
if (fgroups.length > 9) urlFilter += `&${fgroups}`;
51+
if (fevents.length > 0) urlFilter += `&${fevents}`;
52+
return urlFilter;
53+
}
54+
1855
export const ORDER_BY = {
1956
SUBMISSION_END_DATE: 'submissionEndDate',
2057
};
@@ -133,27 +170,42 @@ class ChallengesService {
133170
*/
134171
const getChallenges = async (
135172
endpoint,
136-
filters = {},
137-
params = {},
173+
filter,
138174
) => {
139-
const query = {
140-
...filters,
141-
...params,
142-
};
143-
const url = `${endpoint}?${qs.stringify(query)}`;
144-
const res = await this.private.apiV5.get(url).then(checkErrorV5);
175+
let res = {};
176+
if (_.some(filter.frontFilter.tracks, val => val)) {
177+
const query = getFilterUrl(filter.backendFilter, filter.frontFilter);
178+
const url = `${endpoint}?${query}`;
179+
res = await this.private.apiV5.get(url).then(checkErrorV5);
180+
}
145181
return {
146182
challenges: res.result || [],
147-
totalCount: res.headers.get('x-total'),
183+
totalCount: res.headers ? res.headers.get('x-total') : 0,
148184
meta: {
149-
allChallengesCount: res.headers.get('x-total'),
185+
allChallengesCount: res.headers ? res.headers.get('x-total') : 0,
150186
myChallengesCount: 0,
151187
ongoingChallengesCount: 0,
152188
openChallengesCount: 0,
153-
totalCount: res.headers.get('x-total'),
189+
totalCount: res.headers ? res.headers.get('x-total') : 0,
154190
},
155191
};
156192
};
193+
194+
const getChallengeDetails = async (
195+
endpoint,
196+
legacyInfo,
197+
) => {
198+
let query = '';
199+
if (legacyInfo) {
200+
query = `legacyId=${legacyInfo.legacyId}`;
201+
}
202+
const url = `${endpoint}?${query}`;
203+
const res = await this.private.apiV5.get(url).then(checkErrorV5);
204+
return {
205+
challenges: res.result || [],
206+
};
207+
};
208+
157209
/**
158210
* Private function being re-used in all methods related to getting
159211
* challenges. It handles query-related arguments in the uniform way:
@@ -189,6 +241,7 @@ class ChallengesService {
189241
apiV2: getApi('V2', tokenV2),
190242
apiV3: getApi('V3', tokenV3),
191243
getChallenges,
244+
getChallengeDetails,
192245
getMemberChallenges,
193246
tokenV2,
194247
tokenV3,
@@ -327,10 +380,10 @@ class ChallengesService {
327380
// condition based on ROUTE used for Review Opportunities, change if needed
328381
if (/^[\d]{5,8}$/.test(challengeId)) {
329382
isLegacyChallenge = true;
330-
challenge = await this.private.getChallenges('/challenges/', { legacyId: challengeId })
383+
challenge = await this.private.getChallengeDetails('/challenges/', { legacyId: challengeId })
331384
.then(res => res.challenges[0] || {});
332385
} else {
333-
challenge = await this.private.getChallenges(`/challenges/${challengeId}`)
386+
challenge = await this.private.getChallengeDetails(`/challenges/${challengeId}`)
334387
.then(res => res.challenges);
335388
}
336389

@@ -464,8 +517,8 @@ class ChallengesService {
464517
* @param {Object} params Optional.
465518
* @return {Promise} Resolves to the api response.
466519
*/
467-
async getChallenges(filters, params) {
468-
return this.private.getChallenges('/challenges/', filters, params)
520+
async getChallenges(filter) {
521+
return this.private.getChallenges('/challenges/', filter)
469522
.then((res) => {
470523
res.challenges.forEach(item => normalizeChallenge(item));
471524
return res;
@@ -642,7 +695,7 @@ class ChallengesService {
642695
let contentType;
643696
let url;
644697

645-
if (track === COMPETITION_TRACKS.DESIGN) {
698+
if (track === COMPETITION_TRACKS.DES) {
646699
({ api } = this.private);
647700
contentType = 'application/json';
648701
url = '/submissions/'; // The submission info is contained entirely in the JSON body
@@ -660,7 +713,7 @@ class ChallengesService {
660713
}, onProgress).then((res) => {
661714
const jres = JSON.parse(res);
662715
// Return result for Develop submission
663-
if (track === COMPETITION_TRACKS.DEVELOP) {
716+
if (track === COMPETITION_TRACKS.DEV) {
664717
return jres;
665718
}
666719
// Design Submission requires an extra "Processing" POST

src/services/members.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/* global XMLHttpRequest */
88
import _ from 'lodash';
99
import qs from 'qs';
10-
import { decodeToken } from 'tc-accounts';
10+
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
1111
import logger from '../utils/logger';
1212
import { getApiResponsePayload } from '../utils/tc';
1313
import { getApi } from './api';

src/services/reviewOpportunities.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ export function normalizeChallenges(opportunities) {
1717
* until receive API V5 update. */
1818
_.map(opportunities, (opportunity) => {
1919
const { challenge } = opportunity;
20-
challenge.track = COMPETITION_TRACKS.DEVELOP;
20+
challenge.track = COMPETITION_TRACKS.DEV;
2121
if (challenge.technologies) {
22-
if (challenge.technologies.includes(COMPETITION_TRACKS.DATA_SCIENCE)) {
23-
challenge.track = COMPETITION_TRACKS.DATA_SCIENCE;
22+
if (challenge.technologies.includes(COMPETITION_TRACKS.DS)) {
23+
challenge.track = COMPETITION_TRACKS.DS;
2424
}
2525
} else if (challenge.subTrack === OLD_SUBTRACKS.TEST_SUITES
2626
|| challenge.subTrack === OLD_SUBTRACKS.BUG_HUNT
2727
|| challenge.subTrack === OLD_COMPETITION_TRACKS.TEST_SCENARIOS
2828
|| challenge.subTrack === OLD_COMPETITION_TRACKS.TESTING_COMPETITION) {
2929
challenge.track = COMPETITION_TRACKS.QA;
3030
} else if (challenge.track === OLD_COMPETITION_TRACKS.DESIGN) {
31-
challenge.track = COMPETITION_TRACKS.DESIGN;
31+
challenge.track = COMPETITION_TRACKS.DES;
3232
}
3333
return _.defaults(opportunity, { challenge });
3434
});

0 commit comments

Comments
 (0)