Improve the frontend

This commit is contained in:
Urtzi Alfaro
2025-10-21 19:50:07 +02:00
parent 05da20357d
commit 8d30172483
105 changed files with 14699 additions and 4630 deletions

View File

@@ -0,0 +1,226 @@
"""
Internal Demo API Endpoints for POS Service
Used by demo_session service to clone data for virtual demo tenants
"""
from fastapi import APIRouter, Depends, HTTPException, Header
from typing import Dict, Any
from uuid import UUID
import structlog
import os
from app.core.database import get_db
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.models.pos_config import POSConfiguration
from app.models.pos_transaction import POSTransaction, POSTransactionItem
import uuid
from datetime import datetime, timezone
from typing import Optional
router = APIRouter()
logger = structlog.get_logger()
# Internal API key for service-to-service communication
INTERNAL_API_KEY = os.getenv("INTERNAL_API_KEY", "dev-internal-key-change-in-production")
def verify_internal_api_key(x_internal_api_key: str = Header(...)):
"""Verify internal API key for service-to-service communication"""
if x_internal_api_key != INTERNAL_API_KEY:
raise HTTPException(status_code=403, detail="Invalid internal API key")
return True
@router.post("/internal/demo/clone")
async def clone_demo_data(
base_tenant_id: str,
virtual_tenant_id: str,
demo_account_type: str,
session_id: Optional[str] = None,
db: AsyncSession = Depends(get_db),
_: bool = Depends(verify_internal_api_key)
):
"""
Clone POS demo data from base tenant to virtual tenant
This endpoint is called by the demo_session service during session initialization.
It clones POS configurations and recent transactions.
"""
start_time = datetime.now(timezone.utc)
session_created_at = datetime.now(timezone.utc)
logger.info(
"Starting POS data cloning with date adjustment",
base_tenant_id=base_tenant_id,
virtual_tenant_id=virtual_tenant_id,
demo_account_type=demo_account_type,
session_id=session_id,
session_created_at=session_created_at.isoformat()
)
try:
base_uuid = uuid.UUID(base_tenant_id)
virtual_uuid = uuid.UUID(virtual_tenant_id)
# Fetch base tenant POS configurations
result = await db.execute(
select(POSConfiguration).where(POSConfiguration.tenant_id == base_uuid)
)
base_configs = list(result.scalars().all())
configs_cloned = 0
transactions_cloned = 0
# Clone each configuration
for base_config in base_configs:
# Create new config for virtual tenant
new_config = POSConfiguration(
id=uuid.uuid4(),
tenant_id=virtual_uuid,
pos_system=base_config.pos_system,
provider_name=f"{base_config.provider_name} (Demo Session)",
is_active=base_config.is_active,
is_connected=base_config.is_connected,
encrypted_credentials=base_config.encrypted_credentials,
webhook_url=base_config.webhook_url,
webhook_secret=base_config.webhook_secret,
environment=base_config.environment,
location_id=base_config.location_id,
merchant_id=base_config.merchant_id,
sync_enabled=base_config.sync_enabled,
sync_interval_minutes=base_config.sync_interval_minutes,
auto_sync_products=base_config.auto_sync_products,
auto_sync_transactions=base_config.auto_sync_transactions,
last_sync_at=base_config.last_sync_at,
last_successful_sync_at=base_config.last_successful_sync_at,
last_sync_status=base_config.last_sync_status,
last_sync_message=base_config.last_sync_message,
provider_settings=base_config.provider_settings,
last_health_check_at=base_config.last_health_check_at,
health_status=base_config.health_status,
health_message=base_config.health_message,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
notes=f"Cloned from base config {base_config.id} for demo session {session_id}"
)
db.add(new_config)
await db.flush()
configs_cloned += 1
# Clone recent transactions for this config
tx_result = await db.execute(
select(POSTransaction)
.where(POSTransaction.pos_config_id == base_config.id)
.order_by(POSTransaction.transaction_date.desc())
.limit(10) # Clone last 10 transactions
)
base_transactions = list(tx_result.scalars().all())
# Clone each transaction
for base_tx in base_transactions:
new_tx = POSTransaction(
id=uuid.uuid4(),
tenant_id=virtual_uuid,
pos_config_id=new_config.id,
pos_system=base_tx.pos_system,
external_transaction_id=base_tx.external_transaction_id,
external_order_id=base_tx.external_order_id,
transaction_type=base_tx.transaction_type,
status=base_tx.status,
subtotal=base_tx.subtotal,
tax_amount=base_tx.tax_amount,
tip_amount=base_tx.tip_amount,
discount_amount=base_tx.discount_amount,
total_amount=base_tx.total_amount,
currency=base_tx.currency,
payment_method=base_tx.payment_method,
payment_status=base_tx.payment_status,
transaction_date=base_tx.transaction_date,
pos_created_at=base_tx.pos_created_at,
pos_updated_at=base_tx.pos_updated_at,
location_id=base_tx.location_id,
location_name=base_tx.location_name,
staff_id=base_tx.staff_id,
staff_name=base_tx.staff_name,
customer_id=base_tx.customer_id,
customer_email=base_tx.customer_email,
customer_phone=base_tx.customer_phone,
order_type=base_tx.order_type,
table_number=base_tx.table_number,
receipt_number=base_tx.receipt_number,
is_synced_to_sales=base_tx.is_synced_to_sales,
sales_record_id=base_tx.sales_record_id,
sync_attempted_at=base_tx.sync_attempted_at,
sync_completed_at=base_tx.sync_completed_at,
sync_error=base_tx.sync_error,
sync_retry_count=base_tx.sync_retry_count,
raw_data=base_tx.raw_data,
is_processed=base_tx.is_processed,
processing_error=base_tx.processing_error,
is_duplicate=base_tx.is_duplicate,
duplicate_of=base_tx.duplicate_of,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc)
)
db.add(new_tx)
await db.flush()
transactions_cloned += 1
# Clone transaction items
item_result = await db.execute(
select(POSTransactionItem).where(POSTransactionItem.transaction_id == base_tx.id)
)
base_items = list(item_result.scalars().all())
for base_item in base_items:
new_item = POSTransactionItem(
id=uuid.uuid4(),
transaction_id=new_tx.id,
tenant_id=virtual_uuid,
external_item_id=base_item.external_item_id,
sku=base_item.sku,
product_name=base_item.product_name,
product_category=base_item.product_category,
product_subcategory=base_item.product_subcategory,
quantity=base_item.quantity,
unit_price=base_item.unit_price,
total_price=base_item.total_price,
discount_amount=base_item.discount_amount,
tax_amount=base_item.tax_amount,
modifiers=base_item.modifiers,
inventory_product_id=base_item.inventory_product_id,
is_mapped_to_inventory=base_item.is_mapped_to_inventory,
is_synced_to_sales=base_item.is_synced_to_sales,
sync_error=base_item.sync_error,
raw_data=base_item.raw_data,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc)
)
db.add(new_item)
await db.commit()
logger.info(
"POS demo data cloned successfully",
virtual_tenant_id=str(virtual_tenant_id),
configs_cloned=configs_cloned,
transactions_cloned=transactions_cloned
)
return {
"success": True,
"records_cloned": configs_cloned + transactions_cloned,
"configs_cloned": configs_cloned,
"transactions_cloned": transactions_cloned,
"service": "pos"
}
except Exception as e:
logger.error("Failed to clone POS demo data", error=str(e), exc_info=True)
await db.rollback()
raise HTTPException(status_code=500, detail=f"Failed to clone POS demo data: {str(e)}")

