From 8b1e1da5d8a2be4ef98a248085ed84355c60342d Mon Sep 17 00:00:00 2001 From: Lars Date: Mon, 14 Sep 2026 12:47:12 +0200 Subject: [PATCH] feat: Eigene und zentrale Lebensmittel bearbeiten, mit Sichtbarkeit im Mapping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nutzer können persönliche Foods anlegen und ändern; der Admin legt zentrale Einträge für alle an. Suche und Namens-Mapping berücksichtigen persönlich, zentral und BLS. Co-authored-by: Cursor --- .claude/docs/functional/BLS_FOOD_REFERENCE.md | 9 +- .claude/docs/technical/BLS_FOOD_REFERENCE.md | 7 +- CLAUDE.md | 2 +- backend/data_layer/food_attributes.py | 51 ++++++ backend/data_layer/food_mapping.py | 19 +- backend/data_layer/food_suggest.py | 14 +- backend/data_layer/food_visibility.py | 105 +++++++++++ backend/data_layer/nutrition_items.py | 42 +++++ .../069_food_catalog_visibility.sql | 18 ++ backend/routers/admin_bls.py | 70 +++++++- backend/routers/admin_food_mappings.py | 25 ++- backend/routers/bls.py | 167 +++++++++++++++--- backend/tests/test_food_attributes.py | 39 ++++ backend/tests/test_food_suggest.py | 8 + backend/tests/test_food_visibility.py | 90 ++++++++++ backend/version.py | 12 +- docs/issues/issue-bls-food-mapping.md | 1 + frontend/src/app.css | 2 +- frontend/src/components/FoodEditDialog.jsx | 146 +++++++++++++++ .../src/components/FoodNutrientFields.jsx | 16 +- frontend/src/components/FoodRecipeDialog.jsx | 43 +++-- frontend/src/components/FoodSearchModal.jsx | 58 ++++-- frontend/src/components/MyFoodsPanel.jsx | 123 +++++++++++++ frontend/src/components/NutritionFoodMap.jsx | 36 +++- frontend/src/config/adminNav.js | 2 +- frontend/src/pages/AdminBlsFoodsPage.jsx | 147 +++++++++++++-- frontend/src/pages/AdminFoodMappingsPage.jsx | 16 +- frontend/src/pages/NutritionPage.jsx | 15 +- frontend/src/utils/api.js | 9 +- frontend/src/utils/foodVisibility.js | 23 +++ 30 files changed, 1195 insertions(+), 120 deletions(-) create mode 100644 backend/data_layer/food_visibility.py create mode 100644 backend/migrations/069_food_catalog_visibility.sql create mode 100644 backend/tests/test_food_visibility.py create mode 100644 frontend/src/components/FoodEditDialog.jsx create mode 100644 frontend/src/components/MyFoodsPanel.jsx create mode 100644 frontend/src/utils/foodVisibility.js diff --git a/.claude/docs/functional/BLS_FOOD_REFERENCE.md b/.claude/docs/functional/BLS_FOOD_REFERENCE.md index 9f71272..86dc3f0 100644 --- a/.claude/docs/functional/BLS_FOOD_REFERENCE.md +++ b/.claude/docs/functional/BLS_FOOD_REFERENCE.md @@ -1,6 +1,6 @@ # BLS-Lebensmittelreferenz und FDDB-Mapping -**Stand:** 2026-09-12 · **Status:** Phase 1 (Umsetzung) +**Stand:** 2026-09-14 · **Status:** Phase 1 (Umsetzung) ## WAS @@ -38,14 +38,17 @@ Gelernte Zuordnungen und Listen lassen sich als **JSON sichern** und auf einer a - BLS 4.0 (~7140 Lebensmittel) ist frei nutzbar (MRI / blsdb.de); Dateien nicht im Git. - Mapping ohne KI: Normalisierung + exakter Lookup (User vor Global) + Bestätigung neuer Namen. - Öl/Supplement mit Etikett (EPA/DHA, Vitamine) ist **ein Katalogeintrag**, keine Zutatenliste. Listen sind nur Rohmischungen aus Lebensmitteln — eine Liste kann keine andere Liste enthalten. Quarkspeise = Liste, Omega-3-Öl = Lebensmittel darin. -- Gelernte Zuordnungen bleiben dauerhaft, sind aber änder- und löschbar. +- Gelernte Zuordnungen bleiben dauerhaft, sind aber änder- und löschbar. Nutzer-Mapping vor globalem Mapping. Globales Mapping nur auf BLS oder zentrale Admin-Foods. +- Sichtbarkeit: persönlich (nur Owner) / zentral (alle) / BLS (alle). Vorschläge und Suche filtern danach; bei gleichem Namen gewinnt persönlich vor zentral vor BLS. - Ungemappte / Fertiggerichte: FDDB-Makros, keine erfundenen Mikros. - Fasten und „unvollständig“ sind explizite Marken, kein Auto-Schluss aus fehlendem Import. - Import-Policy (Profil): nachfragen / Katalog überschreiben / FDDB überschreiben / Makros behalten. ## Manuelle Lebensmittel / Supplemente -Eigene Einträge (z. B. Norsan Omega-3 + EPA) speichern **dieselben Stoffwerte** wie BLS-Lebensmittel (`food_attribute_values`, immer pro 100 g). Beim Anlegen: Makros plus Suche nach weiteren Stoffen (EPA, DHA, Omega-3). Etikett „pro 8 ml“ → Portionsgröße in g angeben, Umrechnung auf 100 g erfolgt serverseitig. Einheit des Stoffs beachten (oft **g**, Etikett in mg: 1100 mg = 1,1 g). Fehlt ein Stoff im Katalog: Admin → Stoffe & Attribute. +Eigene Einträge des Nutzers sind **persönlich** (`manual_user`). Der Admin legt **zentrale** Einträge für alle an (`manual_admin`). Beide speichern **dieselben Stoffwerte** wie BLS-Lebensmittel (`food_attribute_values`, immer pro 100 g). Beim Anlegen: Makros plus Suche nach weiteren Stoffen (EPA, DHA, Omega-3). Etikett „pro 8 ml“ → Portionsgröße in g angeben, Umrechnung auf 100 g erfolgt serverseitig. Einheit des Stoffs beachten (oft **g**, Etikett in mg: 1100 mg = 1,1 g). Fehlt ein Stoff im Katalog: Nutzer kann ihn selbst anlegen oder Admin → Stoffe & Attribute. + +**Nachträglich ändern:** Unter Ernährung → Tab **Lebensmittel** (persönliche Liste + zentrale Admin-Einträge, Suche). Persönliche Einträge anlegen/bearbeiten; zentrale nur lesen. Beim Zuordnen gilt die Reihenfolge persönlich → zentral → BLS. Globale Namens-Mappings (Admin) gelten für alle, dürfen aber nicht auf ein privates Nutzer-Lebensmittel zeigen. Offizielle BLS-Einträge bleiben fest. Beim Speichern gelten die Felder als Werte je 100 g, außer die Portionsgröße wird bewusst auf die Etikett-Portion gesetzt. Betroffene Tagebuch-Tage werden im Hintergrund neu berechnet. Admin → Lebensmittelkatalog → Reiter **Manuell** → Zentral anlegen. Weitere Quellen (USDA, Schweizer Nährwertdatenbank) kommen später als zusätzliche Katalogherkunft, nicht als zweite Wertetabelle. diff --git a/.claude/docs/technical/BLS_FOOD_REFERENCE.md b/.claude/docs/technical/BLS_FOOD_REFERENCE.md index e235080..840acbe 100644 --- a/.claude/docs/technical/BLS_FOOD_REFERENCE.md +++ b/.claude/docs/technical/BLS_FOOD_REFERENCE.md @@ -1,6 +1,6 @@ # BLS Food Reference – technische Spec -**Stand:** 2026-09-12 · Migration **062**–**068** +**Stand:** 2026-09-14 · Migration **062**–**069** ## Tabellen @@ -29,6 +29,9 @@ FDDB: Items persistieren; `nutrition_log` nur bei leerem Tag oder laut Policy / ## Router - `/api/bls/*` — Suche, Attribute-Suche, eigene Foods (`attributes` + `serving_g`), eigene Mappings; `POST /bls/attributes` legt Extension-Stoff an (Name + Einheit), `GET /bls/attributes?keys=EPA,DHA` +- `GET /bls/foods/mine` — eigene `manual_user`-Einträge; `GET /bls/foods/shared` — zentrale `manual_admin`; `GET /bls/foods/{id}` — Editor-Payload inkl. `visibility`; `PUT /bls/foods/manual/{id}` — nur Owner. Suche `?scope=global` blendet persönliche Foods aus (Admin-Mappings). +- Sichtbarkeit: `data_layer/food_visibility.py`. Lookup-Mapping: User-Zeile nur wenn Food sichtbar; globale Mapping-Zeile nur `owner_profile_id IS NULL`. `upsert_food_mapping` erzwingt dasselbe. Vorschläge sortieren persönlich → zentral → BLS. +- `GET /admin/bls/foods?kind=manual` — `manual_user`/`manual_admin`. `POST/PUT /admin/bls/foods/manual` — zentrale Foods (`attributes` + `serving_g`). Migration **069**: Owner-Check (persönlich hat Owner, BLS/Admin nicht). - Migration **065** — Extension-Keys EPA/DHA/DPA/ALA/OMEGA3/OMEGA6 falls BLS sie nicht unter diesem Key hat - `/api/admin/bls/*` — Import, Katalog, Attribute - `/api/admin/food-mappings` — Admin-CRUD @@ -36,7 +39,7 @@ FDDB: Items persistieren; `nutrition_log` nur bei leerem Tag oder laut Policy / - `GET/POST /api/nutrition/recipes`, `PUT/DELETE /api/nutrition/recipes/{id}`, `POST …/import-fddb-lists`, `POST …/{id}/apply` - `PUT /api/admin/food-mappings/{id}` — Ziel-Lebensmittel einer bestehenden Zuordnung wechseln - Unmapped = Tagebuchzeilen ohne `food_id`/`recipe_id` **plus** Listenzutaten ohne Mapping -- Frontend: Inline-Vorschläge auf Zuordnen (`food_suggest.py`: Collapse-Key ohne Leerzeichen, Index 5 Min. Cache, Fett-%-Zahlen 9,5/10), `FoodSearchModal` nur noch Zusatzsuche (Abort + Debounce). Listen-UI: `FoodRecipeDialog` (Suche gelernt + Katalog; Katalogpick upsertet Mapping). Offene Zutaten: `kind=recipe_ingredient` (API-Wert unverändert, UI: Listenzutat). +- Frontend: Inline-Vorschläge auf Zuordnen (`food_suggest.py`: Collapse-Key ohne Leerzeichen, Index 5 Min. Cache, Fett-%-Zahlen 9,5/10), `FoodSearchModal` nur noch Zusatzsuche (Abort + Debounce). Eigene Foods: Tab **Lebensmittel** (`MyFoodsPanel` + `FoodEditDialog`). Listen-UI: `FoodRecipeDialog` (Suche gelernt + Katalog; Katalogpick upsertet Mapping). Offene Zutaten: `kind=recipe_ingredient` (API-Wert unverändert, UI: Listenzutat). - Tandoor (Modul `tandoor`): Migration **067** `profile_tandoor_settings`; `GET/PUT /api/tandoor/settings` (Token nur `token_set` / `token_hint`); `POST /api/tandoor/test` (10/min, kein Redirect-Follow). Client: `data_layer/tandoor_client.py`. Import und `cooked_yield_g` später. - Migration **066** — `food_recipe_ingredients.quantity_amount` + `source_unit`; `quantity_g` = Umrechnung (`resolve_quantity` in `food_mapping.py`). `GET /bls/units` = Einheitenkatalog (EL/TL/Prise/Stück, default_g). - `GET /nutrition/unmapped?since_days=28` — nur Namen mit `last_date` im Fenster; `count_only` liefert `{count, total, since_days}`; `meta=true` liefert `{items, count, total, since_days}`. Listenzutaten-Datum: letzte `recipe_id`-Nutzung oder `food_recipes.updated_at`/`created_at` — kein Join über den Listennamen auf alle Tagebuchzeilen. Migration **068**: Partial-Index unmapped-open + `(profile_id, recipe_id, date)`. diff --git a/CLAUDE.md b/CLAUDE.md index 9c51420..5d89dad 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -125,7 +125,7 @@ frontend/src/ - **Zuordnen-Performance:** Katalog-Index im Prozess (5 Min.), Vorschläge nur für sichtbare Zeilen (`POST /bls/foods/suggest-batch`), Suche mit Abort; nach Bestätigen kein Reload der ganzen Ernährungseite. Unmapped ohne Kreuzjoin Liste×Tagebuch; Listenzutaten-Datum aus `recipe.updated_at` oder letzter `recipe_id`-Nutzung. Listen-API `?brief=true`; FDDB-Listenimport rechnet Tageswerte im Hintergrund. Migration **068**. - **Zuordnen-Zeitraum:** Standard letzte 4 Wochen (`since_days`); ältere ungemappte Namen (z. B. Getreide nach Glutenverzicht) bleiben unter „Alle“. - **Katalogsuche:** Fettgehalt mitsuchen (`Joghurt 10%` / `9,5`); Dezimal-Komma bleibt erhalten. -- **Manuelle Foods:** Stoffe über EAV (`GET /bls/attributes`, `attributes` + `serving_g` beim Anlegen). Supplemente wie Norsan: EPA/DHA aus Etikett, Portionsgramm → Speicherung /100 g. Nutzer kann fehlende Stoffe anlegen (`POST /bls/attributes`). Öl/Supplement = ein Lebensmittel, keine Liste (Listen nicht verschachtelbar). +- **Manuelle Foods:** Persönlich (`manual_user` + Owner, nur dieser Nutzer) oder zentral (`manual_admin`, alle Nutzer). Stoffe über EAV. Nutzer: Tab Ernährung → **Lebensmittel**. Admin: Katalog → Manuell → **Zentral anlegen**. Mapping: Nutzer-Mapping darf sichtbare Foods; globales Mapping nur BLS/zentral. Offizielle BLS-Zeilen nicht editierbar. Öl/Supplement = ein Lebensmittel, keine Liste. - **Listen / Kombinationen (Mitai):** Tab **Listen** (anlegen, bearbeiten, FDDB-Import). Im Listendialog Zutaten direkt BLS-zuordnen (Status offen/zugeordnet); Suche zuerst Listen-Namen und eigene Foods, dann BLS. **Tandoor:** User-Einstellungen URL + Token, Verbindungstest (`/api/tandoor/*`). Gerichte bleiben in Tandoor; später Mapping + `cooked_yield_g`. Admin-Mappings: `PUT /admin/food-mappings/{id}`. - **Gitea #106:** BLS-Stammdaten, FDDB-Mapping, Item-Tagebuch — http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/106 - **Doku:** `.claude/docs/functional/BLS_FOOD_REFERENCE.md`, `.claude/docs/technical/BLS_FOOD_REFERENCE.md`, `docs/issues/issue-bls-food-mapping.md`. Folge #75. diff --git a/backend/data_layer/food_attributes.py b/backend/data_layer/food_attributes.py index ad3969c..441fe59 100644 --- a/backend/data_layer/food_attributes.py +++ b/backend/data_layer/food_attributes.py @@ -108,6 +108,57 @@ def write_numeric_attributes(cur, food_id: str, values: dict[str, Any] | None) - return written +def replace_numeric_attributes(cur, food_id: str, values: dict[str, Any] | None) -> int: + cur.execute("DELETE FROM food_attribute_values WHERE food_id = %s", (food_id,)) + return write_numeric_attributes(cur, food_id, values) + + +KEY_TO_MACRO = {v: k for k, v in MACRO_TO_KEY.items()} + + +def editor_payload_for_food(cur, food: dict[str, Any], *, editable: bool) -> dict[str, Any]: + cur.execute( + """ + SELECT a.attr_key, a.name_de, a.name_en, a.unit, a.category, a.origin, v.value_num + FROM food_attribute_values v + JOIN food_attributes a ON a.id = v.attribute_id + WHERE v.food_id = %s AND a.data_type = 'num_per_100g' + AND v.value_num IS NOT NULL AND v.is_trace = false + ORDER BY a.sort_order, a.attr_key + """, + (food["id"],), + ) + macros = {"kcal": 0, "protein_g": 0, "fat_g": 0, "carbs_g": 0} + extras = [] + for row in cur.fetchall(): + rec = dict(row) + field = KEY_TO_MACRO.get(rec["attr_key"]) + if field: + macros[field] = float(rec["value_num"]) + else: + extras.append({ + "attr": { + "attr_key": rec["attr_key"], + "name_de": rec["name_de"], + "name_en": rec["name_en"], + "unit": rec["unit"], + "category": rec["category"], + "origin": rec["origin"], + }, + "value": float(rec["value_num"]), + }) + return { + "id": str(food["id"]), + "name_de": food.get("name_de") or "", + "name_en": food.get("name_en"), + "catalog_kind": food.get("catalog_kind"), + "editable": editable, + "macros_per_100g": macros, + "attributes": extras, + "serving_g": 100, + } + + def macros_and_attributes_to_values( macros: dict[str, Any] | None, attributes: dict[str, Any] | None, diff --git a/backend/data_layer/food_mapping.py b/backend/data_layer/food_mapping.py index 2bede24..97ec9ad 100644 --- a/backend/data_layer/food_mapping.py +++ b/backend/data_layer/food_mapping.py @@ -408,14 +408,16 @@ def get_food_mapping_with_cursor( """ SELECT m.id AS mapping_id, m.food_id, m.profile_id, m.source, m.grams_per_unit, m.source_unit, - f.bls_code, f.name_de, f.catalog_kind + f.bls_code, f.name_de, f.catalog_kind, f.owner_profile_id FROM food_name_mappings m JOIN food_catalog f ON f.id = m.food_id WHERE m.source_system = %s AND m.source_name_normalized = %s AND m.profile_id = %s + AND f.is_active = true + AND (f.owner_profile_id IS NULL OR f.owner_profile_id = %s) LIMIT 1 """, - (source_system, norm, profile_id), + (source_system, norm, profile_id, profile_id), ) row = cur.fetchone() if row: @@ -424,11 +426,13 @@ def get_food_mapping_with_cursor( """ SELECT m.id AS mapping_id, m.food_id, m.profile_id, m.source, m.grams_per_unit, m.source_unit, - f.bls_code, f.name_de, f.catalog_kind + f.bls_code, f.name_de, f.catalog_kind, f.owner_profile_id FROM food_name_mappings m JOIN food_catalog f ON f.id = m.food_id WHERE m.source_system = %s AND m.source_name_normalized = %s AND m.profile_id IS NULL + AND f.is_active = true + AND f.owner_profile_id IS NULL LIMIT 1 """, (source_system, norm), @@ -448,6 +452,9 @@ def upsert_food_mapping( grams_per_unit: float | None = None, source_unit: str | None = None, ) -> int: + from data_layer.food_visibility import require_mapping_target + + require_mapping_target(cur, food_id, profile_id) norm = normalize_food_name(source_name_raw) if not norm: raise ValueError("Leerer Lebensmittelname") @@ -622,7 +629,7 @@ def suggest_catalog_foods(cur, query: str, profile_id: str | None, limit: int = extra_sql = " OR (" + " AND ".join(parts) + ")" cur.execute( f""" - SELECT id, bls_code, name_de, name_en, catalog_kind, food_group + SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, owner_profile_id FROM food_catalog WHERE is_active = true AND ( @@ -642,6 +649,10 @@ def suggest_catalog_foods(cur, query: str, profile_id: str | None, limit: int = WHEN name_de ILIKE %s THEN 1 WHEN COALESCE(bls_code, '') ILIKE %s THEN 2 ELSE 3 END, + CASE catalog_kind + WHEN 'manual_user' THEN 0 + WHEN 'manual_admin' THEN 1 + ELSE 2 END, name_de LIMIT %s """, diff --git a/backend/data_layer/food_suggest.py b/backend/data_layer/food_suggest.py index 570945a..bd6d2ea 100644 --- a/backend/data_layer/food_suggest.py +++ b/backend/data_layer/food_suggest.py @@ -131,6 +131,8 @@ def score_name_match(query: str, name_de: str, name_en: str | None = None) -> in def _public(food: dict[str, Any], score: int) -> dict[str, Any]: + from data_layer.food_visibility import visibility_of + return { "id": str(food["id"]), "bls_code": food.get("bls_code"), @@ -138,6 +140,7 @@ def _public(food: dict[str, Any], score: int) -> dict[str, Any]: "name_en": food.get("name_en"), "catalog_kind": food.get("catalog_kind"), "food_group": food.get("food_group"), + "visibility": visibility_of(food), "score": score, } @@ -163,7 +166,7 @@ def get_suggest_index(cur, profile_id: str | None) -> dict[str, Any]: def load_suggest_index(cur, profile_id: str | None) -> dict[str, Any]: cur.execute( """ - SELECT id, bls_code, name_de, name_en, catalog_kind, food_group + SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, owner_profile_id FROM food_catalog WHERE is_active = true AND (owner_profile_id IS NULL OR owner_profile_id = %s) @@ -245,6 +248,11 @@ def _candidate_foods(index: dict[str, Any], query: str) -> list[dict[str, Any]]: return out[:MAX_CANDIDATES] if not number_tokens(query) else out +def _vis_rank(food: dict[str, Any]) -> int: + from data_layer.food_visibility import visibility_rank + return visibility_rank(food) + + def suggest_for_name(index: dict[str, Any], query: str, limit: int = 3) -> dict[str, Any]: q = (query or "").strip() scored: list[tuple[int, int, dict]] = [] @@ -253,7 +261,7 @@ def suggest_for_name(index: dict[str, Any], query: str, limit: int = 3) -> dict[ if score < MIN_SCORE: continue scored.append((score, len(food.get("name_de") or ""), food)) - scored.sort(key=lambda x: (-x[0], x[1], x[2].get("name_de") or "")) + scored.sort(key=lambda x: (-x[0], _vis_rank(x[2]), x[1], x[2].get("name_de") or "")) top = [_public(food, score) for score, _nlen, food in scored[: max(limit, 3)]] ambiguous = False if len(top) >= 2 and top[0]["score"] - top[1]["score"] <= 8 and top[1]["score"] >= 60: @@ -296,7 +304,7 @@ def suggest_catalog_foods_ranked(cur, query: str, profile_id: str | None, limit: if score < MIN_SCORE and not number_tokens(q): continue hits.append(_public(row, score or 40)) - hits.sort(key=lambda h: (-int(h.get("score") or 0), h.get("name_de") or "")) + hits.sort(key=lambda h: (-int(h.get("score") or 0), _vis_rank(h), h.get("name_de") or "")) return hits[:limit] diff --git a/backend/data_layer/food_visibility.py b/backend/data_layer/food_visibility.py new file mode 100644 index 0000000..93dcc23 --- /dev/null +++ b/backend/data_layer/food_visibility.py @@ -0,0 +1,105 @@ +"""Who may see and map a catalog food. + +Scopes: +- personal — ``manual_user`` with ``owner_profile_id`` (only that profile) +- global — ``manual_admin`` (all users; no owner) +- bls — official BLS (all users; no owner) + +Name-mappings: a *user* mapping may target any food visible to that user. +A *global* mapping (``profile_id IS NULL``) may only target global/BLS foods, +never another user's private entry. +""" +from __future__ import annotations + +from typing import Any + +VIS_PERSONAL = "personal" +VIS_GLOBAL = "global" +VIS_BLS = "bls" + +SQL_VISIBLE_TO_PROFILE = "(owner_profile_id IS NULL OR owner_profile_id = %s)" +SQL_GLOBAL_ONLY = "owner_profile_id IS NULL" + +_KIND_VIS = { + "official_bls": VIS_BLS, + "manual_admin": VIS_GLOBAL, + "manual_user": VIS_PERSONAL, +} + +_VIS_RANK = {VIS_PERSONAL: 0, VIS_GLOBAL: 1, VIS_BLS: 2} + + +def visibility_of(food: dict[str, Any] | None) -> str: + if not food: + return VIS_BLS + kind = food.get("catalog_kind") + if kind in _KIND_VIS: + return _KIND_VIS[kind] + if food.get("owner_profile_id"): + return VIS_PERSONAL + return VIS_GLOBAL + + +def visibility_rank(food: dict[str, Any] | None) -> int: + return _VIS_RANK.get(visibility_of(food), 3) + + +def is_active(food: dict[str, Any] | None) -> bool: + if not food: + return False + if "is_active" not in food: + return True + return bool(food.get("is_active")) + + +def is_visible_to(food: dict[str, Any] | None, profile_id: str | None) -> bool: + if not is_active(food): + return False + owner = food.get("owner_profile_id") + if owner is None: + return True + return profile_id is not None and str(owner) == str(profile_id) + + +def is_global_mapping_target(food: dict[str, Any] | None) -> bool: + return is_active(food) and food.get("owner_profile_id") is None + + +def load_catalog_food(cur, food_id: str) -> dict[str, Any] | None: + cur.execute( + """ + SELECT id, name_de, catalog_kind, owner_profile_id, is_active, bls_code + FROM food_catalog + WHERE id = %s + """, + (food_id,), + ) + row = cur.fetchone() + return dict(row) if row else None + + +def require_visible_food(cur, food_id: str, profile_id: str) -> dict[str, Any]: + food = load_catalog_food(cur, food_id) + if not is_visible_to(food, profile_id): + raise LookupError("Lebensmittel nicht gefunden") + return food + + +def require_global_mapping_target(cur, food_id: str) -> dict[str, Any]: + food = load_catalog_food(cur, food_id) + if not food or not is_active(food): + raise LookupError("Lebensmittel nicht gefunden") + if not is_global_mapping_target(food): + raise PermissionError("Globales Mapping darf nur auf zentrale oder BLS-Lebensmittel zeigen") + return food + + +def require_mapping_target(cur, food_id: str, mapping_profile_id: str | None) -> dict[str, Any]: + if mapping_profile_id: + return require_visible_food(cur, food_id, mapping_profile_id) + return require_global_mapping_target(cur, food_id) + + +def attach_visibility(row: dict[str, Any]) -> dict[str, Any]: + row["visibility"] = visibility_of(row) + return row diff --git a/backend/data_layer/nutrition_items.py b/backend/data_layer/nutrition_items.py index 46ec4f3..91746b3 100644 --- a/backend/data_layer/nutrition_items.py +++ b/backend/data_layer/nutrition_items.py @@ -487,3 +487,45 @@ def dates_for_normalized_name(cur, profile_id: str, source_name_normalized: str) (profile_id, source_name_normalized, profile_id, source_name_normalized), ) return [r["date"] for r in cur.fetchall()] + + +def dates_using_food(cur, profile_id: str, food_id: str) -> list[str]: + cur.execute( + """ + SELECT DISTINCT date::text AS date + FROM nutrition_items + WHERE profile_id = %s AND food_id = %s + UNION + SELECT DISTINCT i.date::text AS date + FROM nutrition_items i + JOIN food_recipe_ingredients ri ON ri.recipe_id = i.recipe_id + JOIN food_name_mappings m + ON m.profile_id = i.profile_id + AND m.source_name_normalized = ri.source_name_normalized + AND m.food_id = %s + WHERE i.profile_id = %s AND i.recipe_id IS NOT NULL + """, + (profile_id, food_id, food_id, profile_id), + ) + return [r["date"] for r in cur.fetchall() if r.get("date")] + + +def profiles_dates_using_food(cur, food_id: str) -> list[tuple[str, str]]: + cur.execute( + """ + SELECT DISTINCT profile_id::text AS profile_id, date::text AS date + FROM nutrition_items + WHERE food_id = %s + UNION + SELECT DISTINCT i.profile_id::text AS profile_id, i.date::text AS date + FROM nutrition_items i + JOIN food_recipe_ingredients ri ON ri.recipe_id = i.recipe_id + JOIN food_name_mappings m + ON m.profile_id = i.profile_id + AND m.source_name_normalized = ri.source_name_normalized + AND m.food_id = %s + WHERE i.recipe_id IS NOT NULL + """, + (food_id, food_id), + ) + return [(r["profile_id"], r["date"]) for r in cur.fetchall() if r.get("profile_id") and r.get("date")] diff --git a/backend/migrations/069_food_catalog_visibility.sql b/backend/migrations/069_food_catalog_visibility.sql new file mode 100644 index 0000000..666b7a8 --- /dev/null +++ b/backend/migrations/069_food_catalog_visibility.sql @@ -0,0 +1,18 @@ +-- Visibility invariant: personal foods have an owner; BLS and central admin foods do not. + +UPDATE food_catalog +SET owner_profile_id = NULL +WHERE catalog_kind IN ('official_bls', 'manual_admin') + AND owner_profile_id IS NOT NULL; + +UPDATE food_catalog +SET is_active = false +WHERE catalog_kind = 'manual_user' + AND owner_profile_id IS NULL; + +ALTER TABLE food_catalog DROP CONSTRAINT IF EXISTS chk_food_catalog_owner; + +ALTER TABLE food_catalog ADD CONSTRAINT chk_food_catalog_owner CHECK ( + (catalog_kind = 'manual_user' AND owner_profile_id IS NOT NULL) + OR (catalog_kind IN ('official_bls', 'manual_admin') AND owner_profile_id IS NULL) +); diff --git a/backend/routers/admin_bls.py b/backend/routers/admin_bls.py index 8546512..d078521 100644 --- a/backend/routers/admin_bls.py +++ b/backend/routers/admin_bls.py @@ -32,6 +32,8 @@ class ManualFoodCreate(BaseModel): name_de: str name_en: Optional[str] = None macros_per_100g: Optional[dict] = None + attributes: Optional[dict] = None + serving_g: Optional[float] = None @router.get("/status") @@ -162,7 +164,9 @@ def admin_list_foods( cur = get_cursor(conn) conds = ["is_active = true"] params: list = [] - if kind: + if kind == "manual": + conds.append("catalog_kind IN ('manual_user', 'manual_admin')") + elif kind: conds.append("catalog_kind = %s") params.append(kind) if q: @@ -172,7 +176,7 @@ def admin_list_foods( params.append(limit) cur.execute( f""" - SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, bls_version + SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, bls_version, owner_profile_id FROM food_catalog WHERE {' AND '.join(conds)} ORDER BY name_de @@ -199,11 +203,15 @@ def admin_food_detail(food_id: str, session: dict = Depends(require_admin)): @router.post("/foods/manual") def admin_create_manual_food(body: ManualFoodCreate, session: dict = Depends(require_admin)): + from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes from data_layer.food_mapping import normalize_food_name + from data_layer.food_visibility import attach_visibility name = (body.name_de or "").strip() if not name: raise HTTPException(400, "Name fehlt") + serving = body.serving_g if body.serving_g and body.serving_g > 0 else None + values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving) with get_db() as conn: cur = get_cursor(conn) cur.execute( @@ -215,10 +223,64 @@ def admin_create_manual_food(body: ManualFoodCreate, session: dict = Depends(req (name, body.name_en, f"man-admin-{normalize_food_name(name)[:40]}"), ) food = r2d(cur.fetchone()) - _write_manual_macros(cur, food["id"], body.macros_per_100g) + write_numeric_attributes(cur, food["id"], values) from data_layer.food_suggest import invalidate_suggest_index invalidate_suggest_index() - return food + return attach_visibility(food) + + +@router.put("/foods/manual/{food_id}") +def admin_update_manual_food(food_id: str, body: ManualFoodCreate, session: dict = Depends(require_admin)): + from collections import defaultdict + + from data_layer.food_attributes import ( + editor_payload_for_food, + macros_and_attributes_to_values, + replace_numeric_attributes, + ) + from data_layer.food_visibility import attach_visibility + from data_layer.nutrition_items import profiles_dates_using_food, schedule_daily_nutrient_rebuild + + name = (body.name_de or "").strip() + if not name: + raise HTTPException(400, "Name fehlt") + serving = body.serving_g if body.serving_g and body.serving_g > 0 else None + values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving) + with get_db() as conn: + cur = get_cursor(conn) + cur.execute( + """ + SELECT id, name_de, name_en, catalog_kind, owner_profile_id + FROM food_catalog + WHERE id = %s AND catalog_kind = 'manual_admin' AND is_active = true + """, + (food_id,), + ) + food = cur.fetchone() + if not food: + raise HTTPException(404, "Zentrales Lebensmittel nicht gefunden") + cur.execute( + """ + UPDATE food_catalog + SET name_de = %s, name_en = %s, updated_at = NOW() + WHERE id = %s + RETURNING id, name_de, name_en, catalog_kind, owner_profile_id + """, + (name, body.name_en, food_id), + ) + updated = dict(cur.fetchone()) + replace_numeric_attributes(cur, food_id, values) + pairs = profiles_dates_using_food(cur, food_id) + payload = attach_visibility(editor_payload_for_food(cur, updated, editable=True)) + from data_layer.food_suggest import invalidate_suggest_index + invalidate_suggest_index() + by_profile = defaultdict(list) + for pid, day in pairs: + by_profile[pid].append(day) + for pid, dates in by_profile.items(): + schedule_daily_nutrient_rebuild(pid, dates, f"Zentrales Lebensmittel {name}") + payload["rebuild_pending"] = len(pairs) + return payload @router.get("/attributes") diff --git a/backend/routers/admin_food_mappings.py b/backend/routers/admin_food_mappings.py index dbdfc2c..886d2f1 100644 --- a/backend/routers/admin_food_mappings.py +++ b/backend/routers/admin_food_mappings.py @@ -52,7 +52,8 @@ def list_food_mappings( q += " WHERE " + " AND ".join(conds) q += " ORDER BY m.source_name_normalized" cur.execute(q, params) - return [r2d(r) for r in cur.fetchall()] + from data_layer.food_visibility import attach_visibility + return [attach_visibility(r2d(r)) for r in cur.fetchall()] @router.get("/stats/coverage") @@ -77,9 +78,13 @@ def mapping_coverage(session: dict = Depends(require_admin)): def create_food_mapping(body: FoodMappingCreate, session: dict = Depends(require_admin)): with get_db() as conn: cur = get_cursor(conn) - cur.execute("SELECT id FROM food_catalog WHERE id = %s", (body.food_id,)) - if not cur.fetchone(): - raise HTTPException(404, "Lebensmittel nicht gefunden") + from data_layer.food_visibility import require_mapping_target + try: + require_mapping_target(cur, body.food_id, body.profile_id or None) + except LookupError as e: + raise HTTPException(404, str(e)) from e + except PermissionError as e: + raise HTTPException(400, str(e)) from e mid = upsert_food_mapping( cur, source_name_raw=body.source_name, @@ -114,9 +119,17 @@ def update_food_mapping(mapping_id: int, body: FoodMappingUpdate, session: dict row = cur.fetchone() if not row: raise HTTPException(404, "Mapping nicht gefunden") - cur.execute("SELECT id FROM food_catalog WHERE id = %s AND is_active = true", (body.food_id,)) - if not cur.fetchone(): + cur.execute("SELECT id, catalog_kind, owner_profile_id, is_active FROM food_catalog WHERE id = %s AND is_active = true", (body.food_id,)) + food = cur.fetchone() + if not food: raise HTTPException(404, "Lebensmittel nicht gefunden") + from data_layer.food_visibility import require_mapping_target + try: + require_mapping_target(cur, body.food_id, row.get("profile_id")) + except LookupError as e: + raise HTTPException(404, str(e)) from e + except PermissionError as e: + raise HTTPException(400, str(e)) from e cur.execute( """ UPDATE food_name_mappings diff --git a/backend/routers/bls.py b/backend/routers/bls.py index fceb5f5..366934d 100644 --- a/backend/routers/bls.py +++ b/backend/routers/bls.py @@ -95,12 +95,16 @@ def list_food_units(session: dict = Depends(require_auth)): def search_foods( q: str = "", limit: int = 20, + scope: str = "visible", session: dict = Depends(require_auth), ): pid = session["profile_id"] with get_db() as conn: cur = get_cursor(conn) - return suggest_catalog_foods_ranked(cur, q, pid, limit=min(max(limit, 1), 50)) + hits = suggest_catalog_foods_ranked(cur, q, pid, limit=min(max(limit, 1), 50)) + if scope == "global": + hits = [h for h in hits if h.get("visibility") != "personal"] + return hits @router.get("/attributes") @@ -172,6 +176,122 @@ def create_user_food( return food +@router.get("/foods/mine") +def list_my_manual_foods(session: dict = Depends(require_auth)): + pid = session["profile_id"] + with get_db() as conn: + cur = get_cursor(conn) + cur.execute( + """ + SELECT id, name_de, catalog_kind, updated_at + FROM food_catalog + WHERE owner_profile_id = %s AND catalog_kind = 'manual_user' AND is_active = true + ORDER BY name_de + """, + (pid,), + ) + rows = [r2d(r) for r in cur.fetchall()] + from data_layer.food_visibility import attach_visibility + for row in rows: + row["id"] = str(row["id"]) + attach_visibility(row) + return rows + + +@router.get("/foods/shared") +def list_shared_manual_foods(session: dict = Depends(require_auth)): + with get_db() as conn: + cur = get_cursor(conn) + cur.execute( + """ + SELECT id, name_de, catalog_kind, updated_at + FROM food_catalog + WHERE catalog_kind = 'manual_admin' AND owner_profile_id IS NULL AND is_active = true + ORDER BY name_de + """ + ) + from data_layer.food_visibility import attach_visibility + rows = [r2d(r) for r in cur.fetchall()] + for row in rows: + row["id"] = str(row["id"]) + attach_visibility(row) + return rows + + +@router.get("/foods/{food_id}") +def get_food_detail(food_id: str, session: dict = Depends(require_auth)): + from data_layer.food_attributes import editor_payload_for_food + + pid = session["profile_id"] + with get_db() as conn: + cur = get_cursor(conn) + cur.execute( + """ + SELECT id, name_de, name_en, catalog_kind, owner_profile_id + FROM food_catalog + WHERE id = %s AND is_active = true + AND (owner_profile_id IS NULL OR owner_profile_id = %s) + """, + (food_id, pid), + ) + food = cur.fetchone() + if not food: + raise HTTPException(404, "Lebensmittel nicht gefunden") + food = dict(food) + editable = food.get("catalog_kind") == "manual_user" and str(food.get("owner_profile_id") or "") == str(pid) + from data_layer.food_visibility import attach_visibility + return attach_visibility(editor_payload_for_food(cur, food, editable=editable)) + + +@router.put("/foods/manual/{food_id}") +def update_user_food(food_id: str, body: UserFoodCreate, session: dict = Depends(require_auth)): + from data_layer.food_attributes import ( + editor_payload_for_food, + macros_and_attributes_to_values, + replace_numeric_attributes, + ) + from data_layer.nutrition_items import dates_using_food + + pid = session["profile_id"] + name = (body.name_de or "").strip() + if not name: + raise HTTPException(400, "Name fehlt") + serving = body.serving_g if body.serving_g and body.serving_g > 0 else None + values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving) + with get_db() as conn: + cur = get_cursor(conn) + cur.execute( + """ + SELECT id, name_de, name_en, catalog_kind, owner_profile_id + FROM food_catalog + WHERE id = %s AND catalog_kind = 'manual_user' AND owner_profile_id = %s AND is_active = true + """, + (food_id, pid), + ) + food = cur.fetchone() + if not food: + raise HTTPException(404, "Eigenes Lebensmittel nicht gefunden") + cur.execute( + """ + UPDATE food_catalog + SET name_de = %s, name_en = %s, updated_at = NOW() + WHERE id = %s + RETURNING id, name_de, name_en, catalog_kind, owner_profile_id + """, + (name, body.name_en, food_id), + ) + updated = dict(cur.fetchone()) + replace_numeric_attributes(cur, food_id, values) + dates = dates_using_food(cur, pid, food_id) + payload = editor_payload_for_food(cur, updated, editable=True) + from data_layer.food_visibility import attach_visibility + payload = attach_visibility(payload) + invalidate_suggest_index(pid) + _schedule_rebuild(pid, dates, f"Lebensmittel {name}") + payload["rebuild_pending"] = len(dates) + return payload + + @router.get("/mappings") def list_my_mappings( x_profile_id: Optional[str] = Header(default=None), @@ -192,7 +312,8 @@ def list_my_mappings( """, (pid,), ) - return [r2d(r) for r in cur.fetchall()] + from data_layer.food_visibility import attach_visibility + return [attach_visibility(r2d(r)) for r in cur.fetchall()] @router.post("/mappings") @@ -204,27 +325,26 @@ def upsert_my_mapping( pid = _pid(session, x_profile_id) with get_db() as conn: cur = get_cursor(conn) - cur.execute( - """ - SELECT id, name_de, bls_code, catalog_kind FROM food_catalog - WHERE id = %s AND is_active = true - AND (owner_profile_id IS NULL OR owner_profile_id = %s) - """, - (body.food_id, pid), - ) - food = cur.fetchone() - if not food: - raise HTTPException(404, "Lebensmittel nicht gefunden") - mid = upsert_food_mapping( - cur, - source_name_raw=body.source_name, - food_id=body.food_id, - profile_id=pid, - source="bulk", - source_system=body.source_system, - grams_per_unit=body.grams_per_unit, - source_unit=body.source_unit, - ) + from data_layer.food_visibility import require_visible_food, visibility_of + try: + food = require_visible_food(cur, body.food_id, pid) + except LookupError as e: + raise HTTPException(404, str(e)) from e + try: + mid = upsert_food_mapping( + cur, + source_name_raw=body.source_name, + food_id=body.food_id, + profile_id=pid, + source="bulk", + source_system=body.source_system, + grams_per_unit=body.grams_per_unit, + source_unit=body.source_unit, + ) + except PermissionError as e: + raise HTTPException(400, str(e)) from e + except LookupError as e: + raise HTTPException(404, str(e)) from e norm = normalize_food_name(body.source_name) n = apply_mapping_to_items(cur, pid, norm, body.food_id, mid) apply_quantities_to_items(cur, pid, norm, body.grams_per_unit) @@ -238,6 +358,7 @@ def upsert_my_mapping( "food_name_de": food["name_de"], "bls_code": food.get("bls_code"), "catalog_kind": food.get("catalog_kind"), + "visibility": visibility_of(food), } diff --git a/backend/tests/test_food_attributes.py b/backend/tests/test_food_attributes.py index 53cdcfa..a471217 100644 --- a/backend/tests/test_food_attributes.py +++ b/backend/tests/test_food_attributes.py @@ -1,5 +1,6 @@ from data_layer.food_attributes import ( create_extension_attribute, + editor_payload_for_food, macros_and_attributes_to_values, normalize_attr_key, normalize_attr_unit, @@ -45,3 +46,41 @@ def test_norsan_label_epa_converts(): assert abs(vals["FAT"] - 100) < 0.01 assert abs(vals["EPA"] - 13.75) < 0.01 assert abs(vals["ENERCC"] - 900) < 0.01 + + +def test_edit_payload_does_not_rescale_at_100g(): + vals = macros_and_attributes_to_values( + {"kcal": 900, "protein_g": 0, "fat_g": 100, "carbs_g": 0}, + {"EPA": 13.75}, + serving_g=100, + ) + assert vals["FAT"] == 100 + assert vals["EPA"] == 13.75 + assert vals["ENERCC"] == 900 + + +def test_editor_payload_splits_macros_and_extras(): + class _Cur: + def execute(self, sql, params=None): + pass + + def fetchall(self): + return [ + {"attr_key": "ENERCC", "name_de": "Energie", "name_en": None, "unit": "kcal", "category": "energy", "origin": "bls", "value_num": 900}, + {"attr_key": "FAT", "name_de": "Fett", "name_en": None, "unit": "g", "category": "macro", "origin": "bls", "value_num": 100}, + {"attr_key": "PROT625", "name_de": "Protein", "name_en": None, "unit": "g", "category": "macro", "origin": "bls", "value_num": 0}, + {"attr_key": "CHO", "name_de": "KH", "name_en": None, "unit": "g", "category": "macro", "origin": "bls", "value_num": 0}, + {"attr_key": "EPA", "name_de": "EPA", "name_en": None, "unit": "g", "category": "extension", "origin": "extension", "value_num": 13.75}, + ] + + payload = editor_payload_for_food( + _Cur(), + {"id": "abc", "name_de": "Omega-3", "name_en": None, "catalog_kind": "manual_user"}, + editable=True, + ) + assert payload["editable"] is True + assert payload["serving_g"] == 100 + assert payload["macros_per_100g"]["kcal"] == 900 + assert payload["macros_per_100g"]["fat_g"] == 100 + assert payload["attributes"][0]["attr"]["attr_key"] == "EPA" + assert payload["attributes"][0]["value"] == 13.75 diff --git a/backend/tests/test_food_suggest.py b/backend/tests/test_food_suggest.py index 3c4dc03..523bf15 100644 --- a/backend/tests/test_food_suggest.py +++ b/backend/tests/test_food_suggest.py @@ -105,3 +105,11 @@ def test_joghurt_10_ranks_above_35(): packed = suggest_for_name(index, query, limit=3) assert packed["suggestions"][0]["name_de"] == full["name_de"], query assert packed["suggestions"][0]["score"] > score_name_match(query, light["name_de"]) + + +def test_personal_food_ranks_above_bls_on_tie(): + bls = {"id": "bls", "name_de": "Omega-3 Öl", "name_en": "", "bls_code": "X", "catalog_kind": "official_bls"} + mine = {"id": "me", "name_de": "Omega-3 Öl", "name_en": "", "bls_code": None, "catalog_kind": "manual_user", "owner_profile_id": "u1"} + packed = suggest_for_name(_index(bls, mine), "Omega-3 Öl", limit=2) + assert packed["suggestions"][0]["id"] == "me" + assert packed["suggestions"][0]["visibility"] == "personal" diff --git a/backend/tests/test_food_visibility.py b/backend/tests/test_food_visibility.py new file mode 100644 index 0000000..42c9b0d --- /dev/null +++ b/backend/tests/test_food_visibility.py @@ -0,0 +1,90 @@ +from data_layer.food_visibility import ( + attach_visibility, + is_global_mapping_target, + is_visible_to, + require_mapping_target, + visibility_of, + visibility_rank, +) + + +def test_visibility_scopes(): + assert visibility_of({"catalog_kind": "official_bls"}) == "bls" + assert visibility_of({"catalog_kind": "manual_admin", "owner_profile_id": None}) == "global" + assert visibility_of({"catalog_kind": "manual_user", "owner_profile_id": "u1"}) == "personal" + assert visibility_rank({"catalog_kind": "manual_user"}) < visibility_rank({"catalog_kind": "manual_admin"}) + assert visibility_rank({"catalog_kind": "manual_admin"}) < visibility_rank({"catalog_kind": "official_bls"}) + + +def test_personal_food_not_visible_to_others(): + food = {"catalog_kind": "manual_user", "owner_profile_id": "alice", "is_active": True} + assert is_visible_to(food, "alice") is True + assert is_visible_to(food, "bob") is False + assert is_global_mapping_target(food) is False + + +def test_admin_and_bls_visible_to_everyone(): + admin = {"catalog_kind": "manual_admin", "owner_profile_id": None, "is_active": True} + bls = {"catalog_kind": "official_bls", "owner_profile_id": None, "is_active": True} + assert is_visible_to(admin, "bob") is True + assert is_visible_to(bls, None) is True + assert is_global_mapping_target(admin) is True + assert is_global_mapping_target(bls) is True + + +def test_attach_visibility(): + row = attach_visibility({"catalog_kind": "manual_admin"}) + assert row["visibility"] == "global" + + +def test_require_global_mapping_rejects_personal(): + class _Cur: + def execute(self, sql, params=None): + self._row = { + "id": params[0], + "name_de": "Öl", + "catalog_kind": "manual_user", + "owner_profile_id": "alice", + "is_active": True, + "bls_code": None, + } + + def fetchone(self): + return self._row + + try: + require_mapping_target(_Cur(), "fid", None) + assert False, "expected PermissionError" + except PermissionError: + pass + + +def test_require_user_mapping_allows_own_and_global(): + class _Cur: + def __init__(self, food): + self.food = food + + def execute(self, sql, params=None): + pass + + def fetchone(self): + return self.food + + own = require_mapping_target( + _Cur({ + "id": "1", "name_de": "X", "catalog_kind": "manual_user", + "owner_profile_id": "alice", "is_active": True, "bls_code": None, + }), + "1", + "alice", + ) + assert own["catalog_kind"] == "manual_user" + shared = require_mapping_target( + _Cur({ + "id": "2", "name_de": "Y", "catalog_kind": "manual_admin", + "owner_profile_id": None, "is_active": True, "bls_code": None, + }), + "2", + "alice", + ) + assert shared["catalog_kind"] == "manual_admin" diff --git a/backend/version.py b/backend/version.py index 4bc3867..99f48b7 100644 --- a/backend/version.py +++ b/backend/version.py @@ -8,8 +8,8 @@ Semantic Versioning: MAJOR.MINOR.PATCH """ APP_VERSION = "0.9v" -BUILD_DATE = "2026-09-12" -DB_SCHEMA_VERSION = "20260912e" # 068 nutrition unmapped/recipe indexes +BUILD_DATE = "2026-09-14" +DB_SCHEMA_VERSION = "20260914a" # 069 food catalog visibility owner check MODULE_VERSIONS = { "auth": "1.2.0", @@ -20,9 +20,10 @@ MODULE_VERSIONS = { "circumference": "1.0.1", "caliper": "1.0.1", "activity": "1.2.1", # Legacy CSV import: activity_entries feature enforcement - "nutrition": "1.3.5", # Listen-Dialog: Mapping an der Zutat; Suche Listen/eigene vor BLS + "nutrition": "1.3.7", # Sichtbarkeit persönlich vs. zentral im Mapping "tandoor": "1.0.1", # Router-Import ohne future-annotations (Backend startet) - "bls": "1.0.6", # Nutzer darf Extension-Stoffe anlegen; EPA-Chips; mg/µg am Etikett + "bls": "1.0.9", # Nutzer-Foods privat, Admin-Foods zentral; Mapping achtet Sichtbarkeit + "photos": "1.0.0", "insights": "1.3.0", "prompts": "1.1.0", @@ -55,6 +56,9 @@ CHANGELOG = [ "Zuordnen: Zeitraum 14 Tage / 4 Wochen / 90 Tage / Alle — zuerst aktuelle Tagebuchnamen", "Katalogsuche: Fettgehalt (9,5 / 10 %) bleibt erhalten, Joghurt 10% vor >3,5%", "Eigenes Lebensmittel: beliebige Katalog-Stoffe (EPA/DHA/…) plus Portionsumrechnung auf 100 g", + "Eigene Lebensmittel nachträglich bearbeiten (Name, Makros, Stoffe); BLS bleibt fest", + "Reiter Lebensmittel (Nutzer) und Admin-Katalog Manuell: Liste + Suche", + "Sichtbarkeit: persönliche Foods nur Owner, zentrale Admin-Foods für alle; Mapping trennt global vs. Nutzer", "Zuordnen: Mapping-Export robust; Rezepte anlegen/bearbeiten; Admin-Mapping direkt ändern", "Rezept-Dialog: Zutaten aus Zuordnungen und Katalog suchen; Tandoor später", "Rezeptzutaten: Menge + Einheit (EL/TL/Prise/Stück); Umrechnung auf Gramm", diff --git a/docs/issues/issue-bls-food-mapping.md b/docs/issues/issue-bls-food-mapping.md index 1dead09..e449738 100644 --- a/docs/issues/issue-bls-food-mapping.md +++ b/docs/issues/issue-bls-food-mapping.md @@ -19,6 +19,7 @@ Verlässliche Lebensmittel-Stammdaten (BLS 4.0 + manuelle Erweiterung), lernende - Zuordnungen und Listen als JSON zwischen Instanzen übertragen - Eigener Katalogeintrag im Suchpopup; Gramm pro Stück/EL/… am Mapping - Manueller Eintrag: beliebige `num_per_100g`-Stoffe (EPA/DHA/…) und Portionsumrechnung +- Eigene Lebensmittel nachträglich bearbeiten (Tab Ernährung → Lebensmittel; persönlich vs. zentral; BLS unveränderlich) - Listen-Dialog mit Zutatensuche (Mappings + Katalog); Mitai = Roh-Kombination - Listenzutaten: Menge + Einheit, Umrechnung auf Gramm (EL/TL/Prise/Stück) - Tandoor: persönlicher URL+Token in Einstellungen, Verbindungstest (kein Import) diff --git a/frontend/src/app.css b/frontend/src/app.css index f8092ee..aebbfc2 100644 --- a/frontend/src/app.css +++ b/frontend/src/app.css @@ -191,7 +191,7 @@ body { font-family: var(--font); background: var(--bg); color: var(--text1); -we /* Tabs */ .tabs { display: flex; gap: 4px; background: var(--surface2); border-radius: 10px; padding: 3px; margin-bottom: 16px; } -.tab { flex: 1; text-align: center; padding: 7px; border-radius: 8px; font-size: 13px; font-weight: 500; color: var(--text3); cursor: pointer; border: none; background: transparent; font-family: var(--font); } +.tab { flex: 1; text-align: center; padding: 7px; border-radius: 8px; font-size: 13px; font-weight: 500; color: var(--text3); cursor: pointer; border: none; background: transparent; font-family: var(--font); white-space: nowrap; } .tab.active { background: var(--surface); color: var(--text1); box-shadow: 0 1px 3px rgba(0,0,0,0.1); } /* Section */ diff --git a/frontend/src/components/FoodEditDialog.jsx b/frontend/src/components/FoodEditDialog.jsx new file mode 100644 index 0000000..a69376c --- /dev/null +++ b/frontend/src/components/FoodEditDialog.jsx @@ -0,0 +1,146 @@ +import { useEffect, useState } from 'react' +import { api } from '../utils/api' +import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields' + +function numStr(v) { + if (v == null || v === '') return '0' + const n = Number(v) + return Number.isFinite(n) ? String(n) : '0' +} + +export default function FoodEditDialog({ foodId, onClose, onSaved, zIndex, mode }) { + const isCreate = !foodId + const isAdmin = mode === 'admin' + const [name, setName] = useState('') + const [macros, setMacros] = useState({ kcal: '0', protein_g: '0', fat_g: '0', carbs_g: '0' }) + const [extras, setExtras] = useState([]) + const [servingG, setServingG] = useState('100') + const [editable, setEditable] = useState(false) + const [loading, setLoading] = useState(!isCreate) + const [saving, setSaving] = useState(false) + const [error, setError] = useState(null) + + useEffect(() => { + if (isCreate) { + setEditable(true) + setLoading(false) + return + } + let alive = true + api.getBlsFood(foodId).then((d) => { + if (!alive) return + setName(d.name_de || '') + const m = d.macros_per_100g || {} + setMacros({ + kcal: numStr(m.kcal), + protein_g: numStr(m.protein_g), + fat_g: numStr(m.fat_g), + carbs_g: numStr(m.carbs_g), + }) + setExtras((d.attributes || []).map((row) => ({ + attr: row.attr, + value: numStr(row.value), + inputUnit: row.attr?.unit || 'g', + }))) + setServingG(numStr(d.serving_g || 100) === '0' ? '100' : numStr(d.serving_g || 100)) + setEditable(isAdmin ? d.catalog_kind === 'manual_admin' : Boolean(d.editable)) + setLoading(false) + }).catch((e) => { + if (!alive) return + setError(e.message) + setLoading(false) + }) + return () => { alive = false } + }, [foodId, isCreate, isAdmin]) + + const save = async () => { + if (!name.trim()) { + setError('Name fehlt') + return + } + setSaving(true) + setError(null) + try { + const body = foodCreatePayload(name, macros, extras, servingG) + let saved + if (isAdmin) { + saved = isCreate ? await api.adminCreateManualFood(body) : await api.adminUpdateManualFood(foodId, body) + } else { + saved = isCreate ? await api.createUserFood(body) : await api.updateUserFood(foodId, body) + } + onSaved?.(saved) + onClose?.() + } catch (e) { + setError(e.message) + } finally { + setSaving(false) + } + } + + return ( +
+
+
+

