Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated the api endpoint #16

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading