diff --git a/database/cocktail.js b/database/cocktail.js index c2d8346..98057e0 100644 --- a/database/cocktail.js +++ b/database/cocktail.js @@ -62,7 +62,7 @@ const CocktailSchema = new Schema({ const Cocktail = mongoose.model('Cocktail', CocktailSchema); async function getCocktailBySlug(slug) { - return Cocktail.findOne({slug}).lean(); + return Cocktail.findOne({ slug }).lean(); } module.exports = { diff --git a/features/cocktail/utils.js b/features/cocktail/utils.js index 5f2b4ac..40bfa70 100644 --- a/features/cocktail/utils.js +++ b/features/cocktail/utils.js @@ -1,6 +1,41 @@ 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'); + + cocktail.rating = cocktail.ratingCount ? cocktail.ratingValue / cocktail.ratingCount : 0; + if (cocktail.rating === 0) { + cocktail.rating = null; + } + delete cocktail.ratingCount; + delete cocktail.ratingValue; + }); + + return cocktails; +} + async function getFullCocktailBySlug(slug) { try { const cocktail = await getCocktailBySlug(slug); @@ -97,6 +132,7 @@ async function getFullCocktailBySlug(slug) { tools: glassware.concat(tools), tags: taste.concat(tags).concat(alcohols).concat([alcoholVolumeTag]), article: article, + recomendationCocktails: await getRecomendations(cocktail), }; } catch (error) { console.error(error); diff --git a/features/filters/rest.js b/features/filters/rest.js index 5dfa2db..57256ad 100644 --- a/features/filters/rest.js +++ b/features/filters/rest.js @@ -41,7 +41,7 @@ const filterDataCache = {}; router.get('/api/filters', async (req, res) => { if (filterDataCache.data) { - // return res.status(200).send(filterDataCache.data); + return res.status(200).send(filterDataCache.data); } const response = await getFiltersData(); filterDataCache.data = response;