refactor: Remove TEXT fields and use only reasoning_data for i18n

Completed the migration to structured reasoning_data for multilingual
dashboard support. Removed hardcoded TEXT fields (reasoning, consequence)
and updated all related code to use JSONB reasoning_data.

Changes:

1. Models Updated (removed TEXT fields):
   - PurchaseOrder: Removed reasoning, consequence TEXT columns
   - ProductionBatch: Removed reasoning TEXT column
   - Both now use only reasoning_data (JSONB/JSON)

2. Dashboard Service Updated:
   - Changed to return reasoning_data instead of TEXT fields
   - Creates default reasoning_data if missing
   - PO actions: reasoning_data with type and parameters
   - Production timeline: reasoning_data for each batch

3. Unified Schemas Updated (no separate migration):
   - services/procurement/migrations/001_unified_initial_schema.py
   - services/production/migrations/001_unified_initial_schema.py
   - Removed reasoning/consequence columns from table definitions
   - Updated comments to reflect i18n approach

Database Schema:
- purchase_orders: Only reasoning_data (JSONB)
- production_batches: Only reasoning_data (JSON)

Backend now generates:
{
  "type": "low_stock_detection",
  "parameters": {
    "supplier_name": "Harinas del Norte",
    "days_until_stockout": 3,
    ...
  },
  "consequence": {
    "type": "stockout_risk",
    "severity": "high"
  }
}

Next Steps:
- Frontend: Create i18n translation keys
- Frontend: Update components to translate reasoning_data
- Test multilingual support (ES, EN, CA)
This commit is contained in:
Claude
2025-11-07 18:20:05 +00:00
parent ddc4928d78
commit f74b8d5402
5 changed files with 75 additions and 34 deletions

View File

@@ -367,14 +367,23 @@ class DashboardService:
# Calculate urgency based on required delivery date
urgency = self._calculate_po_urgency(po)
# Get reasoning_data or create default
reasoning_data = po.get("reasoning_data") or {
"type": "low_stock_detection",
"parameters": {
"supplier_name": po.get('supplier_name', 'Unknown'),
"product_names": ["Items"],
"days_until_stockout": 7
}
}
actions.append({
"id": po["id"],
"type": ActionType.APPROVE_PO,
"urgency": urgency,
"title": f"Purchase Order {po.get('po_number', 'N/A')}",
"subtitle": f"Supplier: {po.get('supplier_name', 'Unknown')}",
"reasoning": po.get("reasoning") or "Low stock levels detected",
"consequence": po.get("consequence") or "Order needed to maintain inventory levels",
"reasoning_data": reasoning_data, # NEW: Structured data for i18n
"amount": po.get("total_amount", 0),
"currency": po.get("currency", "EUR"),
"actions": [
@@ -490,6 +499,16 @@ class DashboardService:
status_icon = ""
status_text = "PENDING"
# Get reasoning_data or create default
reasoning_data = batch.get("reasoning_data") or {
"type": "forecast_demand",
"parameters": {
"product_name": batch.get("product_name", "Product"),
"predicted_demand": batch.get("planned_quantity", 0),
"confidence_score": 85
}
}
timeline.append({
"id": batch["id"],
"batchNumber": batch.get("batch_number"),
@@ -505,7 +524,7 @@ class DashboardService:
"progress": progress,
"readyBy": planned_end.isoformat() if planned_end else None,
"priority": batch.get("priority", "MEDIUM"),
"reasoning": batch.get("reasoning") or "Based on demand forecast"
"reasoning_data": reasoning_data # NEW: Structured data for i18n
})
# Sort by planned start time