Skip to content

Commit

Permalink
feat: addeed recommendations v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya062003 committed Nov 12, 2024
1 parent cdc6b8b commit eb012c5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 52 deletions.
83 changes: 31 additions & 52 deletions controllers/userOrderControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,64 +162,42 @@ exports.getRecommendation = async (req, res, next) => {
exports.getRecommendationv2 = async (req, res, next) => {
try {
const userID = req.params.user_id;
// Fetch the last 5 orders of the user
let orders = await Order.find({ user_id: userID })
.sort({ createdAt: -1 })
.limit(5);
let recommendedItemsSet = new Set();


const defaultItems = ["Samosa", "Pav Bhaji"];
const defaultRecommendationsPromises = defaultItems.map(async (itemName) => {
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;


for (let recipe of recommendedRecipes) {
const dbItem = await MenuItem.findOne({ name: recipe });
if (dbItem) {
recommendedItemsSet.add(JSON.stringify(dbItem));
}
}
} catch (e) {
console.log(e);
}
});
.limit(5);


const ordersRecommendationsPromises = orders.map(async (order) => {
const item = order.items[0];
const itemName = item.name;
let foodItemTitleCase = toTitleCase(itemName);


try {
const response = await axios.get(
`https://food-recommendation-yqpc.onrender.com/recommend/${foodItemTitleCase}`
);
const recommendedRecipes = response.data.recommended_recipes;
// Use a set to collect recommended items without duplicates
let recommendedItemsSet = new Set();

for (let recipe of recommendedRecipes) {
const dbItem = await MenuItem.findOne({ name: recipe });
if (dbItem) {
recommendedItemsSet.add(JSON.stringify(dbItem));
}
// Extract item names from recent orders (up to 3 items)
const recentItemNames = orders
.flatMap(order => order.items.map(item => item.name))
.slice(0, 3);
// If we have less than 3 items, add default items to meet the minimum
const foodItems = recentItemNames.length >= 3 ? recentItemNames : recentItemNames.concat(["Samosa", "Pav Bhaji"]).slice(0, 3);

// Send the list of food items to the /v2/recommend endpoint
try {
const response = await axios.post(
'https://food-recommendation-yqpc.onrender.com/v2/recommend',
foodItems
);

const recommendedRecipes = response.data.recommended_food_items;

// Fetch each recommended item from the database if it exists
for (let recipe of recommendedRecipes) {
const dbItem = await MenuItem.findOne({ name: recipe });
if (dbItem) {
recommendedItemsSet.add(JSON.stringify(dbItem));
}
} catch (e) {
console.log(e);
}
});


await Promise.all(ordersRecommendationsPromises);


await Promise.all(defaultRecommendationsPromises);
} catch (error) {
console.error("Error fetching recommendations:", error);
}

// Convert the set of items to an array and parse JSON objects
const recommendedItems = Array.from(recommendedItemsSet).map((item) =>
JSON.parse(item)
);
Expand All @@ -230,6 +208,7 @@ exports.getRecommendationv2 = async (req, res, next) => {
}
};

// Helper function to convert strings to title case
function toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
Expand All @@ -243,4 +222,4 @@ exports.getOrderbyId = async (req, res, next) => {
} catch (error) {
next(error);
}
};
};
3 changes: 3 additions & 0 deletions routes/orderRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
getOrderbyId,
listenForNewOrders,
getRecommendation,
getRecommendationv2
} = require("../controllers/userOrderControl");

const {
Expand Down Expand Up @@ -47,6 +48,8 @@ router.get("/user/recommend/:user_id", checkAuth, getRecommendation);

router.get("/user/recommendv2/:user_id", checkAuth, getRecommendation);

router.post("/user/recommend/v2/:user_id" , getRecommendationv2)

router.get(
"/delivery_boy/orders/:delivery_boy_id",
checkDeliveryPartner,
Expand Down

0 comments on commit eb012c5

Please sign in to comment.