Skip to content

Issue4536 - Challenge Details - Get submissions and merge with challenge return #197

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 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 55 additions & 19 deletions src/services/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
import { COMPETITION_TRACKS, getApiResponsePayload } from '../utils/tc';
import { getApi } from './api';
import { getService as getMembersService } from './members';
import { getService as getSubmissionsService } from './submissions';

export const ORDER_BY = {
SUBMISSION_END_DATE: 'submissionEndDate',
Expand Down Expand Up @@ -203,6 +204,7 @@ class ChallengesService {
tokenV2,
tokenV3,
memberService: getMembersService(),
submissionsService: getSubmissionsService(tokenV3),
};
}

Expand Down Expand Up @@ -327,8 +329,11 @@ class ChallengesService {
async getChallengeDetails(challengeId) {
const memberId = this.private.tokenV3 ? decodeToken(this.private.tokenV3).userId : null;
let challenge = {};
let registrants = [];
let submissions = [];
let isLegacyChallenge = false;
let isRegistered = false;

// condition based on ROUTE used for Review Opportunities, change if needed
if (/^[\d]{5,8}$/.test(challengeId)) {
isLegacyChallenge = true;
Expand All @@ -339,28 +344,59 @@ class ChallengesService {
.then(res => res.challenges);
}

let registrants = await this.getChallengeRegistrants(challenge.id);
// This TEMP fix to colorStyle, this will be fixed with issue #4530
registrants = _.map(registrants, r => ({
...r, colorStyle: 'color: #151516',
}));
challenge.registrants = registrants;
if (challenge) {
registrants = await this.getChallengeRegistrants(challenge.id);

// This TEMP fix to colorStyle, this will be fixed with issue #4530
registrants = _.map(registrants, r => ({
...r, colorStyle: 'color: #151516',
}));

/* Prepare data to logged user */
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,
perPage: 100,
};
submissions = await this.private.submissionsService.getSubmissions(subParams);

if (submissions) {
// Remove AV Scan, SonarQube Review and Virus Scan review types
const reviewScans = await this.private.submissionsService.getScanReviewIds();
submissions.forEach((s, i) => {
submissions[i].review = _.reject(s.review, r => _.includes(reviewScans, r.typeId));
});

// Add submission date to registrants
registrants.forEach((r, i) => {
const submission = submissions.find(s => s.memberId === Number(r.memberId));
if (submission) {
registrants[i].submissionDate = submission.created;
}
});
}
}

if (memberId) {
isRegistered = _.some(registrants, r => r.memberId === memberId);
challenge = {
...challenge,
isLegacyChallenge,
isRegistered,
registrants,
submissions,
events: _.map(challenge.events, e => ({
eventName: e.key,
eventId: e.id,
description: e.name,
})),
fetchedWithAuth: Boolean(this.private.apiV5.private.token),
};
}

challenge.isLegacyChallenge = isLegacyChallenge;
challenge.isRegistered = isRegistered;

challenge.events = _.map(challenge.events, e => ({
eventName: e.key,
eventId: e.id,
description: e.name,
}));

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

return challenge;
}

Expand Down
21 changes: 21 additions & 0 deletions src/services/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @desc This module provides a service for convenient manipulation with
* Topcoder submissions via TC API. Currently only used for MM challenges
*/
import _ from 'lodash';
import qs from 'qs';
import { getApi } from './api';

Expand Down Expand Up @@ -39,6 +40,26 @@ class SubmissionsService {
.then(res => res);
}

/**
* Get scan reviews types
* @returns {Promise} Resolves to the api response.
*/
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),
this.private.apiV5.get('/reviewTypes?name=SonarQube Review')
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
.then(res => res),
this.private.apiV5.get('/reviewTypes?name=Virus Scan')
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
.then(res => res),
]).then(([av, sonar, virus]) => (_.concat(av, sonar, virus)));

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

/**
* Get submission information by submission id
* @param submissionId The submission id
Expand Down