34 lines
937 B
JavaScript
34 lines
937 B
JavaScript
|
|
export function mealToShoppingListSources(meal) {
|
||
|
|
const sources = [];
|
||
|
|
for (const recipe of meal.recipes) {
|
||
|
|
for (const ingredient of recipe.ingredients) {
|
||
|
|
sources.push({ ingredient, meal, recipe, });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for (const ingredient of meal.extra_ingredients) {
|
||
|
|
sources.push({ ingredient, meal, });
|
||
|
|
}
|
||
|
|
|
||
|
|
return sources;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function removeMealFromShoppingListSources(sources, meal) {
|
||
|
|
return sources.filter(source => source.meal.id !== meal.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function groupByProduct(sources) {
|
||
|
|
const grouped = {};
|
||
|
|
for (const source of sources) {
|
||
|
|
if (!grouped[source.ingredient.product.id]) {
|
||
|
|
grouped[source.ingredient.product.id] = {
|
||
|
|
product: source.ingredient.product,
|
||
|
|
sources: [],
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
grouped[source.ingredient.product.id].sources.push(source);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Object.values(grouped);
|
||
|
|
}
|