diff --git a/src/services/__mocks__/data/recommended-challenges.json b/src/services/__mocks__/data/recommended-challenges.json deleted file mode 100644 index c063290a..00000000 --- a/src/services/__mocks__/data/recommended-challenges.json +++ /dev/null @@ -1,34 +0,0 @@ -[ - { - "challengeId": "9bd8e6a7-b682-401e-bf7f-790daf9f6c60", - "matchScore": "-0.23" - }, - { - "challengeId": "d42df936-41aa-4c10-bd54-f30be8f62d47", - "matchScore": "0.76" - }, - { - "challengeId": "53383a29-80ef-4230-b48f-08a6d302f1ed", - "matchScore": "0" - }, - { - "challengeId": "e9994738-43e4-4725-8621-2eb687088c55", - "matchScore": "-1" - }, - { - "challengeId": "dd0f9e52-e0c9-4e11-8f3e-d75c786a2b65", - "matchScore": "-0.1" - }, - { - "challengeId": "a5c86414-474c-43db-97e1-6d8ad9322c6d", - "matchScore": "0.22" - }, - { - "challengeId": "45261650-418e-4b7f-aadb-edfd782cd407", - "matchScore": "0.78" - }, - { - "challengeId": "f466c20a-845a-46f6-8e91-b6152a5e6e32", - "matchScore": "-0.78" - } -] \ No newline at end of file diff --git a/src/services/challenges.js b/src/services/challenges.js index f79ec5c2..e251f678 100644 --- a/src/services/challenges.js +++ b/src/services/challenges.js @@ -14,7 +14,6 @@ import { COMPETITION_TRACKS, getApiResponsePayload } from '../utils/tc'; import { getApi } from './api'; import { getService as getMembersService } from './members'; import { getService as getSubmissionsService } from './submissions'; -import mockRecommendedChallenges from './__mocks__/data/recommended-challenges.json'; export function getFilterUrl(backendFilter, frontFilter) { const ff = _.clone(frontFilter); @@ -476,21 +475,12 @@ class ChallengesService { * @return {Promise} Resolves to the array of subtrack names. */ getChallengeTypes() { - const recommended = { - id: 'e06b074d-43c2-4e7e-9cd3-c43e13d51b40', - name: 'Recommended', - description: "Available challenges that match competitor's skills", - isActive: true, - isTask: false, - abbreviation: 'REC', - }; - return this.private.apiV5.get('/challenge-types') .then(res => (res.ok ? res.json() : new Error(res.statusText))) .then(res => ( res.message ? new Error(res.message) - : [...res, recommended] + : res )); } @@ -542,49 +532,36 @@ class ChallengesService { * Gets challenges. * @param {Object} filters Optional. * @param {Object} params Optional. + * @param {String} handle user handle * @return {Promise} Resolves to the api response. */ - async getRecommendedChallenges(sort, filter) { - return this.private.getChallenges('/challenges/', { - frontFilter: { ...filter, types: ['TSK', 'CH', 'F2F'] }, - }) - .then((res) => { - res.challenges.forEach(item => normalizeChallenge(item)); - let sortedChallenges = []; - const challenges = res.challenges.slice(0, 8).map((item, index) => ({ - ...item, - matchScore: mockRecommendedChallenges[index].matchScore, - })); - - const tracks = []; - const types = []; - if (filter.types.includes('CH')) types.push('Challenge'); - if (filter.types.includes('F2F')) types.push('First2Finish'); - if (filter.types.includes('TSK')) types.push('Task'); - - if (filter.tracks.DS) tracks.push('Data Science'); - if (filter.tracks.Des) tracks.push('Design'); - if (filter.tracks.Dev) tracks.push('Development'); - if (filter.tracks.QA) tracks.push('Quality Assurance'); - if (sort.openForRegistration === 'bestMatch' || sort.openForRegistration === {}) { - const ascArray = _.sortBy(challenges, [ - item => Math.trunc((parseFloat(item.matchScore) + 1.0) / 2.0 * 100.0)]); - sortedChallenges = _.reverse(ascArray); - } else if (sort.openForRegistration === 'name') { - sortedChallenges = _.sortBy(challenges, ['name']); - } else { - sortedChallenges = _.sortBy(challenges, [sort.openForRegistration]); - } - - let filteredChallenges = sortedChallenges.filter(item => tracks.includes(item.track)); - filteredChallenges = filteredChallenges.filter(item => types.includes(item.type)); - const mockResponse = _.clone(this.private.tokenV3 ? filteredChallenges : []); + async getRecommendedChallenges(filter, handle) { + const query = getFilterUrl( + filter.backendFilter, + { ...filter.frontFilter, per_page: filter.frontFilter.perPage }, + ); + + let res = {}; + if (_.some(filter.frontFilter.tracks, val => val) + && !_.isEqual(filter.frontFilter.types, [])) { + const url = `/recommender-api/${handle}?${query}`; + res = await this.private.apiV5.get(url).then(checkErrorV5); + } + const challenges = res.result.filter(ch => ch.jaccard_index > 0); - return { - challenges: mockResponse, - meta: mockResponse.length, - }; - }); + const totalCount = challenges.length; + return { + challenges, + totalCount, + meta: { + allChallengesCount: challenges.length, + allRecommendedChallengesCount: 0, + myChallengesCount: 0, + ongoingChallengesCount: 0, + openChallengesCount: 0, + totalCount, + }, + }; } /**