Skip to content

Commit

Permalink
fix: food recommend
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya062003 committed Apr 9, 2024
1 parent c56cfee commit 8431d21
Showing 1 changed file with 47 additions and 52 deletions.
99 changes: 47 additions & 52 deletions controllers/userOrderControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,65 +99,60 @@ 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);
.limit(2); // Get the last 5 orders
let recommendedItemsSet = new Set();

// Fetch recommendations for default items
const defaultItems = ["Samosa", "Pav Bhaji"];
const recommendedItemsSet = new Set();

// Fetch recommendations for default items in parallel
await Promise.all(
defaultItems.map(async (itemName) => {
try {
const foodItemTitleCase = toTitleCase(itemName);
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
const dbItems = await MenuItem.find({
name: { $in: recommendedRecipes },
});
dbItems.forEach((item) =>
recommendedItemsSet.add(JSON.stringify(item))
);
} catch (e) {
console.error("Error fetching recommendations for default item:", e);
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);
}
}

// Fetch recommendations for items in the recent orders
await Promise.all(
orders.map(async (order) => {
const itemName = order.items[0].name;
const foodItemTitleCase = toTitleCase(itemName);

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
const dbItems = await MenuItem.find({
name: { $in: recommendedRecipes },
});
dbItems.forEach((item) =>
recommendedItemsSet.add(JSON.stringify(item))
);
} catch (e) {
console.error(
"Error fetching recommendations for recent order item:",
e
);
// 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;
let foodItemTitleCase = toTitleCase(itemName); // Convert the food item name to Title Case

// 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));
}
}
})
);

} catch (e) {
console.log(e);
}
}
const recommendedItems = Array.from(recommendedItemsSet).map((item) =>
JSON.parse(item)
);

res.json({ recommendedItems });
} catch (error) {
next(error);
Expand All @@ -177,4 +172,4 @@ exports.getOrderbyId = async (req, res, next) => {
} catch (error) {
next(error);
}
};
};

0 comments on commit 8431d21

Please sign in to comment.