Skip to content

Commit 20f9ec5

Browse files
authored
Merge pull request #190 from topcoder-platform/issue-4380-v2
Issue4380 : Removed proxyApi and TEMP fix to registrants
2 parents 7c81d7b + 3b7222c commit 20f9ec5

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

__tests__/__snapshots__/index.js.snap

-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ Object {
291291
"getApiV4": [Function],
292292
"getApiV5": [Function],
293293
"getTcM2mToken": [Function],
294-
"proxyApi": [Function],
295294
},
296295
"billing": Object {
297296
"default": [Function],

src/services/api.js

-18
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,3 @@ export async function getTcM2mToken() {
290290
const token = await m2m.getMachineToken(TC_M2M.CLIENT_ID, TC_M2M.CLIENT_SECRET);
291291
return token;
292292
}
293-
294-
/**
295-
* Call API via proxy
296-
*
297-
* @param {String} url to API endpoint
298-
*/
299-
export async function proxyApi(endpoint) {
300-
let domain = '';
301-
if (isomorphy.isServerSide()) {
302-
domain = `http://${config.ENV.HOST || 'localhost'}:${config.ENV.PORT || 3000}`;
303-
}
304-
const url = `${domain}/community-app-assets/api${endpoint}`;
305-
let res = await fetch(url);
306-
if (!res.ok) throw new Error(res.statusText);
307-
res = (await res.json());
308-
if (res.message) throw new Error(res.message);
309-
return res;
310-
}

src/services/challenges.js

+33-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { decodeToken } from 'tc-accounts';
1111
import logger from '../utils/logger';
1212
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
1313
import { COMPETITION_TRACKS, getApiResponsePayload } from '../utils/tc';
14-
import { getApi, proxyApi } from './api';
14+
import { getApi } from './api';
1515
import { getService as getMembersService } from './members';
1616

1717
export const ORDER_BY = {
@@ -200,7 +200,6 @@ class ChallengesService {
200200
apiV3: getApi('V3', tokenV3),
201201
getChallenges,
202202
getMemberChallenges,
203-
proxyApi,
204203
tokenV2,
205204
tokenV3,
206205
memberService: getMembersService(),
@@ -326,8 +325,10 @@ class ChallengesService {
326325
* @return {Promise} Resolves to the challenge object.
327326
*/
328327
async getChallengeDetails(challengeId) {
328+
const user = decodeToken(this.private.tokenV3);
329329
let challenge = {};
330330
let isLegacyChallenge = false;
331+
let isRegistered = false;
331332
// condition based on ROUTE used for Review Opportunities, change if needed
332333
if (/^[\d]{5,8}$/.test(challengeId)) {
333334
isLegacyChallenge = true;
@@ -338,10 +339,22 @@ class ChallengesService {
338339
.then(res => res.challenges);
339340
}
340341

341-
const registrants = await this.getChallengeRegistrants(challenge.id);
342-
challenge.registrants = registrants;
342+
// TEMP FIX until API was fixed
343+
try {
344+
const registrants = await this.getChallengeRegistrants(challenge.id);
345+
challenge.registrants = registrants;
346+
} catch (err) {
347+
challenge.registrants = [];
348+
}
349+
350+
if (user) {
351+
const userChallenges = await this.private.apiV5.get(`/resources/${user.userId}/challenges`)
352+
.then(checkErrorV5).then(res => res.result);
353+
isRegistered = _.includes(userChallenges, challengeId);
354+
}
343355

344356
challenge.isLegacyChallenge = isLegacyChallenge;
357+
challenge.isRegistered = isRegistered;
345358

346359
challenge.events = _.map(challenge.events, e => ({
347360
eventName: e.key,
@@ -360,7 +373,19 @@ class ChallengesService {
360373
* @return {Promise} Resolves to the challenge registrants array.
361374
*/
362375
async getChallengeRegistrants(challengeId) {
363-
const registrants = await this.private.proxyApi(`/challenges/${challengeId}/registrants`);
376+
const roleId = await this.getRoleId('Submitter');
377+
const params = {
378+
challengeId,
379+
roleId,
380+
};
381+
382+
const registrants = await this.private.apiV5.get(`/resources?${qs.stringify(params)}`)
383+
.then(checkErrorV5).then(res => res.result);
384+
385+
if (_.isEmpty(registrants)) {
386+
throw new Error('Resource Role not found!');
387+
}
388+
364389
return registrants || [];
365390
}
366391

@@ -519,8 +544,10 @@ class ChallengesService {
519544
async getRoleId(roleName) {
520545
const params = {
521546
name: roleName,
547+
isActive: true,
522548
};
523-
const roles = await this.private.proxyApi(`/challenges/roleId?${qs.stringify(params)}`);
549+
const roles = await this.private.apiV5.get(`/resource-roles?${qs.stringify(params)}`)
550+
.then(checkErrorV5).then(res => res.result);
524551

525552
if (_.isEmpty(roles)) {
526553
throw new Error('Resource Role not found!');

0 commit comments

Comments
 (0)