Fix unit normalization and max_stock_level validation errors

**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
This commit is contained in:
Claude
2025-11-07 08:51:26 +00:00
parent bedd4868ac
commit 6c3f1f83e6

View File

@@ -455,13 +455,19 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
try { try {
// Create all ingredients in parallel // Create all ingredients in parallel
const creationPromises = inventoryItems.map(item => { 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 = { const ingredientData = {
name: item.name, name: item.name,
product_type: item.product_type, product_type: item.product_type,
category: item.category, category: item.category,
unit_of_measure: item.unit_of_measure, unit_of_measure: normalizedUnit,
low_stock_threshold: item.low_stock_threshold, low_stock_threshold: item.low_stock_threshold,
max_stock_level: item.stock_quantity * 2, max_stock_level: maxStockLevel,
reorder_point: item.reorder_point, reorder_point: item.reorder_point,
shelf_life_days: item.estimated_shelf_life_days, shelf_life_days: item.estimated_shelf_life_days,
requires_refrigeration: item.requires_refrigeration, requires_refrigeration: item.requires_refrigeration,