Sfoglia il codice sorgente

fix: US-08 Expand USDA nutrient mapping to include micros and re-seed database

FerRo988 2 settimane fa
parent
commit
7649d41f92
2 ha cambiato i file con 51 aggiunte e 3 eliminazioni
  1. 34 0
      documentation/Sprint6_DetailedNutrition_Decisions.md
  2. 17 3
      mega_seed_usda.py

+ 34 - 0
documentation/Sprint6_DetailedNutrition_Decisions.md

@@ -0,0 +1,34 @@
+# Sprint 6: US-08 Detailed Nutritional Information — Technical Decisions
+
+## Overview
+Sprint 6 (User Story #08 / US-08) enhances the food search results by providing a structured, in-depth nutritional breakdown for every food item. It builds upon the macro dashboard by adding vitamins, minerals, and extended macronutrient data.
+
+---
+
+## Task #41: Structured Food Detail API
+
+### What we chose
+- **Granular endpoint:** `GET /api/food/{food_id}`
+  - *Reasoning:* Instead of sending all detailed data for every search result (which would bloat the search payload), we fetch the full profile only when the user specifically requests it.
+- **Categorized JSON Structure:**
+  - Nutrients are grouped into `macros`, `extended`, `vitamins`, and `minerals`.
+  - *Reasoning:* Makes it easier for the frontend to render distinct "sections" of a nutrition label rather than a flat list of 15+ keys.
+- **Data Integrity Fix:**
+  - Expanded the `NUTRIENT_MAP` in `mega_seed_usda.py` to include Calcium (301), Iron (303), Potassium (306), Vitamin A (318), Vitamin C (401), and Cholesterol (601).
+  - Re-seeded the database with 7,793 items to ensure all micronutrient columns are populated correctly.
+- **Backend security:** The endpoint requires a valid JWT Bearer token, ensuring food data is only accessible to authenticated users.
+
+---
+
+## Task #40: Expandable Detail View UI
+
+### What we chose
+- **On-demand rendering:** The detailed panel is empty by default and populated via the API only on the first "Details" click.
+- **Visual Feedback:** A loading state is shown during the API fetch to ensure a responsive feel.
+- **Grid-based Layout:** Used a CSS Grid (`grid-template-columns: repeat(2, 1fr)`) for the nutrient lists.
+  - *Reasoning:* Maximizes vertical space and mimics the look of a traditional nutrition facts panel while remaining readable on mobile.
+- **Independent Click Targets:**
+  - Click Name/Header -> Auto-fill chat (Fast Action).
+  - Click "Details" -> Expand/Collapse (Investigation Action).
+  - *Reasoning:* Preserves the fast "Add to Meal" workflow while allowing deep-dives.
+- **Smooth Transition:** `max-height` transitions combined with glassmorphism effects maintain the premium aesthetic.

+ 17 - 3
mega_seed_usda.py

@@ -15,7 +15,13 @@ NUTRIENT_MAP = {
     '205': 'carbs_g',
     '291': 'fiber_g',
     '269': 'sugar_g',
-    '307': 'sodium_mg'
+    '307': 'sodium_mg',
+    '301': 'calcium_mg',
+    '303': 'iron_mg',
+    '306': 'potassium_mg',
+    '318': 'vitamin_a_iu',
+    '401': 'vitamin_c_mg',
+    '601': 'cholesterol_mg'
 }
 
 def parse_usda_line(line):
@@ -86,8 +92,10 @@ def run_seeding():
         
         cursor.execute('''
             INSERT INTO foods (
-                name, category, calories, protein_g, fat_g, carbs_g, fiber_g, sugar_g, sodium_mg, source
-            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+                name, category, calories, protein_g, fat_g, carbs_g, fiber_g, sugar_g, 
+                sodium_mg, calcium_mg, iron_mg, potassium_mg, vitamin_a_iu, vitamin_c_mg, 
+                cholesterol_mg, source
+            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
         ''', (
             info['name'], 
             info['category'],
@@ -98,6 +106,12 @@ def run_seeding():
             macros.get('fiber_g', 0.0),
             macros.get('sugar_g', 0.0),
             macros.get('sodium_mg', 0.0),
+            macros.get('calcium_mg', 0.0),
+            macros.get('iron_mg', 0.0),
+            macros.get('potassium_mg', 0.0),
+            macros.get('vitamin_a_iu', 0.0),
+            macros.get('vitamin_c_mg', 0.0),
+            macros.get('cholesterol_mg', 0.0),
             f"USDA-{ndb_no}"
         ))
         count += 1