+ {isCreate + ? (isAdmin ? 'Zentrales Lebensmittel anlegen' : 'Lebensmittel anlegen') + : (isAdmin ? 'Zentrales Lebensmittel bearbeiten' : 'Lebensmittel bearbeiten')} +

+ +
+
+ {loading &&

Lade…

} + {error &&
{error}
} + {!loading && !editable && ( +

+ Offizielle BLS-Einträge sind fest. Nur selbst angelegte{isAdmin ? ' zentrale' : ''} Lebensmittel lassen sich ändern. +

+ )} + {!loading && editable && ( + <> + + setName(e.target.value)} + /> + + + )} +
+ {editable && ( +
+ + +
+ )} +
+
+ ) +} diff --git a/frontend/src/components/FoodNutrientFields.jsx b/frontend/src/components/FoodNutrientFields.jsx index 68ccffa..3239356 100644 --- a/frontend/src/components/FoodNutrientFields.jsx +++ b/frontend/src/components/FoodNutrientFields.jsx @@ -45,7 +45,7 @@ export function foodCreatePayload(name, macros, extras, servingG) { } } -export default function FoodNutrientFields({ macros, setMacros, extras, setExtras, servingG, setServingG }) { +export default function FoodNutrientFields({ macros, setMacros, extras, setExtras, servingG, setServingG, valuesArePer100g }) { const [q, setQ] = useState('') const [hits, setHits] = useState([]) const [quick, setQuick] = useState([]) @@ -124,8 +124,9 @@ export default function FoodNutrientFields({ macros, setMacros, extras, setExtra return (

- Etikettwerte so eintragen, wie sie dastehen. Portionsgröße setzen (5 ml Öl ≈ 5 g). Gespeichert wird immer pro 100 g. - Ein Öl/Supplement ist ein Lebensmittel, keine Liste — Listen können keine Listen enthalten. + {valuesArePer100g + ? 'Geladen sind die gespeicherten Werte je 100 g. Portion nur ändern, wenn du vom Etikett neu einträgst (sonst 100 lassen).' + : 'Etikettwerte so eintragen, wie sie dastehen. Portionsgröße setzen (5 ml Öl ≈ 5 g). Gespeichert wird immer pro 100 g. Ein Öl/Supplement ist ein Lebensmittel, keine Liste — Listen können keine Listen enthalten.'}

) diff --git a/frontend/src/components/FoodRecipeDialog.jsx b/frontend/src/components/FoodRecipeDialog.jsx index 01a8bb5..95f635c 100644 --- a/frontend/src/components/FoodRecipeDialog.jsx +++ b/frontend/src/components/FoodRecipeDialog.jsx @@ -1,6 +1,8 @@ import { useEffect, useRef, useState } from 'react' import { api } from '../utils/api' import FoodSearchModal from './FoodSearchModal' +import FoodEditDialog from './FoodEditDialog' +import { foodVisibilityLabel } from '../utils/foodVisibility' const FALLBACK_UNITS = [ { id: 'g', label: 'g', default_g: 1, needs_unit_map: false }, @@ -128,6 +130,7 @@ export default function FoodRecipeDialog({ const [knownNames, setKnownNames] = useState([]) const [suggestions, setSuggestions] = useState({}) const [mapFor, setMapFor] = useState(null) + const [editFoodId, setEditFoodId] = useState(null) const nameRef = useRef(null) const qtyRefs = useRef({}) const timer = useRef(null) @@ -476,21 +479,27 @@ export default function FoodRecipeDialog({ ))} {searching &&

Katalog…

} {userHits.length > 0 && ( -
Eigene Lebensmittel
+
Persönlich und zentral
)} {userHits.map((h) => ( - +
+ + {h.catalog_kind === 'manual_user' && ( + + )} +
))} {blsHits.length > 0 && (
BLS-Katalog
@@ -623,6 +632,14 @@ export default function FoodRecipeDialog({ onSelect={(food, extras) => assignIngredient(mapFor.source_name_raw, food, extras).catch((e) => setError(e.message))} /> )} + {editFoodId && ( + setEditFoodId(null)} + onSaved={() => runCatalog(q)} + /> + )}
) } diff --git a/frontend/src/components/FoodSearchModal.jsx b/frontend/src/components/FoodSearchModal.jsx index d13c0a8..08e5c8c 100644 --- a/frontend/src/components/FoodSearchModal.jsx +++ b/frontend/src/components/FoodSearchModal.jsx @@ -1,6 +1,8 @@ import { useEffect, useRef, useState } from 'react' import { api } from '../utils/api' import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields' +import FoodEditDialog from './FoodEditDialog' +import { foodVisibilitySuffix } from '../utils/foodVisibility' const COUNT_UNITS = new Set(['stück', 'scheibe', 'portion', 'becher', 'tasse']) const UNIT_LABEL = { @@ -33,6 +35,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, all const [creating, setCreating] = useState(false) const [showCreate, setShowCreate] = useState(false) const [createKind, setCreateKind] = useState(null) + const [editId, setEditId] = useState(null) const [manual, setManual] = useState({ name_de: initialQuery || '', kcal: '', protein_g: '', fat_g: '', carbs_g: '', @@ -127,6 +130,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, all const unitLabel = UNIT_LABEL[suggestedUnit] || suggestedUnit return ( + <>
= 2 && hits.length === 0 && (

Kein Treffer im Katalog.

)} - {hits.map((h) => ( - - ))} + {hits.map((h) => { + const manual = h.catalog_kind && h.catalog_kind !== 'official_bls' && h.catalog_kind !== 'manual_admin' + return ( +
+ + {manual && ( + + )} +
+ ) + })}
{!showCreate ? ( @@ -262,5 +275,14 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, all
+ {editId && ( + setEditId(null)} + onSaved={() => runSearch(q)} + /> + )} + ) } diff --git a/frontend/src/components/MyFoodsPanel.jsx b/frontend/src/components/MyFoodsPanel.jsx new file mode 100644 index 0000000..0de7f68 --- /dev/null +++ b/frontend/src/components/MyFoodsPanel.jsx @@ -0,0 +1,123 @@ +import { useEffect, useMemo, useState } from 'react' +import { api } from '../utils/api' +import FoodEditDialog from './FoodEditDialog' +import { foodVisibilitySuffix } from '../utils/foodVisibility' + +function filterByName(list, term) { + if (!term) return list + return list.filter((f) => (f.name_de || '').toLowerCase().includes(term)) +} + +export default function MyFoodsPanel() { + const [foods, setFoods] = useState([]) + const [shared, setShared] = useState([]) + const [q, setQ] = useState('') + const [visible, setVisible] = useState(60) + const [loading, setLoading] = useState(true) + const [error, setError] = useState(null) + const [notice, setNotice] = useState(null) + const [editId, setEditId] = useState(null) + const [creating, setCreating] = useState(false) + + const load = async () => { + try { + const [mine, central] = await Promise.all([ + api.listMyFoods(), + api.listSharedFoods().catch(() => []), + ]) + setFoods(Array.isArray(mine) ? mine : []) + setShared(Array.isArray(central) ? central : []) + setError(null) + } catch (e) { + setError(e.message) + } finally { + setLoading(false) + } + } + + useEffect(() => { load() }, []) + + const term = q.trim().toLowerCase() + const filteredMine = useMemo(() => filterByName(foods, term), [foods, term]) + const filteredShared = useMemo(() => filterByName(shared, term), [shared, term]) + const shownMine = filteredMine.slice(0, visible) + + const afterSave = (res) => { + load() + const n = res?.rebuild_pending + if (n) setNotice(`Gespeichert. Tageswerte für ${n} Tag${n === 1 ? '' : 'e'} werden nachgerechnet.`) + else setNotice('Lebensmittel gespeichert.') + } + + return ( +
+
Lebensmittel
+

+ Persönlich: nur für dich. Zentral: vom Admin für alle Nutzer. + BLS-Katalog suchst du beim Zuordnen. Ein Öl/Supplement bleibt ein Lebensmittel, keine Liste. +

+ {error &&
{error}
} + {notice &&
{notice}
} + +
+ +
+ + { setQ(e.target.value); setVisible(60) }} + /> + +

Persönlich ({loading ? '…' : filteredMine.length})

+ {!loading && foods.length === 0 && ( +

Noch keine eigenen Einträge. Hier anlegen oder beim Zuordnen über „Neu anlegen“.

+ )} + {!loading && foods.length > 0 && filteredMine.length === 0 && ( +

Kein persönlicher Treffer für „{q.trim()}“.

+ )} + {shownMine.map((f) => ( +
+
+
{f.name_de}
+
nur du
+
+ +
+ ))} + {visible < filteredMine.length && ( + + )} + +

Zentral für alle ({loading ? '…' : filteredShared.length})

+ {!loading && shared.length === 0 && ( +

Noch keine zentralen Einträge vom Admin.

+ )} + {!loading && shared.length > 0 && filteredShared.length === 0 && ( +

Kein zentraler Treffer für „{q.trim()}“.

+ )} + {filteredShared.map((f) => ( +
+
{f.name_de}
+
für alle Nutzer{foodVisibilitySuffix(f.catalog_kind, f.visibility)}
+
+ ))} + + {(creating || editId) && ( + { setCreating(false); setEditId(null) }} + onSaved={(res) => { + setCreating(false) + setEditId(null) + afterSave(res) + }} + /> + )} +
+ ) +} diff --git a/frontend/src/components/NutritionFoodMap.jsx b/frontend/src/components/NutritionFoodMap.jsx index c4465d1..a26bed4 100644 --- a/frontend/src/components/NutritionFoodMap.jsx +++ b/frontend/src/components/NutritionFoodMap.jsx @@ -3,6 +3,8 @@ import { api } from '../utils/api' import FoodSearchModal from './FoodSearchModal' import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields' import FoodRecipeDialog from './FoodRecipeDialog' +import FoodEditDialog from './FoodEditDialog' +import { foodVisibilitySuffix } from '../utils/foodVisibility' const PERIODS = [ { days: 14, label: '14 Tage' }, @@ -127,7 +129,7 @@ function InlineCreate({ defaultName, disabled, allowRecipe, onCreateFood, onWant ) } -export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, onCreatedList }) { +export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, onCreatedList, onOpenMyFoods }) { const [unmapped, setUnmapped] = useState([]) const [learned, setLearned] = useState([]) const [recipes, setRecipes] = useState([]) @@ -144,6 +146,7 @@ export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, o const [q, setQ] = useState('') const [visible, setVisible] = useState(80) const [showLearned, setShowLearned] = useState(false) + const [editFoodId, setEditFoodId] = useState(null) const [importing, setImporting] = useState(false) const [busy, setBusy] = useState(false) const listRef = useRef(null) @@ -371,6 +374,7 @@ export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, o { const f = e.target.files?.[0]; e.target.value = ''; if (f) importLists(f) }} />
+ @@ -448,7 +452,9 @@ export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, o
→ {best.name_de} - {best.bls_code ? ` · ${best.bls_code}` : ' · manuell'} + + {best.bls_code ? ` · ${best.bls_code}` : foodVisibilitySuffix(best.catalog_kind, best.visibility)} +
))} @@ -510,15 +516,20 @@ export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, o {showLearned && learned.map((m) => ( -
+
{m.source_name_raw}
→ {m.food_name_de}{m.bls_code ? ` · ${m.bls_code}` : ''} - {m.catalog_kind !== 'official_bls' ? ' (manuell)' : ''} + {foodVisibilitySuffix(m.catalog_kind, m.visibility)}
- +
+ {m.catalog_kind === 'manual_user' && m.food_id && ( + + )} + +
))} @@ -558,6 +569,19 @@ export default function NutritionFoodMap({ onChanged, onMapped, onManageLists, o {recipeFor && ( setRecipeFor(null)} onPick={(id) => applyRecipe(recipeFor.source_name_raw, id)} /> )} + {editFoodId && ( + setEditFoodId(null)} + onSaved={(res) => { + load() + onChanged?.() + const n = res?.rebuild_pending + if (n) setNotice(`Lebensmittel gespeichert. Tageswerte für ${n} Tag${n === 1 ? '' : 'e'} werden nachgerechnet.`) + else setNotice('Lebensmittel gespeichert.') + }} + /> + )}
) } diff --git a/frontend/src/config/adminNav.js b/frontend/src/config/adminNav.js index 3945ea0..d22533f 100644 --- a/frontend/src/config/adminNav.js +++ b/frontend/src/config/adminNav.js @@ -128,7 +128,7 @@ export const ADMIN_GROUPS = [ { to: '/admin/bls-foods', label: 'Lebensmittelkatalog', - description: 'Suche inkl. BLS-Code, manuelle Einträge.', + description: 'Suche (BLS) und Reiter Manuell für selbst angelegte Einträge.', }, { to: '/admin/food-attributes', diff --git a/frontend/src/pages/AdminBlsFoodsPage.jsx b/frontend/src/pages/AdminBlsFoodsPage.jsx index c2937e9..59dab86 100644 --- a/frontend/src/pages/AdminBlsFoodsPage.jsx +++ b/frontend/src/pages/AdminBlsFoodsPage.jsx @@ -1,43 +1,142 @@ -import { useState } from 'react' +import { useEffect, useMemo, useRef, useState } from 'react' import { api } from '../utils/api' +import FoodEditDialog from '../components/FoodEditDialog' export default function AdminBlsFoodsPage() { + const [tab, setTab] = useState('search') const [q, setQ] = useState('') + const [manualQ, setManualQ] = useState('') const [rows, setRows] = useState([]) + const [manualRows, setManualRows] = useState([]) const [detail, setDetail] = useState(null) const [error, setError] = useState(null) + const [loadingManual, setLoadingManual] = useState(false) + const [creating, setCreating] = useState(false) + const [editId, setEditId] = useState(null) + const timer = useRef(null) - const search = async () => { + const search = async (term) => { try { - setRows(await api.adminBlsFoods(q)) + setError(null) + setRows(await api.adminBlsFoods(term, '', 50)) } catch (e) { setError(e.message) } } + const loadManual = async (term) => { + setLoadingManual(true) + try { + setError(null) + setManualRows(await api.adminBlsFoods(term, 'manual', 200)) + } catch (e) { + setError(e.message) + } finally { + setLoadingManual(false) + } + } + + useEffect(() => { + if (tab === 'manual') loadManual(manualQ) + }, [tab]) + + useEffect(() => () => clearTimeout(timer.current), []) + + const onManualQuery = (value) => { + setManualQ(value) + clearTimeout(timer.current) + timer.current = setTimeout(() => loadManual(value), 300) + } + + const shownManual = useMemo(() => manualRows, [manualRows]) + + const openDetail = (id) => { + api.adminBlsFoodDetail(id).then(setDetail).catch((e) => setError(e.message)) + } + + const list = tab === 'manual' ? shownManual : rows + return (

Lebensmittelkatalog

{error &&

{error}

} -
- setQ(e.target.value)} placeholder="Name oder BLS-Code" /> - -
- {rows.map((r) => ( - + +
+ + {tab === 'search' && ( + <> +

+ BLS und manuelle Einträge nach Name oder Code. Die komplette BLS-Liste ist zu groß zum Durchblättern. +

+
+ setQ(e.target.value)} + onKeyDown={(e) => { if (e.key === 'Enter') search(q) }} + placeholder="Name oder BLS-Code" + /> + +
+ + )} + + {tab === 'manual' && ( + <> +

+ Zentral (für alle Nutzer) und persönlich (nur der jeweilige Nutzer). Zentrale Einträge hier anlegen; + persönliche bleiben beim Owner. +

+
+ +
+ onManualQuery(e.target.value)} + placeholder="In manuellen Einträgen suchen…" + /> +

+ {loadingManual ? 'Lade…' : `${shownManual.length} Einträge`} + {!loadingManual && shownManual.length >= 200 ? ' (Anzeige begrenzt auf 200)' : ''} +

+ + )} + + {tab === 'search' && rows.length === 0 && ( +

Suchbegriff eingeben und Suchen.

+ )} + {tab === 'manual' && !loadingManual && shownManual.length === 0 && ( +

Keine manuellen Lebensmittel{manualQ.trim() ? ` zu „${manualQ.trim()}“` : ''}.

+ )} + + {list.map((r) => ( +
+ + {r.catalog_kind === 'manual_admin' && ( + + )} +
))} {detail && (
-

{detail.bls_code} {detail.name_de}

+

{detail.bls_code ? `${detail.bls_code} · ` : ''}{detail.name_de}

{detail.catalog_kind} · {detail.bls_version || '—'}

@@ -52,6 +151,18 @@ export default function AdminBlsFoodsPage() {
)} + {(creating || editId) && ( + { setCreating(false); setEditId(null) }} + onSaved={() => { + setCreating(false) + setEditId(null) + if (tab === 'manual') loadManual(manualQ) + }} + /> + )}
) } diff --git a/frontend/src/pages/AdminFoodMappingsPage.jsx b/frontend/src/pages/AdminFoodMappingsPage.jsx index 9b7d45f..5598ac1 100644 --- a/frontend/src/pages/AdminFoodMappingsPage.jsx +++ b/frontend/src/pages/AdminFoodMappingsPage.jsx @@ -1,5 +1,6 @@ import { useEffect, useRef, useState } from 'react' import { api } from '../utils/api' +import { foodVisibilityLabel } from '../utils/foodVisibility' export default function AdminFoodMappingsPage() { const [rows, setRows] = useState([]) @@ -30,7 +31,7 @@ export default function AdminFoodMappingsPage() { setter([]) return } - api.searchBlsFoods(q, 20).then(setter).catch(() => setter([])) + api.searchBlsFoods(q, 20, undefined, 'global').then(setter).catch(() => setter([])) } const shown = rows.filter((r) => { @@ -59,6 +60,10 @@ export default function AdminFoodMappingsPage() {

FDDB→BLS Mappings

{error &&

{error}

} +

+ Globale Zuordnungen gelten für alle Nutzer und dürfen nur auf BLS oder zentrale Lebensmittel zeigen. + Persönliche Nutzer-Mappings bleiben beim jeweiligen Profil. +

{coverage && (

{coverage.mapped_items}/{coverage.total_items} Zeilen gemappt · @@ -77,7 +82,7 @@ export default function AdminFoodMappingsPage() {

{hits.map((h) => ( ))} ))} diff --git a/frontend/src/pages/NutritionPage.jsx b/frontend/src/pages/NutritionPage.jsx index e26d179..6d1b3c8 100644 --- a/frontend/src/pages/NutritionPage.jsx +++ b/frontend/src/pages/NutritionPage.jsx @@ -3,6 +3,7 @@ import { Upload, CheckCircle, TrendingUp, Info } from 'lucide-react' import UsageBadge from '../components/UsageBadge' import NutritionFoodMap, { DayMarkButtons } from '../components/NutritionFoodMap' import FoodRecipeEditor from '../components/FoodRecipeEditor' +import MyFoodsPanel from '../components/MyFoodsPanel' import { LineChart, Line, BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid, Legend, ReferenceLine, ScatterChart, Scatter @@ -957,7 +958,7 @@ export default function NutritionPage() { )} {/* Input Method Tabs */} -
+
@@ -967,6 +968,9 @@ export default function NutritionPage() { + @@ -992,9 +996,12 @@ export default function NutritionPage() { onChanged={refreshUnmappedCount} onManageLists={(id) => { setFocusListId(id || null); setInputTab('lists') }} onCreatedList={(rec) => { setFocusListId(rec?.id || null); setInputTab('lists') }} + onOpenMyFoods={() => setInputTab('foods')} /> )} + {inputTab==='foods' && } + {inputTab==='lists' && ( )} - {loading && inputTab !== 'map' && inputTab !== 'lists' &&
} + {loading && inputTab !== 'map' && inputTab !== 'lists' && inputTab !== 'foods' &&
} - {!loading && !hasData && inputTab !== 'map' && inputTab !== 'lists' && ( + {!loading && !hasData && inputTab !== 'map' && inputTab !== 'lists' && inputTab !== 'foods' && (

Noch keine Ernährungsdaten

Erfasse Daten über Einzelerfassung oder importiere deinen FDDB-Export.

@@ -1012,7 +1019,7 @@ export default function NutritionPage() { )} {/* Analysis Section */} - {!loading && hasData && inputTab !== 'map' && inputTab !== 'lists' && ( + {!loading && hasData && inputTab !== 'map' && inputTab !== 'lists' && inputTab !== 'foods' && ( <> diff --git a/frontend/src/utils/api.js b/frontend/src/utils/api.js index 8761f9d..f1cd94a 100644 --- a/frontend/src/utils/api.js +++ b/frontend/src/utils/api.js @@ -285,9 +285,13 @@ export const api = { deleteNutritionDayMark: (date) => req(`/nutrition/days/${date}/mark`, {method:'DELETE'}), resolveNutritionConflicts: (decisions) => req('/nutrition/import-conflicts/resolve', json({decisions})), listFoodUnits: () => req('/bls/units'), - searchBlsFoods: (q, limit=20, signal) => req(`/bls/foods?q=${encodeURIComponent(q||'')}&limit=${limit}`, signal ? { signal } : {}), + searchBlsFoods: (q, limit=20, signal, scope) => req(`/bls/foods?q=${encodeURIComponent(q||'')}&limit=${limit}${scope?`&scope=${encodeURIComponent(scope)}`:''}`, signal ? { signal } : {}), suggestFoodsBatch: (names, limit=3) => req('/bls/foods/suggest-batch', json({ names, limit })), createUserFood: (d) => req('/bls/foods/manual', json(d)), + listMyFoods: () => req('/bls/foods/mine'), + listSharedFoods: () => req('/bls/foods/shared'), + getBlsFood: (id) => req(`/bls/foods/${encodeURIComponent(id)}`), + updateUserFood: (id, d) => req(`/bls/foods/manual/${encodeURIComponent(id)}`, jput(d)), listFoodAttributes: (q='', limit=40, keys='') => req(`/bls/attributes?q=${encodeURIComponent(q||'')}&limit=${limit}${keys ? `&keys=${encodeURIComponent(keys)}` : ''}`), createFoodAttribute: (d) => req('/bls/attributes', json(d)), listMyFoodMappings: () => req('/bls/mappings'), @@ -301,9 +305,10 @@ export const api = { }, adminBlsImportJob: (id) => req(`/admin/bls/import/jobs/${id}`), adminBlsImportApply: (id) => req(`/admin/bls/import/jobs/${id}/apply`, {method:'POST'}), - adminBlsFoods: (q, kind) => req(`/admin/bls/foods?${q?('q='+encodeURIComponent(q)+'&'):''}${kind?('kind='+kind):''}`), + adminBlsFoods: (q, kind, limit) => req(`/admin/bls/foods?${q?('q='+encodeURIComponent(q)+'&'):''}${kind?('kind='+encodeURIComponent(kind)+'&'):''}${limit?('limit='+limit):''}`), adminBlsFoodDetail: (id) => req(`/admin/bls/foods/${id}`), adminCreateManualFood: (d) => req('/admin/bls/foods/manual', json(d)), + adminUpdateManualFood: (id, d) => req(`/admin/bls/foods/manual/${encodeURIComponent(id)}`, jput(d)), adminBlsAttributes: () => req('/admin/bls/attributes'), adminCreateBlsAttribute: (d) => req('/admin/bls/attributes', json(d)), adminListFoodMappings: (globalOnly) => req(`/admin/food-mappings${globalOnly?'?global_only=true':''}`), diff --git a/frontend/src/utils/foodVisibility.js b/frontend/src/utils/foodVisibility.js new file mode 100644 index 0000000..b56f2de --- /dev/null +++ b/frontend/src/utils/foodVisibility.js @@ -0,0 +1,23 @@ +const LABELS = { + personal: 'persönlich', + global: 'zentral', + bls: 'BLS', +} + +export function foodVisibility(kind, visibility) { + if (visibility && LABELS[visibility]) return visibility + if (kind === 'official_bls') return 'bls' + if (kind === 'manual_admin') return 'global' + if (kind === 'manual_user') return 'personal' + return '' +} + +export function foodVisibilityLabel(kind, visibility) { + const key = foodVisibility(kind, visibility) + return LABELS[key] || '' +} + +export function foodVisibilitySuffix(kind, visibility) { + const label = foodVisibilityLabel(kind, visibility) + return label ? ` · ${label}` : '' +}