Skip to content

Commit

Permalink
fix: recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya062003 committed Apr 9, 2024
1 parent 333b4b9 commit c894f72
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions controllers/userOrderControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,65 +100,51 @@ 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
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
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();
Expand Down

0 comments on commit c894f72

Please sign in to comment.