Skip to content

Commit

Permalink
implement new recomandation
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Sep 15, 2024
1 parent 3e151e8 commit 343de66
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions features/cocktail/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@ const { buildImages, buildOgImage } = require("../../utils/image");
const Database = require('../../database/newclient');
const { getCocktailBySlug } = require("../../database/cocktail");

async function getRecomendations(cocktail) {
const goodMatchSlugs = cocktail.matches.good || [];
const otherMatchSlugs = cocktail.matches.other || [];

const goodCocktails = await Database.collection('cocktails')
.find({ slug: { $in: Array.from(goodMatchSlugs) } })
.sort({ visitCount: -1 })
.limit(20)
.project({ _id: 0, id: 1, slug: 1, name: 1, ratingCount: 1, ratingValue: 1, visitCount: 1 })
.toArray();

const otherCocktails = await Database.collection('cocktails')
.find({ slug: { $in: Array.from(otherMatchSlugs) } })
.sort({ visitCount: -1 })
.limit(20)
.project({ _id: 0, id: 1, slug: 1, name: 1, ratingCount: 1, ratingValue: 1, visitCount: 1 })
.toArray();

const cocktails = goodCocktails.concat(otherCocktails).slice(0, 20);

cocktails.forEach(cocktail => {
cocktail.images = buildImages(cocktail.id, 'COCKTAIL');
async function getRecommendations(cocktail) {
const maxRecommendations = 20;
const classCocktailSlugs = [
cocktail.matchesV2.class1 || [],
cocktail.matchesV2.class2 || [],
cocktail.matchesV2.class3 || [],
cocktail.matchesV2.class4 || [],
cocktail.matchesV2.class5 || [],
cocktail.matchesV2.class6 || []
];

const recommendationCocktails = [];

for (const slugs of classCocktailSlugs) {
if (recommendationCocktails.length >= maxRecommendations) break;

const countToFull = maxRecommendations - recommendationCocktails.length;

const classCocktails = await Database.collection('cocktails')
.find({ slug: { $in: slugs } })
.sort({ visitCount: -1 })
.limit(countToFull)
.project({ _id: 0, id: 1, slug: 1, name: 1, ratingCount: 1, ratingValue: 1, visitCount: 1 })
.toArray();

recommendationCocktails.push(...classCocktails);
}

cocktail.rating = cocktail.ratingCount ? cocktail.ratingValue / cocktail.ratingCount : 0;
if (cocktail.rating === 0) {
cocktail.rating = null;
}
recommendationCocktails.forEach(cocktail => {
cocktail.images = buildImages(cocktail.id, 'COCKTAIL');
cocktail.rating = cocktail.ratingCount ? cocktail.ratingValue / cocktail.ratingCount : null;
delete cocktail.ratingCount;
delete cocktail.ratingValue;
});

return cocktails;
return recommendationCocktails;
}

async function getFullCocktailBySlug(slug) {
Expand Down Expand Up @@ -132,7 +136,7 @@ async function getFullCocktailBySlug(slug) {
tools: glassware.concat(tools),
tags: taste.concat(tags).concat(alcohols).concat([alcoholVolumeTag]),
article: article,
recomendationCocktails: await getRecomendations(cocktail),
recomendationCocktails: await getRecommendations(cocktail),
};
} catch (error) {
console.error(error);
Expand Down

0 comments on commit 343de66

Please sign in to comment.