From c894f72973c53c1f63276e1bf7210a1b3624b5a2 Mon Sep 17 00:00:00 2001 From: Aditya062003 Date: Wed, 10 Apr 2024 03:20:06 +0530 Subject: [PATCH] fix: recommendation --- controllers/userOrderControl.js | 56 +++++++++++++-------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/controllers/userOrderControl.js b/controllers/userOrderControl.js index 51aaa69..55e1bdc 100644 --- a/controllers/userOrderControl.js +++ b/controllers/userOrderControl.js @@ -100,30 +100,16 @@ exports.getRecommendation = async (req, res, next) => { const userID = req.params.user_id; let orders = await Order.find({ user_id: userID }) .sort({ createdAt: -1 }) - .limit(2); // Get the last 5 orders + .limit(3); // Get the last 2 orders + + let queryItemList=[] let recommendedItemsSet = new Set(); // Fetch recommendations for default items const defaultItems = ["Samosa", "Pav Bhaji"]; for (let itemName of defaultItems) { let foodItemTitleCase = toTitleCase(itemName); - try { - const response = await axios.get( - `https://food-recommendation-yqpc.onrender.com/recommend/${foodItemTitleCase}` - ); - console.log(response); - const recommendedRecipes = response.data.recommended_recipes; - - // Check if recommended items exist in the database - for (let recipe of recommendedRecipes) { - const dbItem = await MenuItem.findOne({ name: recipe }); - if (dbItem) { - recommendedItemsSet.add(JSON.stringify(dbItem)); - } - } - } catch (e) { - console.log(e); - } + queryItemList.push(foodItemTitleCase); } // Process orders for additional recommendations @@ -131,34 +117,34 @@ exports.getRecommendation = async (req, res, next) => { const item = order.items[0]; // Assuming you want the first item of each order const itemName = item.name; let foodItemTitleCase = toTitleCase(itemName); // Convert the food item name to Title Case + queryItemList.push(foodItemTitleCase); + } - // Make API call to get recommendations - try { - const response = await axios.get( - `https://food-recommendation-yqpc.onrender.com/recommend/${foodItemTitleCase}` - ); - const recommendedRecipes = response.data.recommended_recipes; - - // Check if recommended items exist in the database - for (let recipe of recommendedRecipes) { - const dbItem = await MenuItem.findOne({ name: recipe }); - if (dbItem) { - recommendedItemsSet.add(JSON.stringify(dbItem)); - } + try { + const recommendedRecipes = fetchRecommendations(foodItemTitleCase) + + for (let recipe of recommendedRecipes) { + const dbItem = await MenuItem.findOne({ name: recipe }); + if (dbItem) { + recommendedItemsSet.add(JSON.stringify(dbItem)); } - } catch (e) { - console.log(e); } + } catch (e) { + console.log(e); } - const recommendedItems = Array.from(recommendedItemsSet).map(item => JSON.parse(item)); - + const recommendedItems = Array.from(recommendedItemsSet).map(item => JSON.parse(item)); res.json({ recommendedItems }); } catch (error) { next(error); } }; +async function fetchRecommendations(foodItems) { + const response = await axios.post("https://food-recommendation-yqpc.onrender.com/recommend", foodItems); + return response.data.recommended_food_items; +} + function toTitleCase(str) { return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();