Skip to content

Commit d685a39

Browse files
authored
Merge pull request #205 from topcoder-platform/issue-4582
Issue4582 - Update getSubmissionsDone() to use V5 API
2 parents 45d09bb + 910cebf commit d685a39

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

src/actions/challenge.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +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';
1011
import { getService as getChallengesService } from '../services/challenges';
1112
import { getService as getSubmissionService } from '../services/submissions';
1213
import { getService as getMemberService } from '../services/members';
@@ -103,16 +104,20 @@ function getSubmissionsInit(challengeId) {
103104
* @desc Creates an action that loads user's submissions to the specified
104105
* challenge.
105106
* @param {String} challengeId Challenge ID.
106-
* @param {String} tokenV2 Topcoder auth token v2.
107+
* @param {String} tokenV3 Topcoder auth token v3.
107108
* @return {Action}
108109
*/
109-
function getSubmissionsDone(challengeId, tokenV2) {
110-
return getApi('V2', tokenV2)
111-
.fetch(`/challenges/submissions/${challengeId}/mySubmissions`)
112-
.then(response => response.json())
113-
.then(response => ({
110+
function getSubmissionsDone(challengeId, tokenV3) {
111+
const user = decodeToken(tokenV3);
112+
const submissionsService = getSubmissionService(tokenV3);
113+
const filters = {
114+
challengeId,
115+
memberId: user.userId,
116+
};
117+
return submissionsService.getSubmissions(filters)
118+
.then(submissions => ({
114119
challengeId: _.toString(challengeId),
115-
submissions: response.submissions,
120+
submissions,
116121
}))
117122
.catch((error) => {
118123
const err = { challengeId: _.toString(challengeId), error };
@@ -289,13 +294,13 @@ function fetchCheckpointsDone(tokenV2, challengeId) {
289294
response.checkpointResults[index].expanded = false;
290295
});
291296
return {
292-
challengeId: Number(challengeId),
297+
challengeId: String(challengeId),
293298
checkpoints: response,
294299
};
295300
})
296301
.catch(error => ({
297302
error,
298-
challengeId: Number(challengeId),
303+
challengeId: String(challengeId),
299304
}));
300305
}
301306

src/actions/smp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function deleteSubmissionInit() {}
2222
* @return {Action}
2323
*/
2424
function deleteSubmissionDone(tokenV3, submissionId) {
25-
return getApi('V3', tokenV3).delete(`/submissions/${submissionId}`)
25+
return getApi('V5', tokenV3).delete(`/submissions/${submissionId}`)
2626
.then(() => submissionId);
2727
}
2828

src/services/challenges.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,11 @@ class ChallengesService {
350350
if (memberId) {
351351
isRegistered = _.some(registrants, r => r.memberId === memberId);
352352

353-
/**
354-
* TODO: Currenlty using legacyId until submissions_api fix issue with UUID
355-
*/
356353
const subParams = {
357-
challengeId: challenge.legacyId,
354+
challengeId,
358355
perPage: 100,
359356
};
357+
360358
submissions = await this.private.submissionsService.getSubmissions(subParams);
361359

362360
if (submissions) {

src/services/submissions.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,32 @@
55
*/
66
import _ from 'lodash';
77
import qs from 'qs';
8+
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
89
import { getApi } from './api';
910

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 >= 500) {
20+
setErrorIcon(ERROR_ICON_TYPES.API, '/challenges', 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+
}
33+
1034
/**
1135
* Submission service.
1236
*/
@@ -36,8 +60,8 @@ class SubmissionsService {
3660

3761
const url = `/submissions?${qs.stringify(query, { encode: false })}`;
3862
return this.private.apiV5.get(url)
39-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
40-
.then(res => res);
63+
.then(checkErrorV5)
64+
.then(res => res.result);
4165
}
4266

4367
/**
@@ -47,14 +71,14 @@ class SubmissionsService {
4771
async getScanReviewIds() {
4872
const reviews = await Promise.all([
4973
this.private.apiV5.get('/reviewTypes?name=AV Scan')
50-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
51-
.then(res => res),
74+
.then(checkErrorV5)
75+
.then(res => res.result),
5276
this.private.apiV5.get('/reviewTypes?name=SonarQube Review')
53-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
54-
.then(res => res),
77+
.then(checkErrorV5)
78+
.then(res => res.result),
5579
this.private.apiV5.get('/reviewTypes?name=Virus Scan')
56-
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
57-
.then(res => res),
80+
.then(checkErrorV5)
81+
.then(res => res.result),
5882
]).then(([av, sonar, virus]) => (_.concat(av, sonar, virus)));
5983

6084
return reviews.map(r => r.id);

0 commit comments

Comments
 (0)