Make backend robust with comprehensive onboarding steps
Backend Changes (services/auth/app/api/onboarding_progress.py): - Expanded ONBOARDING_STEPS to include all 19 frontend steps - Phase 0: user_registered (system) - Phase 1: bakery-type-selection, data-source-choice (discovery) - Phase 2: setup, smart-inventory-setup, product-categorization, initial-stock-entry (core setup & AI path) - Phase 2b: suppliers-setup, inventory-setup, recipes-setup, production-processes (manual path) - Phase 3: quality-setup, team-setup (advanced config) - Phase 4: ml-training, setup-review, completion (finalization) - Updated STEP_DEPENDENCIES with granular requirements - AI path: smart-inventory-setup → product-categorization → initial-stock-entry - Manual path: Independent setup for suppliers, inventory, recipes, processes - Flexible ML training: accepts either AI or manual inventory path - Enhanced ML training validation - Supports both AI-assisted path (sales data) and manual inventory path - More flexible validation logic for multi-path onboarding Frontend Changes (UnifiedOnboardingWizard.tsx): - Fixed auto-complete step name: 'suppliers' → 'suppliers-setup' - All step IDs now match backend ONBOARDING_STEPS exactly - Removed temporary step mapping workarounds Frontend Changes (apiClient.ts): - Fixed tenant ID requirement warnings for onboarding endpoints - Added noTenantEndpoints list for user-level endpoints: - /auth/me/onboarding (tenant created during onboarding) - /auth/me (user profile) - /auth/register, /auth/login - Eliminated false warnings during onboarding flow This makes the onboarding system fully functional with: ✅ Backend validates all 19 onboarding steps ✅ Proper dependency tracking for multi-path onboarding ✅ No more "Invalid step name" errors ✅ No more tenant ID warnings for onboarding ✅ Robust state tracking for complete user journey
This commit is contained in:
@@ -38,22 +38,69 @@ class UpdateStepRequest(BaseModel):
|
||||
completed: bool
|
||||
data: Optional[Dict[str, Any]] = None
|
||||
|
||||
# Define the onboarding steps and their order - matching frontend step IDs
|
||||
# Define the onboarding steps and their order - matching frontend UnifiedOnboardingWizard step IDs
|
||||
ONBOARDING_STEPS = [
|
||||
"user_registered", # Auto-completed: User account created
|
||||
"setup", # Step 1: Basic bakery setup and tenant creation
|
||||
"smart-inventory-setup", # Step 2: Sales data upload and inventory configuration
|
||||
"suppliers", # Step 3: Suppliers configuration (optional)
|
||||
"ml-training", # Step 4: AI model training
|
||||
"completion" # Step 5: Onboarding completed, ready to use dashboard
|
||||
# Phase 0: System Steps
|
||||
"user_registered", # Auto-completed: User account created
|
||||
|
||||
# Phase 1: Discovery
|
||||
"bakery-type-selection", # Choose bakery type: production/retail/mixed
|
||||
"data-source-choice", # Choose setup method: AI-assisted or manual
|
||||
|
||||
# Phase 2: Core Setup
|
||||
"setup", # Basic bakery setup and tenant creation
|
||||
|
||||
# Phase 2a: AI-Assisted Path
|
||||
"smart-inventory-setup", # Sales data upload and AI analysis
|
||||
"product-categorization", # Categorize products as ingredients vs finished products
|
||||
"initial-stock-entry", # Capture initial stock levels
|
||||
|
||||
# Phase 2b: Manual Setup Path
|
||||
"suppliers-setup", # Suppliers configuration
|
||||
"inventory-setup", # Manual inventory configuration
|
||||
"recipes-setup", # Production recipes (conditional: production/mixed bakery)
|
||||
"production-processes", # Finishing processes (conditional: retail/mixed bakery)
|
||||
|
||||
# Phase 3: Advanced Configuration
|
||||
"quality-setup", # Quality standards and templates
|
||||
"team-setup", # Team members and permissions
|
||||
|
||||
# Phase 4: ML & Finalization
|
||||
"ml-training", # AI model training
|
||||
"setup-review", # Review all configuration
|
||||
"completion" # Onboarding completed
|
||||
]
|
||||
|
||||
# Step dependencies - defines which steps must be completed before others
|
||||
# Steps not listed here have no dependencies (can be completed anytime after user_registered)
|
||||
STEP_DEPENDENCIES = {
|
||||
"setup": ["user_registered"],
|
||||
# Discovery phase
|
||||
"data-source-choice": ["user_registered", "bakery-type-selection"],
|
||||
|
||||
# Core setup
|
||||
"setup": ["user_registered", "data-source-choice"],
|
||||
|
||||
# AI-Assisted path dependencies
|
||||
"smart-inventory-setup": ["user_registered", "setup"],
|
||||
"suppliers": ["user_registered", "setup", "smart-inventory-setup"], # Optional step
|
||||
"ml-training": ["user_registered", "setup", "smart-inventory-setup"],
|
||||
"completion": ["user_registered", "setup", "smart-inventory-setup", "ml-training"]
|
||||
"product-categorization": ["user_registered", "setup", "smart-inventory-setup"],
|
||||
"initial-stock-entry": ["user_registered", "setup", "smart-inventory-setup", "product-categorization"],
|
||||
|
||||
# Manual path dependencies
|
||||
"suppliers-setup": ["user_registered", "setup"],
|
||||
"inventory-setup": ["user_registered", "setup"],
|
||||
"recipes-setup": ["user_registered", "setup"],
|
||||
"production-processes": ["user_registered", "setup"],
|
||||
|
||||
# Advanced configuration
|
||||
"quality-setup": ["user_registered", "setup"],
|
||||
"team-setup": ["user_registered", "setup"],
|
||||
|
||||
# ML Training - requires either AI path or manual inventory
|
||||
"ml-training": ["user_registered", "setup"], # Flexible: can work with either path
|
||||
|
||||
# Review and completion
|
||||
"setup-review": ["user_registered", "setup"],
|
||||
"completion": ["user_registered", "setup"] # Minimal requirements for completion
|
||||
}
|
||||
|
||||
class OnboardingService:
|
||||
@@ -233,27 +280,34 @@ class OnboardingService:
|
||||
|
||||
# SPECIAL VALIDATION FOR ML TRAINING STEP
|
||||
if step_name == "ml-training":
|
||||
# Ensure that smart-inventory-setup was completed with sales data imported
|
||||
smart_inventory_data = user_progress_data.get("smart-inventory-setup", {}).get("data", {})
|
||||
|
||||
# Check if sales data was imported successfully
|
||||
sales_import_result = smart_inventory_data.get("salesImportResult", {})
|
||||
has_sales_data_imported = (
|
||||
sales_import_result.get("records_created", 0) > 0 or
|
||||
sales_import_result.get("success", False) or
|
||||
sales_import_result.get("imported", False)
|
||||
)
|
||||
|
||||
if not has_sales_data_imported:
|
||||
logger.warning(f"ML training blocked for user {user_id}: No sales data imported",
|
||||
extra={"sales_import_result": sales_import_result})
|
||||
return False
|
||||
|
||||
# Also check if inventory is configured
|
||||
inventory_configured = smart_inventory_data.get("inventoryConfigured", False)
|
||||
if not inventory_configured:
|
||||
logger.warning(f"ML training blocked for user {user_id}: Inventory not configured")
|
||||
return False
|
||||
# ML training can work with either AI-assisted path or manual inventory path
|
||||
# Check if user has data through either path
|
||||
|
||||
ai_path_complete = user_progress_data.get("smart-inventory-setup", {}).get("completed", False)
|
||||
manual_path_complete = user_progress_data.get("inventory-setup", {}).get("completed", False)
|
||||
|
||||
if ai_path_complete:
|
||||
# AI path: validate sales data was imported
|
||||
smart_inventory_data = user_progress_data.get("smart-inventory-setup", {}).get("data", {})
|
||||
sales_import_result = smart_inventory_data.get("salesImportResult", {})
|
||||
has_sales_data_imported = (
|
||||
sales_import_result.get("records_created", 0) > 0 or
|
||||
sales_import_result.get("success", False) or
|
||||
sales_import_result.get("imported", False)
|
||||
)
|
||||
|
||||
if has_sales_data_imported:
|
||||
logger.info(f"ML training allowed for user {user_id}: AI path with sales data")
|
||||
return True
|
||||
|
||||
if manual_path_complete:
|
||||
# Manual path: just check if inventory setup was completed
|
||||
logger.info(f"ML training allowed for user {user_id}: Manual inventory path")
|
||||
return True
|
||||
|
||||
# Neither path is complete
|
||||
logger.warning(f"ML training blocked for user {user_id}: No inventory data (AI or manual)")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user