demo seed change

This commit is contained in:
Urtzi Alfaro
2025-12-13 23:57:54 +01:00
parent f3688dfb04
commit ff830a3415
299 changed files with 20328 additions and 19485 deletions

View File

@@ -98,127 +98,160 @@ async def clone_demo_data(
# Customer ID mapping (old -> new)
customer_id_map = {}
# Clone Customers
result = await db.execute(
select(Customer).where(Customer.tenant_id == base_uuid)
)
base_customers = result.scalars().all()
# Load Customers from seed data
try:
from shared.utils.seed_data_paths import get_seed_data_path
if demo_account_type == "professional":
json_file = get_seed_data_path("professional", "08-orders.json")
elif demo_account_type == "enterprise":
json_file = get_seed_data_path("enterprise", "08-orders.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" / "08-orders.json"
elif demo_account_type == "enterprise":
json_file = seed_data_dir / "enterprise" / "parent" / "08-orders.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 customers to clone",
count=len(base_customers),
base_tenant=str(base_uuid)
"Loaded orders seed data",
customers=len(seed_data.get('customers', [])),
orders=len(seed_data.get('orders', []))
)
for customer in base_customers:
new_customer_id = uuid.uuid4()
customer_id_map[customer.id] = new_customer_id
# Load Customers from seed data
for customer_data in seed_data.get('customers', []):
# Transform IDs using XOR
from shared.utils.demo_id_transformer import transform_id
try:
customer_uuid = uuid.UUID(customer_data['id'])
transformed_id = transform_id(customer_data['id'], virtual_uuid)
except ValueError as e:
logger.error("Failed to parse customer UUID",
customer_id=customer_data['id'],
error=str(e))
continue
customer_id_map[uuid.UUID(customer_data['id'])] = transformed_id
new_customer = Customer(
id=new_customer_id,
id=transformed_id,
tenant_id=virtual_uuid,
customer_code=customer.customer_code,
name=customer.name,
business_name=customer.business_name,
customer_type=customer.customer_type,
tax_id=customer.tax_id,
email=customer.email,
phone=customer.phone,
address_line1=customer.address_line1,
address_line2=customer.address_line2,
city=customer.city,
state=customer.state,
postal_code=customer.postal_code,
country=customer.country,
business_license=customer.business_license,
is_active=customer.is_active,
preferred_delivery_method=customer.preferred_delivery_method,
payment_terms=customer.payment_terms,
credit_limit=customer.credit_limit,
discount_percentage=customer.discount_percentage,
customer_segment=customer.customer_segment,
priority_level=customer.priority_level,
special_instructions=customer.special_instructions,
delivery_preferences=customer.delivery_preferences,
product_preferences=customer.product_preferences,
total_orders=customer.total_orders,
total_spent=customer.total_spent,
average_order_value=customer.average_order_value,
last_order_date=customer.last_order_date,
customer_code=customer_data.get('customer_code'),
name=customer_data.get('name'),
business_name=customer_data.get('business_name'),
customer_type=customer_data.get('customer_type'),
tax_id=customer_data.get('tax_id'),
email=customer_data.get('email'),
phone=customer_data.get('phone'),
address_line1=customer_data.get('address_line1'),
address_line2=customer_data.get('address_line2'),
city=customer_data.get('city'),
state=customer_data.get('state'),
postal_code=customer_data.get('postal_code'),
country=customer_data.get('country'),
business_license=customer_data.get('business_license'),
is_active=customer_data.get('is_active', True),
preferred_delivery_method=customer_data.get('preferred_delivery_method'),
payment_terms=customer_data.get('payment_terms'),
credit_limit=customer_data.get('credit_limit', 0.0),
discount_percentage=customer_data.get('discount_percentage', 0.0),
customer_segment=customer_data.get('customer_segment'),
priority_level=customer_data.get('priority_level'),
special_instructions=customer_data.get('special_instructions'),
delivery_preferences=customer_data.get('delivery_preferences'),
product_preferences=customer_data.get('product_preferences'),
total_orders=customer_data.get('total_orders', 0),
total_spent=customer_data.get('total_spent', 0.0),
average_order_value=customer_data.get('average_order_value', 0.0),
last_order_date=adjust_date_for_demo(
datetime.fromisoformat(customer_data['last_order_date'].replace('Z', '+00:00')),
session_time,
BASE_REFERENCE_DATE
) if customer_data.get('last_order_date') else None,
created_at=session_time,
updated_at=session_time
)
db.add(new_customer)
stats["customers"] += 1
# Clone Customer Orders with Line Items
result = await db.execute(
select(CustomerOrder).where(CustomerOrder.tenant_id == base_uuid)
)
base_orders = result.scalars().all()
logger.info(
"Found customer orders to clone",
count=len(base_orders),
base_tenant=str(base_uuid)
)
# Load Customer Orders from seed data
order_id_map = {}
for order in base_orders:
new_order_id = uuid.uuid4()
order_id_map[order.id] = new_order_id
for order_data in seed_data.get('orders', []):
# Transform IDs using XOR
from shared.utils.demo_id_transformer import transform_id
try:
order_uuid = uuid.UUID(order_data['id'])
transformed_id = transform_id(order_data['id'], virtual_uuid)
except ValueError as e:
logger.error("Failed to parse order UUID",
order_id=order_data['id'],
error=str(e))
continue
order_id_map[uuid.UUID(order_data['id'])] = transformed_id
# Map customer_id if it exists in our map
customer_id_value = order_data.get('customer_id')
if customer_id_value:
customer_id_value = customer_id_map.get(uuid.UUID(customer_id_value), uuid.UUID(customer_id_value))
# Adjust dates using demo_dates utility
adjusted_order_date = adjust_date_for_demo(
order.order_date, session_time, BASE_REFERENCE_DATE
)
adjusted_requested_delivery = adjust_date_for_demo(
order.requested_delivery_date, session_time, BASE_REFERENCE_DATE
)
adjusted_confirmed_delivery = adjust_date_for_demo(
order.confirmed_delivery_date, session_time, BASE_REFERENCE_DATE
)
adjusted_actual_delivery = adjust_date_for_demo(
order.actual_delivery_date, session_time, BASE_REFERENCE_DATE
)
adjusted_window_start = adjust_date_for_demo(
order.delivery_window_start, session_time, BASE_REFERENCE_DATE
)
adjusted_window_end = adjust_date_for_demo(
order.delivery_window_end, session_time, BASE_REFERENCE_DATE
)
datetime.fromisoformat(order_data['order_date'].replace('Z', '+00:00')),
session_time,
BASE_REFERENCE_DATE
) if order_data.get('order_date') else session_time
adjusted_requested_delivery = adjust_date_for_demo(
datetime.fromisoformat(order_data['requested_delivery_date'].replace('Z', '+00:00')),
session_time,
BASE_REFERENCE_DATE
) if order_data.get('requested_delivery_date') else None
# Create new order from seed data
new_order = CustomerOrder(
id=new_order_id,
id=str(transformed_id),
tenant_id=virtual_uuid,
order_number=f"ORD-{uuid.uuid4().hex[:8].upper()}", # New order number
customer_id=customer_id_map.get(order.customer_id, order.customer_id),
status=order.status,
order_type=order.order_type,
priority=order.priority,
order_number=order_data.get('order_number', f"ORD-{uuid.uuid4().hex[:8].upper()}"),
customer_id=str(customer_id_value) if customer_id_value else None,
status=order_data.get('status', 'pending'),
order_type=order_data.get('order_type', 'standard'),
priority=order_data.get('priority', 'normal'),
order_date=adjusted_order_date,
requested_delivery_date=adjusted_requested_delivery,
confirmed_delivery_date=adjusted_confirmed_delivery,
actual_delivery_date=adjusted_actual_delivery,
delivery_method=order.delivery_method,
delivery_address=order.delivery_address,
delivery_instructions=order.delivery_instructions,
delivery_window_start=adjusted_window_start,
delivery_window_end=adjusted_window_end,
subtotal=order.subtotal,
tax_amount=order.tax_amount,
discount_amount=order.discount_amount,
discount_percentage=order.discount_percentage,
delivery_fee=order.delivery_fee,
total_amount=order.total_amount,
payment_status=order.payment_status,
payment_method=order.payment_method,
payment_terms=order.payment_terms,
payment_due_date=order.payment_due_date,
special_instructions=order.special_instructions,
order_source=order.order_source,
sales_channel=order.sales_channel,
delivery_method=order_data.get('delivery_method'),
delivery_address=order_data.get('delivery_address'),
delivery_instructions=order_data.get('delivery_instructions'),
subtotal=order_data.get('subtotal', 0.0),
tax_amount=order_data.get('tax_amount', 0.0),
discount_amount=order_data.get('discount_amount', 0.0),
discount_percentage=order_data.get('discount_percentage', 0.0),
delivery_fee=order_data.get('delivery_fee', 0.0),
total_amount=order_data.get('total_amount', 0.0),
payment_status=order_data.get('payment_status', 'pending'),
payment_method=order_data.get('payment_method'),
payment_terms=order_data.get('payment_terms'),
special_instructions=order_data.get('special_instructions'),
order_source=order_data.get('order_source', 'demo'),
sales_channel=order_data.get('sales_channel', 'direct'),
created_at=session_time,
updated_at=session_time
)

View File

@@ -13,7 +13,7 @@ from app.core.database import database_manager
from app.api.orders import router as orders_router
from app.api.customers import router as customers_router
from app.api.order_operations import router as order_operations_router
from app.api import internal_demo, audit
from app.api import audit, internal_demo
from shared.service_base import StandardFastAPIService
@@ -104,8 +104,8 @@ service.add_router(orders_router)
# BUSINESS: Complex operations and workflows
service.add_router(order_operations_router)
# INTERNAL: Service-to-service endpoints
service.add_router(internal_demo.router)
# INTERNAL: Service-to-service endpoints - DEPRECATED: Replaced by script-based seed data loading
service.add_router(internal_demo.router, tags=["internal-demo"])
# REMOVED: test_procurement_scheduler endpoint
# Procurement scheduling is now triggered by the Orchestrator Service