Fix Demo enterprise

This commit is contained in:
Urtzi Alfaro
2025-12-17 13:03:52 +01:00
parent 0bbfa010bf
commit 8bfe4f2dd7
111 changed files with 26200 additions and 2245 deletions

View File

@@ -159,32 +159,17 @@ async def clone_demo_data(
"sales_records": 0,
}
# Load seed data from JSON files instead of cloning from database
try:
from shared.utils.seed_data_paths import get_seed_data_path
if demo_account_type == "professional":
json_file = get_seed_data_path("professional", "09-sales.json")
elif demo_account_type == "enterprise":
json_file = get_seed_data_path("enterprise", "09-sales.json")
else:
raise ValueError(f"Invalid demo account type: {demo_account_type}")
# Load seed data from JSON files
from shared.utils.seed_data_paths import get_seed_data_path
except ImportError:
# Fallback to original path
seed_data_dir = Path(__file__).parent.parent.parent.parent / "infrastructure" / "seed-data"
if demo_account_type == "professional":
json_file = seed_data_dir / "professional" / "09-sales.json"
elif demo_account_type == "enterprise":
json_file = seed_data_dir / "enterprise" / "parent" / "09-sales.json"
else:
raise ValueError(f"Invalid demo account type: {demo_account_type}")
if not json_file.exists():
raise HTTPException(
status_code=404,
detail=f"Seed data file not found: {json_file}"
)
if demo_account_type == "professional":
json_file = get_seed_data_path("professional", "09-sales.json")
elif demo_account_type == "enterprise":
json_file = get_seed_data_path("enterprise", "09-sales.json")
elif demo_account_type == "enterprise_child":
json_file = get_seed_data_path("enterprise", "09-sales.json", child_id=base_tenant_id)
else:
raise ValueError(f"Invalid demo account type: {demo_account_type}")
# Load JSON data
with open(json_file, 'r', encoding='utf-8') as f:
@@ -198,25 +183,36 @@ async def clone_demo_data(
# Load Sales Data from seed data
for sale_data in seed_data.get('sales_data', []):
# Parse date field (supports BASE_TS markers and ISO timestamps)
# Different demo types may use different field names for the date
# Prioritize in order: date, sale_date, sales_date
date_value = (sale_data.get('date') or
sale_data.get('sale_date') or
sale_data.get('sales_date'))
adjusted_date = parse_date_field(
sale_data.get('sales_date'),
date_value,
session_time,
"sales_date"
"date"
)
# Ensure date is not None for NOT NULL constraint by using session_time as fallback
if adjusted_date is None:
adjusted_date = session_time
# Create new sales record with adjusted date
# Map different possible JSON field names to the correct model field names
new_sale = SalesData(
id=uuid.uuid4(),
tenant_id=virtual_uuid,
date=adjusted_date,
inventory_product_id=sale_data.get('product_id'), # Use product_id from seed data
quantity_sold=sale_data.get('quantity', 0.0), # Map quantity to quantity_sold
unit_price=sale_data.get('unit_price', 0.0),
revenue=sale_data.get('total_amount', 0.0), # Map total_amount to revenue
cost_of_goods=sale_data.get('cost_of_goods', 0.0),
discount_applied=sale_data.get('discount_applied', 0.0),
inventory_product_id=sale_data.get('inventory_product_id') or sale_data.get('product_id'), # inventory_product_id is the model field
quantity_sold=sale_data.get('quantity_sold') or sale_data.get('quantity', 0.0), # quantity_sold is the model field
unit_price=sale_data.get('unit_price', 0.0), # unit_price is the model field
revenue=sale_data.get('revenue') or sale_data.get('total_revenue') or sale_data.get('total_amount', 0.0), # revenue is the model field
cost_of_goods=sale_data.get('cost_of_goods', 0.0), # cost_of_goods is the model field
discount_applied=sale_data.get('discount_applied', 0.0), # discount_applied is the model field
location_id=sale_data.get('location_id'),
sales_channel=sale_data.get('sales_channel', 'IN_STORE'),
sales_channel=sale_data.get('sales_channel', 'IN_STORE'), # sales_channel is the model field
source="demo_clone", # Mark as seeded
is_validated=sale_data.get('is_validated', True),
validation_notes=sale_data.get('validation_notes'),