From 6c3f1f83e6f61d8a4e76b12c6e1835ac089451c9 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 7 Nov 2025 08:51:26 +0000 Subject: [PATCH] Fix unit normalization and max_stock_level validation errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Issues:** 1. ❌ 422 Error: unit_of_measure "L" (uppercase) invalid Backend requires: 'kg', 'g', 'l', 'ml', 'units', 'pcs', 'pkg', 'bags', 'boxes' 2. ❌ 422 Error: max_stock_level must be > 0 Input: 0 when stock_quantity = 0 **Root Causes:** 1. AI suggestions return unit_of_measure with mixed case (e.g., "L" vs "l") 2. When stock_quantity = 0, max_stock_level = 0 * 2 = 0 (fails validation) **Solutions:** **1. Unit Normalization (Line 459)** ```typescript // Before: unit_of_measure: item.unit_of_measure // Could be "L", "Ml", etc. // After: const normalizedUnit = item.unit_of_measure.toLowerCase(); unit_of_measure: normalizedUnit // Always "l", "ml", etc. ``` **2. Max Stock Level Validation (Line 462)** ```typescript // Before: max_stock_level: item.stock_quantity * 2 // Could be 0 // After: const maxStockLevel = Math.max(1, item.stock_quantity * 2); max_stock_level: maxStockLevel // Always >= 1 ``` **Changes:** - frontend/src/components/domain/onboarding/steps/UploadSalesDataStep.tsx:458-470 **Examples:** ```typescript // Stock = 50, Unit = "L" normalizedUnit = "l" maxStockLevel = max(1, 50 * 2) = 100 ✅ // Stock = 0, Unit = "kg" normalizedUnit = "kg" maxStockLevel = max(1, 0 * 2) = 1 ✅ // Stock = 10, Unit = "ML" normalizedUnit = "ml" maxStockLevel = max(1, 10 * 2) = 20 ✅ ``` **Impact:** ✅ All units normalized to lowercase before API call ✅ max_stock_level always >= 1 ✅ No more 422 validation errors ✅ Works with AI suggestions of any case **Build Status:** ✓ Successful in 21.70s --- .../domain/onboarding/steps/UploadSalesDataStep.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/domain/onboarding/steps/UploadSalesDataStep.tsx b/frontend/src/components/domain/onboarding/steps/UploadSalesDataStep.tsx index 59d43cce..646a6f4b 100644 --- a/frontend/src/components/domain/onboarding/steps/UploadSalesDataStep.tsx +++ b/frontend/src/components/domain/onboarding/steps/UploadSalesDataStep.tsx @@ -455,13 +455,19 @@ export const UploadSalesDataStep: React.FC = ({ try { // Create all ingredients in parallel const creationPromises = inventoryItems.map(item => { + // Normalize unit to lowercase (backend requires lowercase) + const normalizedUnit = item.unit_of_measure.toLowerCase(); + + // Ensure max_stock_level is at least 1 (backend validation requires > 0) + const maxStockLevel = Math.max(1, item.stock_quantity * 2); + const ingredientData = { name: item.name, product_type: item.product_type, category: item.category, - unit_of_measure: item.unit_of_measure, + unit_of_measure: normalizedUnit, low_stock_threshold: item.low_stock_threshold, - max_stock_level: item.stock_quantity * 2, + max_stock_level: maxStockLevel, reorder_point: item.reorder_point, shelf_life_days: item.estimated_shelf_life_days, requires_refrigeration: item.requires_refrigeration,