Skip to content
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
107 changes: 107 additions & 0 deletions __tests__/shared/services/submissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* eslint-env jest */
import { config } from 'topcoder-react-utils';
import { getChallengeSubmissions } from '../../../src/shared/services/submissions';

const baseUrl = `${config.API.V6}/submissions`;

describe('submissions service', () => {
const originalFetch = global.fetch;
const originalHeaders = global.Headers;

beforeAll(() => {
if (!global.Headers) {
global.Headers = class HeadersMock {
constructor() {
this.values = {};
}

set(key, value) {
this.values[key] = value;
}

get(key) {
return this.values[key];
}
};
}
});

afterAll(() => {
global.fetch = originalFetch;
global.Headers = originalHeaders;
});

beforeEach(() => {
global.fetch = jest.fn();
});

it('loads every submissions page reported by metadata', async () => {
global.fetch
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
data: [{ id: 'submission-page-1' }],
meta: {
page: 1,
perPage: 100,
totalPages: 2,
totalItems: 101,
},
}),
})
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
data: [{ id: 'submission-page-2' }],
meta: {
page: 2,
perPage: 100,
totalPages: 2,
totalItems: 101,
},
}),
});

const result = await getChallengeSubmissions('token-v3', 'challenge-id');

expect(global.fetch).toHaveBeenCalledTimes(2);
expect(global.fetch).toHaveBeenNthCalledWith(
1,
`${baseUrl}?challengeId=challenge-id&perPage=500&page=1`,
expect.objectContaining({ method: 'GET' }),
);
expect(global.fetch).toHaveBeenNthCalledWith(
2,
`${baseUrl}?challengeId=challenge-id&perPage=500&page=2`,
expect.objectContaining({ method: 'GET' }),
);
expect(result.data).toEqual([
{ id: 'submission-page-1' },
{ id: 'submission-page-2' },
]);
expect(result.meta).toEqual(expect.objectContaining({
page: 2,
perPage: 500,
totalItems: 2,
totalPages: 2,
}));
});

it('uses the short-page heuristic when total pages are absent', async () => {
global.fetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
data: [{ id: 'submission-only-page' }],
meta: {
page: 1,
perPage: 100,
},
}),
});

const result = await getChallengeSubmissions(null, 'challenge-id');

expect(global.fetch).toHaveBeenCalledTimes(1);
expect(result.data).toEqual([{ id: 'submission-only-page' }]);
});
});
57 changes: 57 additions & 0 deletions __tests__/shared/utils/mm-review-summations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,61 @@ describe('buildMmSubmissionData', () => {
}),
]);
});

it('uses v6 submitter fields and submittedDate for imported raw submissions', () => {
const rawSubmissions = [
{
createdAt: '2026-04-09T05:00:55.279Z',
createdBy: 'historical-mm-importer',
finalScore: '7186.79',
id: 'submission-imported',
isLatest: true,
memberId: '16064986',
submittedDate: '2006-05-16T10:31:42.790Z',
submitterHandle: 'ctrucza',
submitterMaxRating: 1228,
},
];

const result = buildMmSubmissionData([], rawSubmissions);

expect(result).toHaveLength(1);
expect(result[0]).toEqual(expect.objectContaining({
member: 'ctrucza',
memberId: '16064986',
rating: 1228,
}));
expect(result[0].submissions).toEqual([
expect.objectContaining({
finalScore: 7186.79,
submissionId: 'submission-imported',
submissionTime: '2006-05-16T10:31:42.790Z',
}),
]);
});

it('uses reviewedDate before import createdAt for review summation times', () => {
const reviewSummations = [
{
aggregateScore: 7186.79,
createdAt: '2026-04-21T02:55:21.255Z',
id: 'summation-imported',
isFinal: true,
reviewedDate: '2006-05-16T10:31:42.790Z',
submissionId: 'submission-reviewed',
submitterHandle: 'ctrucza',
submitterId: '16064986',
},
];

const result = buildMmSubmissionData(reviewSummations);

expect(result[0].submissions).toEqual([
expect.objectContaining({
finalScore: 7186.79,
submissionId: 'submission-reviewed',
submissionTime: '2006-05-16T10:31:42.790Z',
}),
]);
});
});
31 changes: 25 additions & 6 deletions src/shared/containers/challenge-detail/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ import MetaTags from 'components/MetaTags';
import { decodeToken } from '@topcoder-platform/tc-auth-lib';
import { actions, errors, services } from 'topcoder-react-lib';
import { getService } from 'services/contentful';
import { getSubmissionArtifacts as getSubmissionArtifactsService } from 'services/submissions';
import {
getChallengeSubmissions as getChallengeSubmissionsService,
getSubmissionArtifacts as getSubmissionArtifactsService,
} from 'services/submissions';
import getReviewSummationsService from 'services/reviewSummations';
import { buildMmSubmissionData, buildStatisticsData } from 'utils/mm-review-summations';
import { appendUtmParamsToUrl } from 'utils/utm';
Expand Down Expand Up @@ -931,6 +934,9 @@ function extractArrayFromStateSlice(slice, challengeId) {
return slice;
}
if (slice && Array.isArray(slice.data)) {
if (slice.challengeId && _.toString(slice.challengeId) !== _.toString(challengeId)) {
return [];
}
return slice.data;
}
const key = challengeId ? String(challengeId) : null;
Expand Down Expand Up @@ -1159,8 +1165,10 @@ function mapStateToProps(state, props) {
? challenge.submissions
: (_.get(challenge, 'submissions.data') || []);
let mmSubmissions = extractArrayFromStateSlice(state.challenge.mmSubmissions, challengeId);
if (reviewSummations.length || rawChallengeSubmissions.length) {
if (reviewSummations.length) {
mmSubmissions = buildMmSubmissionData(reviewSummations, rawChallengeSubmissions);
} else if (!mmSubmissions.length && rawChallengeSubmissions.length) {
mmSubmissions = buildMmSubmissionData([], rawChallengeSubmissions);
}
const { auth } = state;
let statisticsData = extractArrayFromStateSlice(state.challenge.statisticsData, challengeId);
Expand Down Expand Up @@ -1513,22 +1521,33 @@ const mapDispatchToProps = (dispatch) => {
});
}

