Skip to content

Issue4582 - Update getSubmissionsDone() to use V5 API #205

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 7 commits into from
Jul 1, 2020
23 changes: 14 additions & 9 deletions src/actions/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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),
}));
}

Expand Down
2 changes: 1 addition & 1 deletion src/actions/smp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 2 additions & 4 deletions src/services/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 32 additions & 8 deletions src/services/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down