From e9311704c62d41ab13f9c82706f474d820e34df9 Mon Sep 17 00:00:00 2001 From: M Fikri A Date: Fri, 2 Apr 2021 17:26:00 +0700 Subject: [PATCH 1/2] Fetch All Submissions --- src/actions/challenge.js | 34 +--------------------------------- src/services/challenges.js | 7 +++---- src/services/submissions.js | 3 ++- src/utils/tc.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 38 deletions(-) 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..704c2a67 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,11 @@ 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, 500); 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..1b85cb0d 100644 --- a/src/services/submissions.js +++ b/src/services/submissions.js @@ -7,6 +7,7 @@ import _ from 'lodash'; import qs from 'qs'; import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors'; import { getApi } from './api'; +import { getAll } from '../utils/tc' /** * Helper method that checks for HTTP error response v5 and throws Error in this case. @@ -52,7 +53,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..ec58f258 100644 --- a/src/utils/tc.js +++ b/src/utils/tc.js @@ -3,6 +3,7 @@ * @desc Collection of small Topcoder-related functions. * @todo More TC-related utils should be moved here from Community-app. */ +const { PAGE_SIZE } = CONFIG; /** * Codes of the Topcoder communities. @@ -101,3 +102,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 = PAGE_SIZE, 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); + }); +} From c7c23ead33840d279622aa0fcc4ae42b89bf0a80 Mon Sep 17 00:00:00 2001 From: M Fikri A Date: Fri, 2 Apr 2021 17:35:51 +0700 Subject: [PATCH 2/2] Fix lint --- src/services/challenges.js | 7 +++++-- src/services/submissions.js | 1 - src/utils/tc.js | 3 +-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/services/challenges.js b/src/services/challenges.js index 704c2a67..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, getAll, 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'; @@ -406,7 +406,10 @@ class ChallengesService { challengeId, }; - submissions = await getAll(params => this.private.submissionsService.getSubmissions(filter, params), 1, 500); + 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 1b85cb0d..c2fdf85e 100644 --- a/src/services/submissions.js +++ b/src/services/submissions.js @@ -7,7 +7,6 @@ import _ from 'lodash'; import qs from 'qs'; import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors'; import { getApi } from './api'; -import { getAll } from '../utils/tc' /** * Helper method that checks for HTTP error response v5 and throws Error in this case. diff --git a/src/utils/tc.js b/src/utils/tc.js index ec58f258..6b63428a 100644 --- a/src/utils/tc.js +++ b/src/utils/tc.js @@ -3,7 +3,6 @@ * @desc Collection of small Topcoder-related functions. * @todo More TC-related utils should be moved here from Community-app. */ -const { PAGE_SIZE } = CONFIG; /** * Codes of the Topcoder communities. @@ -112,7 +111,7 @@ export async function getLookerApiResponsePayload(res) { * @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 = PAGE_SIZE, prev) { +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. */