diff --git a/src/actions/challenge.js b/src/actions/challenge.js index 86655807..52efd29e 100644 --- a/src/actions/challenge.js +++ b/src/actions/challenge.js @@ -12,39 +12,7 @@ import { getService as getChallengesService } from '../services/challenges'; import { getService as getSubmissionService } from '../services/submissions'; import { getApi } from '../services/api'; import * as submissionUtil from '../utils/submission'; - -const { PAGE_SIZE } = CONFIG; - -/** - * Private. Loads from the backend all data matching some conditions. - * @param {Function} getter Given params object of shape { limit, offset } - * loads from the backend at most "limit" data, skipping the first - * "offset" ones. Returns loaded data as an array. - * @param {Number} page Optional. Next page of data to load. - * @param {Number} perPage Optional. The size of the page content to load. - * @param {Array} prev Optional. data loaded so far. - */ -function getAll(getter, page = 1, perPage = PAGE_SIZE, prev) { - /* Amount of submissions to fetch in one API call. 50 is the current maximum - * amount of submissions the backend returns, event when the larger limit is - * explicitely required. */ - return getter({ - page, - perPage, - }).then((res) => { - if (res.length === 0) { - return prev || res; - } - // parse submissions - let current = []; - if (prev) { - current = prev.concat(res); - } else { - current = res; - } - return getAll(getter, 1 + page, perPage, current); - }); -} +import { getAll } from '../utils/tc'; /** * @static diff --git a/src/services/challenges.js b/src/services/challenges.js index 65cae82d..595116cc 100644 --- a/src/services/challenges.js +++ b/src/services/challenges.js @@ -10,7 +10,7 @@ import qs from 'qs'; import { decodeToken } from '@topcoder-platform/tc-auth-lib'; import logger from '../utils/logger'; import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors'; -import { COMPETITION_TRACKS, getApiResponsePayload } from '../utils/tc'; +import { COMPETITION_TRACKS, getAll, getApiResponsePayload } from '../utils/tc'; import { getApi } from './api'; import { getService as getMembersService } from './members'; import { getService as getSubmissionsService } from './submissions'; @@ -402,12 +402,14 @@ class ChallengesService { if (memberId) { isRegistered = _.some(registrants, r => `${r.memberId}` === `${memberId}`); - const subParams = { + const filter = { challengeId, - perPage: challenge.numOfSubmissions ? challenge.numOfSubmissions : 100, }; - submissions = await this.private.submissionsService.getSubmissions(subParams); + submissions = await getAll( + params => this.private.submissionsService.getSubmissions(filter, params), + 1, + ); if (submissions) { // Remove AV Scan, SonarQube Review and Virus Scan review types diff --git a/src/services/submissions.js b/src/services/submissions.js index 12f27021..c2fdf85e 100644 --- a/src/services/submissions.js +++ b/src/services/submissions.js @@ -52,7 +52,7 @@ class SubmissionsService { * @param {Object} params * @return {Promise} Resolves to the api response. */ - async getSubmissions(filters, params) { + async getSubmissions(filters, params = {}) { const query = { ...filters, ...params, diff --git a/src/utils/tc.js b/src/utils/tc.js index cf0c470a..6b63428a 100644 --- a/src/utils/tc.js +++ b/src/utils/tc.js @@ -101,3 +101,34 @@ export async function getLookerApiResponsePayload(res) { status: x.status, }; } + +/** + * Private. Loads from the backend all data matching some conditions. + * @param {Function} getter Given params object of shape { limit, offset } + * loads from the backend at most "limit" data, skipping the first + * "offset" ones. Returns loaded data as an array. + * @param {Number} page Optional. Next page of data to load. + * @param {Number} perPage Optional. The size of the page content to load. + * @param {Array} prev Optional. data loaded so far. + */ +export function getAll(getter, page = 1, perPage = 500, prev) { + /* Amount of items to fetch in one API call. 50 is the current maximum + * amount of items the backend returns, event when the larger limit is + * explicitely required. */ + return getter({ + page, + perPage, + }).then((res) => { + if (res.length === 0) { + return prev || res; + } + // parse items + let current = []; + if (prev) { + current = prev.concat(res); + } else { + current = res; + } + return getAll(getter, 1 + page, perPage, current); + }); +}