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

@@ -157,12 +157,6 @@ async def clone_demo_data_internal(
else:
session_created_at_parsed = datetime.now(timezone.utc)
# Determine profile based on demo_account_type
if demo_account_type == "enterprise":
profile = "enterprise"
else:
profile = "professional"
logger.info(
"Starting inventory data cloning with date adjustment",
base_tenant_id=base_tenant_id,
@@ -172,32 +166,17 @@ async def clone_demo_data_internal(
session_time=session_created_at_parsed.isoformat()
)
# Load seed data using shared utility
try:
from shared.utils.seed_data_paths import get_seed_data_path
if profile == "professional":
json_file = get_seed_data_path("professional", "03-inventory.json")
elif profile == "enterprise":
json_file = get_seed_data_path("enterprise", "03-inventory.json")
else:
raise ValueError(f"Invalid profile: {profile}")
# 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 profile == "professional":
json_file = seed_data_dir / "professional" / "03-inventory.json"
elif profile == "enterprise":
json_file = seed_data_dir / "enterprise" / "parent" / "03-inventory.json"
else:
raise ValueError(f"Invalid profile: {profile}")
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", "03-inventory.json")
elif demo_account_type == "enterprise":
json_file = get_seed_data_path("enterprise", "03-inventory.json")
elif demo_account_type == "enterprise_child":
json_file = get_seed_data_path("enterprise", "03-inventory.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:
@@ -223,7 +202,7 @@ async def clone_demo_data_internal(
# Transform and insert data
records_cloned = 0
# Clone ingredients
for ingredient_data in seed_data.get('ingredients', []):
# Transform ID
@@ -241,7 +220,7 @@ async def clone_demo_data_internal(
status_code=400,
detail=f"Invalid UUID format in ingredient data: {str(e)}"
)
# Transform dates using standardized helper
ingredient_data['created_at'] = parse_date_field(
ingredient_data.get('created_at'), session_time, 'created_at'
@@ -249,7 +228,7 @@ async def clone_demo_data_internal(
ingredient_data['updated_at'] = parse_date_field(
ingredient_data.get('updated_at'), session_time, 'updated_at'
) or session_time
# Map category field to ingredient_category enum
if 'category' in ingredient_data:
category_value = ingredient_data.pop('category')
@@ -260,7 +239,7 @@ async def clone_demo_data_internal(
except KeyError:
# If category not found in enum, use OTHER
ingredient_data['ingredient_category'] = IngredientCategory.OTHER
# Map unit_of_measure string to enum
if 'unit_of_measure' in ingredient_data:
from app.models.inventory import UnitOfMeasure
@@ -297,14 +276,14 @@ async def clone_demo_data_internal(
ingredient_data['unit_of_measure'] = UnitOfMeasure.UNITS
logger.warning("Unknown unit_of_measure, defaulting to UNITS",
original_unit=unit_str)
# Note: All seed data fields now match the model schema exactly
# No field filtering needed
# Remove original id and tenant_id from ingredient_data to avoid conflict
ingredient_data.pop('id', None)
ingredient_data.pop('tenant_id', None)
# Create ingredient
ingredient = Ingredient(
id=str(transformed_id),
@@ -314,6 +293,9 @@ async def clone_demo_data_internal(
db.add(ingredient)
records_cloned += 1
# Commit ingredients before creating stock to ensure foreign key references exist
await db.flush() # Use flush instead of commit to maintain transaction while continuing
# Clone stock batches
for stock_data in seed_data.get('stock', []):
# Transform ID - handle both UUID and string IDs