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

@@ -133,16 +133,29 @@ class ProductionBatch(Base):
delay_reason = Column(String(255), nullable=True)
cancellation_reason = Column(String(255), nullable=True)
# JTBD Dashboard: Reasoning and context for user transparency
# Deferred loading to prevent breaking queries when columns don't exist yet
reasoning = deferred(Column(Text, nullable=True)) # Why this batch was scheduled (e.g., "Based on wedding order #1234")
reasoning_data = deferred(Column(JSON, nullable=True)) # Structured reasoning data
# reasoning_data structure: {
# "trigger": "forecast" | "order" | "inventory" | "manual",
# "forecast_id": "uuid",
# "orders_fulfilled": [{"id": "uuid", "customer": "Maria's Bakery", "quantity": 100}],
# "demand_score": 0-100,
# "scheduling_priority_reason": "High demand + VIP customer"
# JTBD Dashboard: Structured reasoning data for i18n support
# Backend stores structured data, frontend translates using i18n
reasoning_data = Column(JSON, nullable=True) # Structured reasoning data for multilingual support
# reasoning_data structure (see shared/schemas/reasoning_types.py):
# {
# "type": "forecast_demand" | "customer_order" | "stock_replenishment" | etc.,
# "parameters": {
# "product_name": "Croissant",
# "predicted_demand": 500,
# "current_stock": 120,
# "production_needed": 380,
# "confidence_score": 87
# },
# "urgency": {
# "level": "normal",
# "ready_by_time": "08:00",
# "customer_commitment": false
# },
# "metadata": {
# "trigger_source": "orchestrator_auto",
# "forecast_id": "uuid-here",
# "ai_assisted": true
# }
# }
# Timestamps
@@ -191,7 +204,6 @@ class ProductionBatch(Base):
"quality_notes": self.quality_notes,
"delay_reason": self.delay_reason,
"cancellation_reason": self.cancellation_reason,
"reasoning": self.reasoning,
"reasoning_data": self.reasoning_data,
"created_at": self.created_at.isoformat() if self.created_at else None,
"updated_at": self.updated_at.isoformat() if self.updated_at else None,