Skip to content

Commit 5d9280d

Browse files
Added getChallengeSubmissions and getChallengeRegistrants
Removed directly call via m2m token and added call via tc proxy
1 parent 1d24bea commit 5d9280d

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

src/services/api.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,24 @@ 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(url) {
300+
let base = '';
301+
if (isomorphy.isServerSide()) {
302+
base = 'http://localhost';
303+
}
304+
const proxyUrl = `${base}/community-app-assets/api/proxy-get?url=${
305+
encodeURIComponent(url)
306+
}`;
307+
let res = await fetch(proxyUrl, {
308+
headers: { Authorization: `ApiKey ${config.SERVER_API_KEY}` },
309+
});
310+
if (!res.ok) throw new Error(res.statusText);
311+
res = (await res.json());
312+
return res;
313+
}

src/services/challenges.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import _ from 'lodash';
88
import moment from 'moment';
99
import qs from 'qs';
1010
import { decodeToken } from 'tc-accounts';
11-
import { isomorphy } from 'topcoder-react-utils';
11+
import { config } from 'topcoder-react-utils';
1212
import logger from '../utils/logger';
1313
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
1414
import { COMPETITION_TRACKS, getApiResponsePayload } from '../utils/tc';
15-
import { getTcM2mToken, getApi } from './api';
15+
import { getTcM2mToken, getApi, proxyApi } from './api';
1616
import { getService as getMembersService } from './members';
17-
import { getService as getSubmissionsService } from './submissions';
1817

1918
export const ORDER_BY = {
2019
SUBMISSION_END_DATE: 'submissionEndDate',
@@ -185,12 +184,12 @@ class ChallengesService {
185184
apiV2: getApi('V2', tokenV2),
186185
apiV3: getApi('V3', tokenV3),
187186
getTcM2mToken,
187+
proxyApi,
188188
getChallenges,
189189
getMemberChallenges,
190190
tokenV2,
191191
tokenV3,
192192
memberService: getMembersService(),
193-
submissionsServices: getSubmissionsService(tokenV3),
194193
};
195194
}
196195

@@ -306,7 +305,7 @@ class ChallengesService {
306305

307306
/**
308307
* Gets challenge details from Topcoder API.
309-
* NOTE: This function also uses API v2 and other endpoints for now, due
308+
* NOTE: This function also uses other endpoints for now, due
310309
* to some information is missing or
311310
* incorrect in the main endpoint. This may change in the future.
312311
* @param {Number|String} challengeId
@@ -316,15 +315,21 @@ class ChallengesService {
316315
const challenge = await this.private.getChallenges(`/challenges/${challengeId}`)
317316
.then(res => res.challenges);
318317

319-
if (isomorphy.isServerSide()) {
320-
const registrants = await this.getChallengeRegistrants(challengeId);
321-
challenge.registrants = registrants.result;
322-
}
318+
/**
319+
* TODO: Currenlty using legacyId until submissions_api fix issue with UUID
320+
*/
321+
const submissions = await this.getChallengeSubmissions(challenge.legacyId);
322+
challenge.submissions = submissions;
323323

324-
const submissions = await this.private.submissionsServices.getSubmissions({
325-
challengeId: challenge.legacy.id,
324+
const registrants = await this.getChallengeRegistrants(challengeId);
325+
// Add submission date to registrants
326+
registrants.forEach((r, i) => {
327+
const submission = submissions.find(s => s.memberId === Number(r.memberId));
328+
if (submission) {
329+
registrants[i].submissionDate = submission.created;
330+
}
326331
});
327-
challenge.submissions = submissions;
332+
challenge.registrants = registrants;
328333

329334
challenge.fetchedWithAuth = Boolean(this.private.apiV5.private.token);
330335

@@ -337,18 +342,31 @@ class ChallengesService {
337342
* @return {Promise} Resolves to the challenge registrants array.
338343
*/
339344
async getChallengeRegistrants(challengeId) {
340-
const m2mToken = await this.private.getTcM2mToken();
341-
const apiM2M = getApi('V5', m2mToken);
342345
const roleId = await this.getResourceRoleId('Submitter');
343346
const params = {
344347
challengeId,
345348
roleId,
346349
};
347-
const registrants = await apiM2M.get(`/resources?${qs.stringify(params)}`)
348-
.then(checkErrorV5).then(res => res);
350+
const url = `${config.API.V5}/resources?${qs.stringify(params)}`;
351+
const registrants = await this.private.proxyApi(url);
349352
return registrants || [];
350353
}
351354

355+
/**
356+
* Gets challenge submissions from Topcoder API.
357+
* @param {Number|String} challengeId
358+
* @return {Promise} Resolves to the challenge registrants array.
359+
*/
360+
async getChallengeSubmissions(challengeId) {
361+
const params = {
362+
challengeId,
363+
perPage: 100,
364+
};
365+
const url = `${config.API.V5}/submissions?${qs.stringify(params)}`;
366+
const submissions = await this.private.proxyApi(url);
367+
return submissions || [];
368+
}
369+
352370
/**
353371
* Gets possible challenge types.
354372
* @return {Promise} Resolves to the array of subtrack names.
@@ -496,23 +514,15 @@ class ChallengesService {
496514
name: roleName,
497515
isActive: true,
498516
};
499-
let api = this.private.apiV5;
500-
501-
// Check if user is authenticated
502-
if (!api.private.token && isomorphy.isServerSide()) {
503-
// if not, make call with m2m token
504-
const m2mToken = await this.private.getTcM2mToken();
505-
api = getApi('V5', m2mToken);
506-
}
507517

508-
const roles = await api.get(`/resource-roles?${qs.stringify(params)}`)
509-
.then(checkErrorV5).then(res => res);
518+
const url = `${config.API.V5}/resource-roles?${qs.stringify(params)}`;
519+
const roles = await this.private.proxyApi(url);
510520

511-
if (_.isEmpty(roles.result)) {
521+
if (_.isEmpty(roles)) {
512522
throw new Error('Resource Role not found!');
513523
}
514524

515-
return roles.result[0].id;
525+
return roles[0].id;
516526
}
517527

518528
/**

0 commit comments

Comments
 (0)