diff --git a/src/actions/challenge.js b/src/actions/challenge.js index 4627c0a3..c8fa4ace 100644 --- a/src/actions/challenge.js +++ b/src/actions/challenge.js @@ -7,6 +7,7 @@ import _ from 'lodash'; import { config } from 'topcoder-react-utils'; import { createActions } from 'redux-actions'; +import { decodeToken } from 'tc-accounts'; import { getService as getChallengesService } from '../services/challenges'; import { getService as getSubmissionService } from '../services/submissions'; import { getService as getMemberService } from '../services/members'; @@ -103,16 +104,20 @@ function getSubmissionsInit(challengeId) { * @desc Creates an action that loads user's submissions to the specified * challenge. * @param {String} challengeId Challenge ID. - * @param {String} tokenV2 Topcoder auth token v2. + * @param {String} tokenV3 Topcoder auth token v3. * @return {Action} */ -function getSubmissionsDone(challengeId, tokenV2) { - return getApi('V2', tokenV2) - .fetch(`/challenges/submissions/${challengeId}/mySubmissions`) - .then(response => response.json()) - .then(response => ({ +function getSubmissionsDone(challengeId, tokenV3) { + const user = decodeToken(tokenV3); + const submissionsService = getSubmissionService(tokenV3); + const filters = { + challengeId, + memberId: user.userId, + }; + return submissionsService.getSubmissions(filters) + .then(submissions => ({ challengeId: _.toString(challengeId), - submissions: response.submissions, + submissions, })) .catch((error) => { const err = { challengeId: _.toString(challengeId), error }; @@ -289,13 +294,13 @@ function fetchCheckpointsDone(tokenV2, challengeId) { response.checkpointResults[index].expanded = false; }); return { - challengeId: Number(challengeId), + challengeId: String(challengeId), checkpoints: response, }; }) .catch(error => ({ error, - challengeId: Number(challengeId), + challengeId: String(challengeId), })); } diff --git a/src/actions/smp.js b/src/actions/smp.js index 9c46f513..bded6d70 100644 --- a/src/actions/smp.js +++ b/src/actions/smp.js @@ -22,7 +22,7 @@ function deleteSubmissionInit() {} * @return {Action} */ function deleteSubmissionDone(tokenV3, submissionId) { - return getApi('V3', tokenV3).delete(`/submissions/${submissionId}`) + return getApi('V5', tokenV3).delete(`/submissions/${submissionId}`) .then(() => submissionId); } diff --git a/src/services/challenges.js b/src/services/challenges.js index 25811f62..079a5ef7 100644 --- a/src/services/challenges.js +++ b/src/services/challenges.js @@ -350,13 +350,11 @@ class ChallengesService { if (memberId) { isRegistered = _.some(registrants, r => r.memberId === memberId); - /** - * TODO: Currenlty using legacyId until submissions_api fix issue with UUID - */ const subParams = { - challengeId: challenge.legacyId, + challengeId, perPage: 100, }; + submissions = await this.private.submissionsService.getSubmissions(subParams); if (submissions) { diff --git a/src/services/submissions.js b/src/services/submissions.js index 36e78fb6..12f27021 100644 --- a/src/services/submissions.js +++ b/src/services/submissions.js @@ -5,8 +5,32 @@ */ import _ from 'lodash'; import qs from 'qs'; +import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors'; import { getApi } from './api'; +/** + * Helper method that checks for HTTP error response v5 and throws Error in this case. + * @param {Object} res HTTP response object + * @return {Object} API JSON response object + * @private + */ +async function checkErrorV5(res) { + if (!res.ok) { + if (res.status >= 500) { + setErrorIcon(ERROR_ICON_TYPES.API, '/challenges', res.statusText); + } + throw new Error(res.statusText); + } + const jsonRes = (await res.json()); + if (jsonRes.message) { + throw new Error(res.message); + } + return { + result: jsonRes, + headers: res.headers, + }; +} + /** * Submission service. */ @@ -36,8 +60,8 @@ class SubmissionsService { const url = `/submissions?${qs.stringify(query, { encode: false })}`; return this.private.apiV5.get(url) - .then(res => (res.ok ? res.json() : new Error(res.statusText))) - .then(res => res); + .then(checkErrorV5) + .then(res => res.result); } /** @@ -47,14 +71,14 @@ class SubmissionsService { async getScanReviewIds() { const reviews = await Promise.all([ this.private.apiV5.get('/reviewTypes?name=AV Scan') - .then(res => (res.ok ? res.json() : new Error(res.statusText))) - .then(res => res), + .then(checkErrorV5) + .then(res => res.result), this.private.apiV5.get('/reviewTypes?name=SonarQube Review') - .then(res => (res.ok ? res.json() : new Error(res.statusText))) - .then(res => res), + .then(checkErrorV5) + .then(res => res.result), this.private.apiV5.get('/reviewTypes?name=Virus Scan') - .then(res => (res.ok ? res.json() : new Error(res.statusText))) - .then(res => res), + .then(checkErrorV5) + .then(res => res.result), ]).then(([av, sonar, virus]) => (_.concat(av, sonar, virus))); return reviews.map(r => r.id);