Skip to content

Commit

Permalink
Merge pull request #16 from Swifty-The-Delivery-Ecosystem/recommendat…
Browse files Browse the repository at this point in the history
…ion_modif

updated the api endpoint
  • Loading branch information
adismort14 authored Apr 9, 2024
2 parents 6911813 + 8a08976 commit bb5ef09
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions controllers/userOrderControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bb5ef09

Please sign in to comment.