View File

@@ -11,6 +11,7 @@ from app.api.configurations import router as configurations_router
from app.api.transactions import router as transactions_router
from app.api.pos_operations import router as pos_operations_router
from app.api.analytics import router as analytics_router
from app.api.internal_demo import router as internal_demo_router
from app.core.database import database_manager
from shared.service_base import StandardFastAPIService
@@ -173,6 +174,7 @@ service.add_router(configurations_router, tags=["pos-configurations"])
service.add_router(transactions_router, tags=["pos-transactions"])
service.add_router(pos_operations_router, tags=["pos-operations"])
service.add_router(analytics_router, tags=["pos-analytics"])
service.add_router(internal_demo_router, tags=["internal-demo"])
if __name__ == "__main__":

View File

@@ -0,0 +1,301 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demo POS Configurations Seeding Script for POS Service
Creates realistic POS configurations and transactions for demo template tenants
This script runs as a Kubernetes init job inside the pos-service container.
"""
import asyncio
import uuid
import sys
import os
from datetime import datetime, timezone, timedelta
from pathlib import Path
# Add app to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
import structlog
from app.models.pos_config import POSConfiguration
from app.models.pos_transaction import POSTransaction, POSTransactionItem
# Configure logging
logger = structlog.get_logger()
# Base demo tenant IDs
DEMO_TENANT_SAN_PABLO = uuid.UUID("a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6") # Individual bakery
DEMO_TENANT_LA_ESPIGA = uuid.UUID("b2c3d4e5-f6a7-48b9-c0d1-e2f3a4b5c6d7") # Central bakery
# Base reference date for date calculations
BASE_REFERENCE_DATE = datetime(2025, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
async def generate_pos_config_for_tenant(
db: AsyncSession,
tenant_id: uuid.UUID,
tenant_name: str,
pos_system: str,
provider_name: str
):
"""Generate a demo POS configuration for a tenant"""
logger.info(f"Generating POS config for: {tenant_name}", tenant_id=str(tenant_id), pos_system=pos_system)
# Check if config already exists
result = await db.execute(
select(POSConfiguration).where(
POSConfiguration.tenant_id == tenant_id,
POSConfiguration.pos_system == pos_system
).limit(1)
)
existing = result.scalar_one_or_none()
if existing:
logger.info(f"POS config already exists for {tenant_name}, skipping")
return {"tenant_id": str(tenant_id), "configs_created": 0, "skipped": True}
# Create demo POS configuration
config = POSConfiguration(
id=uuid.uuid4(),
tenant_id=tenant_id,
pos_system=pos_system,
provider_name=provider_name,
is_active=True,
is_connected=True,
encrypted_credentials="demo_credentials_encrypted", # In real scenario, this would be encrypted
environment="sandbox",
location_id=f"LOC-{tenant_name.replace(' ', '-').upper()}-001",
merchant_id=f"MERCH-{tenant_name.replace(' ', '-').upper()}",
sync_enabled=True,
sync_interval_minutes="5",
auto_sync_products=True,
auto_sync_transactions=True,
last_sync_at=BASE_REFERENCE_DATE - timedelta(hours=1),
last_successful_sync_at=BASE_REFERENCE_DATE - timedelta(hours=1),
last_sync_status="success",
last_sync_message="Sincronización completada exitosamente",
provider_settings={
"api_key": "demo_api_key_***",
"location_id": f"LOC-{tenant_name.replace(' ', '-').upper()}-001",
"environment": "sandbox"
},
last_health_check_at=BASE_REFERENCE_DATE - timedelta(minutes=30),
health_status="healthy",
health_message="Conexión saludable - todas las operaciones funcionando correctamente",
created_at=BASE_REFERENCE_DATE - timedelta(days=30),
updated_at=BASE_REFERENCE_DATE - timedelta(hours=1),
notes=f"Configuración demo para {tenant_name}"
)
db.add(config)
await db.flush()
logger.info(f"Created POS config for {tenant_name}", config_id=str(config.id))
# Generate demo transactions
transactions_created = await generate_demo_transactions(db, tenant_id, config.id, pos_system)
return {
"tenant_id": str(tenant_id),
"configs_created": 1,
"transactions_created": transactions_created,
"skipped": False
}
async def generate_demo_transactions(
db: AsyncSession,
tenant_id: uuid.UUID,
pos_config_id: uuid.UUID,
pos_system: str
):
"""Generate demo POS transactions"""
transactions_to_create = 10 # Create 10 demo transactions
transactions_created = 0
for i in range(transactions_to_create):
# Calculate transaction date (spread over last 7 days)
days_ago = i % 7
transaction_date = BASE_REFERENCE_DATE - timedelta(days=days_ago, hours=i % 12)
# Generate realistic transaction amounts
base_amounts = [12.50, 25.00, 45.75, 18.20, 32.00, 60.50, 15.80, 28.90, 55.00, 40.25]
subtotal = base_amounts[i % len(base_amounts)]
tax_amount = round(subtotal * 0.10, 2) # 10% tax
total_amount = subtotal + tax_amount
# Create transaction
transaction = POSTransaction(
id=uuid.uuid4(),
tenant_id=tenant_id,
pos_config_id=pos_config_id,
pos_system=pos_system,
external_transaction_id=f"{pos_system.upper()}-TXN-{i+1:05d}",
external_order_id=f"{pos_system.upper()}-ORD-{i+1:05d}",
transaction_type="sale",
status="completed",
subtotal=subtotal,
tax_amount=tax_amount,
tip_amount=0.00,
discount_amount=0.00,
total_amount=total_amount,
currency="EUR",
payment_method="card" if i % 2 == 0 else "cash",
payment_status="paid",
transaction_date=transaction_date,
pos_created_at=transaction_date,
pos_updated_at=transaction_date,
location_id=f"LOC-001",
location_name="Tienda Principal",
order_type="takeout" if i % 3 == 0 else "dine_in",
receipt_number=f"RCP-{i+1:06d}",
is_synced_to_sales=True,
sync_completed_at=transaction_date + timedelta(minutes=5),
sync_retry_count=0,
is_processed=True,
is_duplicate=False,
created_at=transaction_date,
updated_at=transaction_date
)
db.add(transaction)
await db.flush()
# Add transaction items
num_items = (i % 3) + 1 # 1-3 items per transaction
for item_idx in range(num_items):
product_names = [
"Pan de masa madre", "Croissant de mantequilla", "Pastel de chocolate",
"Baguette artesanal", "Tarta de manzana", "Bollería variada",
"Pan integral", "Galletas artesanales", "Café con leche"
]
product_name = product_names[(i + item_idx) % len(product_names)]
item_price = round(subtotal / num_items, 2)
item = POSTransactionItem(
id=uuid.uuid4(),
transaction_id=transaction.id,
tenant_id=tenant_id,
external_item_id=f"ITEM-{i+1:05d}-{item_idx+1}",
sku=f"SKU-{(i + item_idx) % len(product_names):03d}",
product_name=product_name,
product_category="bakery",
quantity=1,
unit_price=item_price,
total_price=item_price,
discount_amount=0.00,
tax_amount=round(item_price * 0.10, 2),
is_mapped_to_inventory=False,
is_synced_to_sales=True,
created_at=transaction_date,
updated_at=transaction_date
)
db.add(item)
transactions_created += 1
logger.info(f"Created {transactions_created} demo transactions for tenant {tenant_id}")
return transactions_created
async def seed_all(db: AsyncSession):
"""Seed all demo tenants with POS configurations"""
logger.info("Starting demo POS configurations seed process")
results = []
# Seed San Pablo with Square POS
result_san_pablo = await generate_pos_config_for_tenant(
db,
DEMO_TENANT_SAN_PABLO,
"San Pablo",
"square",
"Square POS - San Pablo"
)
results.append(result_san_pablo)
# Seed La Espiga with Toast POS
result_la_espiga = await generate_pos_config_for_tenant(
db,
DEMO_TENANT_LA_ESPIGA,
"La Espiga",
"toast",
"Toast POS - La Espiga"
)
results.append(result_la_espiga)
await db.commit()
total_configs = sum(r["configs_created"] for r in results)
total_transactions = sum(r.get("transactions_created", 0) for r in results)
return {
"results": results,
"total_configs_created": total_configs,
"total_transactions_created": total_transactions,
"status": "completed"
}
async def main():
"""Main execution function"""
# Get database URL from environment
database_url = os.getenv("POS_DATABASE_URL")
if not database_url:
logger.error("POS_DATABASE_URL environment variable must be set")
return 1
# Ensure asyncpg driver
if database_url.startswith("postgresql://"):
database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
# Create async engine
engine = create_async_engine(database_url, echo=False)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
try:
async with async_session() as session:
result = await seed_all(session)
logger.info(
"POS configurations seed completed successfully!",
total_configs=result["total_configs_created"],
total_transactions=result["total_transactions_created"],
status=result["status"]
)
# Print summary
print("\n" + "="*60)
print("DEMO POS CONFIGURATIONS SEED SUMMARY")
print("="*60)
for tenant_result in result["results"]:
tenant_id = tenant_result["tenant_id"]
configs = tenant_result["configs_created"]
transactions = tenant_result.get("transactions_created", 0)
skipped = tenant_result.get("skipped", False)
status = "SKIPPED (already exists)" if skipped else f"CREATED {configs} config(s), {transactions} transaction(s)"
print(f"Tenant {tenant_id}: {status}")
print(f"\nTotal Configs: {result['total_configs_created']}")
print(f"Total Transactions: {result['total_transactions_created']}")
print("="*60 + "\n")
return 0
except Exception as e:
logger.error(f"POS configurations seed failed: {str(e)}", exc_info=True)
return 1
finally:
await engine.dispose()
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)