From 71fdfaa00fb4034e2df0077d9dd0e5def4845c32 Mon Sep 17 00:00:00 2001 From: vovastelmashchuk Date: Sun, 15 Sep 2024 20:36:56 +0300 Subject: [PATCH] add recomendation cocktalis --- database/cocktail.js | 2 +- features/cocktail/utils.js | 36 ++++++++++++++++++++++++++++++++++++ features/filters/rest.js | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) 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;