Skip to content

Commit 10fef55

Browse files
committed
feature/poc-recommender-sub-2
1 parent 28f06e3 commit 10fef55

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

src/services/challenges.js

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,18 +513,87 @@ class ChallengesService {
513513
));
514514
}
515515

516+
/**
517+
* Gets challenges recommend.
518+
*/
519+
async getChallengesRecommend(challenges) {
520+
// No Open Recommendation Challenges that match their skills
521+
// return Promise.resolve([]);
522+
const { tokenV2, tokenV3 } = this.private.tokenV2;
523+
if ((!tokenV2 && !tokenV3) || !challenges || challenges.length < 4) {
524+
return Promise.resolve([]);
525+
}
526+
return Promise.resolve([
527+
{
528+
'challenge-id': challenges[0].id,
529+
'match-score': 0.25,
530+
},
531+
{
532+
'challenge-id': challenges[1].id,
533+
'match-score': 0.01,
534+
},
535+
{
536+
'challenge-id': challenges[2].id,
537+
'match-score': -0.98,
538+
},
539+
{
540+
'challenge-id': challenges[3].id,
541+
'match-score': -0.24,
542+
},
543+
]);
544+
}
545+
516546
/**
517547
* Gets challenges.
518548
* @param {Object} filters Optional.
519549
* @param {Object} params Optional.
520550
* @return {Promise} Resolves to the api response.
521551
*/
522552
async getChallenges(filter) {
523-
return this.private.getChallenges('/challenges/', filter)
524-
.then((res) => {
525-
res.challenges.forEach(item => normalizeChallenge(item));
526-
return res;
527-
});
553+
const filterQuery = _.cloneDeep(filter);
554+
const types = _.get(filterQuery, 'frontFilter.types');
555+
const sortBy = _.get(filterQuery, 'frontFilter.sortBy');
556+
const sortOrder = _.get(filterQuery, 'frontFilter.sortOrder');
557+
const currentPhaseName = _.get(filterQuery, 'frontFilter.currentPhaseName');
558+
let isRecommended = false;
559+
if (types) {
560+
isRecommended = types.indexOf('Recommended') >= 0;
561+
filterQuery.frontFilter.types = null;
562+
}
563+
let isSortByBestMatch = false;
564+
if (sortBy === 'best-match') {
565+
filterQuery.frontFilter.sortBy = null;
566+
isSortByBestMatch = true;
567+
}
568+
const challengesResults = await this.private.getChallenges(
569+
'/challenges/',
570+
filterQuery,
571+
);
572+
challengesResults.challenges.forEach(item => normalizeChallenge(item));
573+
if (isRecommended && currentPhaseName === 'Registration') {
574+
const recommendedChallenges = await this.getChallengesRecommend(
575+
challengesResults.challenges,
576+
);
577+
challengesResults.challenges = _.filter(
578+
challengesResults.challenges,
579+
(c) => {
580+
const match = _.find(recommendedChallenges, { 'challenge-id': c.id });
581+
if (match) {
582+
c['match-score'] = match['match-score'];
583+
return true;
584+
}
585+
return false;
586+
},
587+
);
588+
if (isSortByBestMatch) {
589+
challengesResults.challenges = _.orderBy(
590+
challengesResults.challenges,
591+
['match-score'],
592+
[sortOrder],
593+
);
594+
}
595+
}
596+
return challengesResults;
528597
}
529598

530599
/**

0 commit comments

Comments
 (0)