fix: enrich ingredient names in shopping list API

Same root cause as meal detail: recipe JSONB stores ingredient_id but
not name. Shopping list now looks up names from the Ingredient table
before aggregating quantities, so items show "3 cups onion" instead
of "Unknown".
This commit is contained in:
2026-05-15 14:21:51 -07:00
parent 2708cc4bdd
commit 2f6ab006de
+11 -2
View File
@@ -71,14 +71,23 @@ def get_shopping_list(db: Session = Depends(get_db)):
aggregated = defaultdict(lambda: {"quantity": 0.0, "unit": None, "name": ""}) aggregated = defaultdict(lambda: {"quantity": 0.0, "unit": None, "name": ""})
# Build ingredient name lookup from IDs
ingredient_names = {}
if all_ingredient_ids:
ingredient_names = {str(i.id): i.name for i in ingredients}
for plan_item in current_plan.items: for plan_item in current_plan.items:
recipe = db.query(Recipe).filter(Recipe.id == plan_item.recipe_id).first() recipe = db.query(Recipe).filter(Recipe.id == plan_item.recipe_id).first()
if recipe and recipe.ingredients: if recipe and recipe.ingredients:
for ing in recipe.ingredients: for ing in recipe.ingredients:
name = ing.get('name', 'Unknown') ing_id = ing.get('ingredient_id')
name = ing.get('name')
if not name and ing_id:
name = ingredient_names.get(str(ing_id))
if not name:
name = 'Unknown'
quantity = ing.get('quantity', 1.0) or 1.0 quantity = ing.get('quantity', 1.0) or 1.0
unit = ing.get('unit') unit = ing.get('unit')
ing_id = ing.get('ingredient_id')
key = name.lower() key = name.lower()
aggregated[key]["quantity"] += quantity aggregated[key]["quantity"] += quantity