demo seed change
This commit is contained in:
@@ -13,6 +13,7 @@ from typing import Optional
|
||||
import os
|
||||
from decimal import Decimal
|
||||
import sys
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
|
||||
@@ -24,7 +25,7 @@ from app.models.sales import SalesData
|
||||
from app.core.config import settings
|
||||
|
||||
logger = structlog.get_logger()
|
||||
router = APIRouter(prefix="/internal/demo", tags=["internal"])
|
||||
router = APIRouter()
|
||||
|
||||
# Base demo tenant IDs
|
||||
DEMO_TENANT_PROFESSIONAL = "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"
|
||||
@@ -38,7 +39,7 @@ def verify_internal_api_key(x_internal_api_key: Optional[str] = Header(None)):
|
||||
return True
|
||||
|
||||
|
||||
@router.post("/clone")
|
||||
@router.post("/internal/demo/clone")
|
||||
async def clone_demo_data(
|
||||
base_tenant_id: str,
|
||||
virtual_tenant_id: str,
|
||||
@@ -102,46 +103,71 @@ async def clone_demo_data(
|
||||
"sales_records": 0,
|
||||
}
|
||||
|
||||
# Clone Sales Data
|
||||
result = await db.execute(
|
||||
select(SalesData).where(SalesData.tenant_id == base_uuid)
|
||||
)
|
||||
base_sales = result.scalars().all()
|
||||
# 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}")
|
||||
|
||||
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}"
|
||||
)
|
||||
|
||||
# Load JSON data
|
||||
with open(json_file, 'r', encoding='utf-8') as f:
|
||||
seed_data = json.load(f)
|
||||
|
||||
logger.info(
|
||||
"Found sales records to clone",
|
||||
count=len(base_sales),
|
||||
base_tenant=str(base_uuid)
|
||||
"Loaded sales seed data",
|
||||
sales_records=len(seed_data.get('sales_data', []))
|
||||
)
|
||||
|
||||
for sale in base_sales:
|
||||
# Load Sales Data from seed data
|
||||
for sale_data in seed_data.get('sales_data', []):
|
||||
# Adjust date using the shared utility
|
||||
adjusted_date = adjust_date_for_demo(
|
||||
sale.date,
|
||||
datetime.fromisoformat(sale_data['sale_date'].replace('Z', '+00:00')),
|
||||
session_time,
|
||||
BASE_REFERENCE_DATE
|
||||
) if sale.date else None
|
||||
) if sale_data.get('sale_date') else None
|
||||
|
||||
# Create new sales record with adjusted date
|
||||
new_sale = SalesData(
|
||||
id=uuid.uuid4(),
|
||||
tenant_id=virtual_uuid,
|
||||
date=adjusted_date,
|
||||
inventory_product_id=sale.inventory_product_id, # Keep same product refs
|
||||
quantity_sold=sale.quantity_sold,
|
||||
unit_price=sale.unit_price,
|
||||
revenue=sale.revenue,
|
||||
cost_of_goods=sale.cost_of_goods,
|
||||
discount_applied=sale.discount_applied,
|
||||
location_id=sale.location_id,
|
||||
sales_channel=sale.sales_channel,
|
||||
source="demo_clone", # Mark as cloned
|
||||
is_validated=sale.is_validated,
|
||||
validation_notes=sale.validation_notes,
|
||||
notes=sale.notes,
|
||||
weather_condition=sale.weather_condition,
|
||||
is_holiday=sale.is_holiday,
|
||||
is_weekend=sale.is_weekend,
|
||||
inventory_product_id=sale_data.get('product_id'), # Use product_id from seed data
|
||||
quantity_sold=sale_data.get('quantity_sold', 0.0),
|
||||
unit_price=sale_data.get('unit_price', 0.0),
|
||||
revenue=sale_data.get('total_revenue', 0.0),
|
||||
cost_of_goods=sale_data.get('cost_of_goods', 0.0),
|
||||
discount_applied=sale_data.get('discount_applied', 0.0),
|
||||
location_id=sale_data.get('location_id'),
|
||||
sales_channel=sale_data.get('sales_channel', 'IN_STORE'),
|
||||
source="demo_seed", # Mark as seeded
|
||||
is_validated=sale_data.get('is_validated', True),
|
||||
validation_notes=sale_data.get('validation_notes'),
|
||||
notes=sale_data.get('notes'),
|
||||
weather_condition=sale_data.get('weather_condition'),
|
||||
is_holiday=sale_data.get('is_holiday', False),
|
||||
is_weekend=sale_data.get('is_weekend', False),
|
||||
created_at=session_time,
|
||||
updated_at=session_time
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user