getReviewSummationsService(tokenV3, challengeIdStr)
.then(({ data }) => {
const challengeSubmissionsPromise = includeMmSubmissions
? getChallengeSubmissionsService(tokenV3, challengeIdStr)
: Promise.resolve({ data: [] });

Promise.all([
getReviewSummationsService(tokenV3, challengeIdStr),
challengeSubmissionsPromise,
])
.then(([{ data }, { data: rawSubmissions }]) => {
const reviewSummations = Array.isArray(data) ? data : [];
const rawChallengeSubmissions = Array.isArray(rawSubmissions) ? rawSubmissions : [];

dispatch({
type: 'CHALLENGE/GET_REVIEW_SUMMATIONS_DONE',
payload: reviewSummations,
meta: { challengeId: challengeIdStr },
});
if (includeMmSubmissions) {
const mmSubmissions = buildMmSubmissionData(reviewSummations);
const mmSubmissions = buildMmSubmissionData(reviewSummations, rawChallengeSubmissions);
dispatch({
type: 'CHALLENGE/GET_MM_SUBMISSIONS_DONE',
payload: {
challengeId: challengeIdStr,
submissions: mmSubmissions,
submissions: {
challengeId: challengeIdStr,
data: mmSubmissions,
},
},
});
}
Expand Down
96 changes: 93 additions & 3 deletions src/shared/services/submissions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,107 @@
import { config } from 'topcoder-react-utils';

const v5ApiUrl = config.API.V6;
const v6ApiUrl = config.API.V6;
const DEFAULT_PER_PAGE = 500;

export const downloadSubmissions = (tokenV3, submissionId, artifactId) => fetch(`${v5ApiUrl}/submissions/${submissionId}/artifacts/${artifactId}/download`, {
export const downloadSubmissions = (tokenV3, submissionId, artifactId) => fetch(`${v6ApiUrl}/submissions/${submissionId}/artifacts/${artifactId}/download`, {
method: 'GET',
headers: new Headers({
Authorization: `Bearer ${tokenV3}`,
}),
}).then(res => res.blob());

export const getSubmissionArtifacts = (tokenV3, submissionId) => fetch(`${v5ApiUrl}/submissions/${submissionId}/artifacts`, {
export const getSubmissionArtifacts = (tokenV3, submissionId) => fetch(`${v6ApiUrl}/submissions/${submissionId}/artifacts`, {
method: 'GET',
headers: new Headers({
Authorization: `Bearer ${tokenV3}`,
}),
}).then(res => res.json());

function getHeaders(tokenV3) {
const headers = new Headers();
if (tokenV3) {
headers.set('Authorization', `Bearer ${tokenV3}`);
}
return headers;
}

async function fetchChallengeSubmissionsPage({
tokenV3,
challengeId,
page,
perPage,
aggregated,
meta,
}) {
const url = `${v6ApiUrl}/submissions?challengeId=${encodeURIComponent(challengeId)}&perPage=${perPage}&page=${page}`;
const response = await fetch(url, {
method: 'GET',
headers: getHeaders(tokenV3),
});

if (!response.ok) {
const error = new Error(`Failed to fetch submissions: ${response.status} ${response.statusText}`);
error.status = response.status;
throw error;
}

const payload = await response.json();
const data = payload.data || [];
const combined = [...aggregated, ...data];
const latestMeta = payload.meta || meta;
const totalPages = payload.meta && (payload.meta.totalPages || payload.meta.total_pages);
const reachedEnd = !data.length
|| (totalPages ? page >= totalPages : data.length < perPage);

if (reachedEnd) {
return {
data: combined,
meta: latestMeta,
};
}

return fetchChallengeSubmissionsPage({
tokenV3,
challengeId,
page: page + 1,
perPage,
aggregated: combined,
meta: latestMeta,
});
}

/**
* Fetches every submission page for a challenge from the v6 submissions API.
*
* The challenge details service only embeds the first page of submissions, so
* Marathon Match views use this helper when they need complete member attempt
* history.
*
* @param {String} tokenV3 Topcoder auth token v3 used for private challenge access.
* @param {String|Number} challengeId Challenge identifier used by the submissions API.
* @param {Object} options Optional pagination settings.
* @param {Number} options.perPage Number of records requested per API page.
* @return {Promise<{data: Array, meta: Object}>} Aggregated submissions and
* final response metadata.
* @throws {Error} Throws when any submissions API page returns a non-2xx status.
*/
export async function getChallengeSubmissions(tokenV3, challengeId, options = {}) {
const { perPage = DEFAULT_PER_PAGE } = options;
const { data, meta } = await fetchChallengeSubmissionsPage({
tokenV3,
challengeId,
page: 1,
perPage,
aggregated: [],
meta: null,
});

return {
data,
meta: {
...(meta || {}),
totalItems: data.length,
perPage,
},
};
}
Loading
Loading