Skip to content

Commit

Permalink
limit & filter recomends
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Aug 23, 2024
1 parent 4875373 commit fb4d29e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion models/Dish.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ let Model = {
}
return updatedDishes;
},
getRecommended: async function (ids, limit = 12, includeReverse = true) {
getRecommended: async function (ids, limit = 12, includeReverse = false) {
if (!Array.isArray(ids) || ids.length === 0) {
throw new Error('You must provide an array of IDs.');
}
Expand Down Expand Up @@ -402,6 +402,7 @@ let Model = {
});
}
recommendedDishes = [...new Set(recommendedDishes.map((dish) => dish.id))].map(id => recommendedDishes.find((dish) => dish.id === id));
recommendedDishes = recommendedDishes.filter((dish) => !ids.includes(dish.id));
// Fisher-Yates shifle
recommendedDishes = recommendedDishes.sort(() => Math.random() - 0.5);
if (limit && Number.isInteger(limit) && limit > 0) {
Expand Down
6 changes: 4 additions & 2 deletions models/Dish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ let Model = {
},


getRecommended: async function(ids: string[], limit = 12, includeReverse = true): Promise<Dish[]> {
getRecommended: async function(ids: string[], limit = 12, includeReverse = false): Promise<Dish[]> {
if (!Array.isArray(ids) || ids.length === 0) {
throw new Error('You must provide an array of IDs.');
}
Expand All @@ -490,7 +490,7 @@ let Model = {
});


let recommendedDishes = dishes.reduce((acc: Dish[], dish:Dish) => {
let recommendedDishes: Dish[] = dishes.reduce((acc: Dish[], dish:Dish) => {
return acc.concat(dish.recommendations);
}, []);

Expand All @@ -505,6 +505,8 @@ let Model = {
recommendedDishes.find((dish: Dish) => dish.id === id)
);

recommendedDishes = recommendedDishes.filter((dish: Dish) => !ids.includes(dish.id));

// Fisher-Yates shifle
recommendedDishes = recommendedDishes.sort(() => Math.random() - 0.5);

Expand Down

0 comments on commit fb4d29e

Please sign in to comment.