From 8a08976c6962bc8e739b5dac6f564e071ae9b584 Mon Sep 17 00:00:00 2001 From: Aditya Sankhla Date: Wed, 10 Apr 2024 01:26:53 +0530 Subject: [PATCH] updated the api endpoint --- controllers/userOrderControl.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/controllers/userOrderControl.js b/controllers/userOrderControl.js index 6328ae4..55e1bdc 100644 --- a/controllers/userOrderControl.js +++ b/controllers/userOrderControl.js @@ -100,36 +100,40 @@ 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 2 orders - + .limit(3); // Get the last 2 orders + + let queryItemList=[] let recommendedItemsSet = new Set(); + // Fetch recommendations for default items const defaultItems = ["Samosa", "Pav Bhaji"]; - const recommendedRecipes = await fetchRecommendations(defaultItems); - for (let recipe of recommendedRecipes) { - const dbItem = await MenuItem.findOne({ name: recipe }); - if (dbItem) { - recommendedItemsSet.add(JSON.stringify(dbItem)); - } + for (let itemName of defaultItems) { + let foodItemTitleCase = toTitleCase(itemName); + queryItemList.push(foodItemTitleCase); } // Process orders for additional recommendations for (let order of orders) { const item = order.items[0]; // Assuming you want the first item of each order const itemName = item.name; - const recommendedRecipes = await fetchRecommendations([toTitleCase(itemName)]); + let foodItemTitleCase = toTitleCase(itemName); // Convert the food item name to Title Case + queryItemList.push(foodItemTitleCase); + } + + try { + const recommendedRecipes = fetchRecommendations(foodItemTitleCase) + for (let recipe of recommendedRecipes) { const dbItem = await MenuItem.findOne({ name: recipe }); if (dbItem) { - recommendedItemsSet.add(JSON.stringify(dbItem)); + recommendedItemsSet.add(JSON.stringify(dbItem)); } } + } 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);