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

@@ -177,11 +177,33 @@ async def clone_demo_data(
new_price_id = uuid.uuid4()
price_list_map[price_list.id] = new_price_id
# Transform inventory_product_id to match virtual tenant's ingredient IDs
# Using same formula as inventory service: tenant_int ^ base_int
base_product_int = int(price_list.inventory_product_id.hex, 16)
virtual_tenant_int = int(virtual_uuid.hex, 16)
base_tenant_int = int(base_uuid.hex, 16)
# Reverse the original XOR to get the base ingredient ID
# base_product = base_tenant ^ base_ingredient_id
# So: base_ingredient_id = base_tenant ^ base_product
base_ingredient_int = base_tenant_int ^ base_product_int
# Now apply virtual tenant XOR
new_product_id = uuid.UUID(int=virtual_tenant_int ^ base_ingredient_int)
logger.debug(
"Transforming price list product ID using XOR",
supplier_name=supplier.name,
base_product_id=str(price_list.inventory_product_id),
new_product_id=str(new_product_id),
product_code=price_list.product_code
)
new_price_list = SupplierPriceList(
id=new_price_id,
tenant_id=virtual_uuid,
supplier_id=new_supplier_id,
inventory_product_id=price_list.inventory_product_id, # Keep product reference
inventory_product_id=new_product_id, # Transformed for virtual tenant
product_code=price_list.product_code,
unit_price=price_list.unit_price,
unit_of_measure=price_list.unit_of_measure,
@@ -290,12 +312,20 @@ async def clone_demo_data(
new_price_list_id = price_list_map.get(item.price_list_item_id, item.price_list_item_id) if item.price_list_item_id else None
# Transform inventory_product_id to match virtual tenant's ingredient IDs
if item.inventory_product_id:
base_product_int = int(item.inventory_product_id.hex, 16)
base_ingredient_int = base_tenant_int ^ base_product_int
new_inventory_product_id = uuid.UUID(int=virtual_tenant_int ^ base_ingredient_int)
else:
new_inventory_product_id = None
new_item = PurchaseOrderItem(
id=new_item_id,
tenant_id=virtual_uuid,
purchase_order_id=new_po_id,
price_list_item_id=new_price_list_id,
inventory_product_id=item.inventory_product_id, # Keep product reference
inventory_product_id=new_inventory_product_id, # Transformed product reference
product_code=item.product_code,
ordered_quantity=item.ordered_quantity,
unit_of_measure=item.unit_of_measure,
@@ -377,12 +407,20 @@ async def clone_demo_data(
for item in delivery_items:
new_po_item_id = po_item_map.get(item.purchase_order_item_id, item.purchase_order_item_id)
# Transform inventory_product_id to match virtual tenant's ingredient IDs
if item.inventory_product_id:
base_product_int = int(item.inventory_product_id.hex, 16)
base_ingredient_int = base_tenant_int ^ base_product_int
new_inventory_product_id = uuid.UUID(int=virtual_tenant_int ^ base_ingredient_int)
else:
new_inventory_product_id = None
new_item = DeliveryItem(
id=uuid.uuid4(),
tenant_id=virtual_uuid,
delivery_id=new_delivery_id,
purchase_order_item_id=new_po_item_id,
inventory_product_id=item.inventory_product_id, # Keep product reference
inventory_product_id=new_inventory_product_id, # Transformed product reference
ordered_quantity=item.ordered_quantity,
delivered_quantity=item.delivered_quantity,
accepted_quantity=item.accepted_quantity,

View File

@@ -43,9 +43,9 @@ async def create_purchase_order(
try:
service = PurchaseOrderService(db)
purchase_order = await service.create_purchase_order(
tenant_id=current_user.tenant_id,
tenant_id=current_user["tenant_id"],
po_data=po_data,
created_by=current_user.user_id
created_by=current_user["user_id"]
)
return PurchaseOrderResponse.from_orm(purchase_order)
except ValueError as e:
@@ -93,7 +93,9 @@ async def list_purchase_orders(
status_enum = None
if status:
try:
status_enum = PurchaseOrderStatus(status.upper())
# Convert from PENDING_APPROVAL to pending_approval format
status_value = status.lower()
status_enum = PurchaseOrderStatus(status_value)
except ValueError:
raise HTTPException(status_code=400, detail="Invalid status")
@@ -110,11 +112,29 @@ async def list_purchase_orders(
)
orders = await service.search_purchase_orders(
tenant_id=current_user.tenant_id,
tenant_id=current_user["tenant_id"],
search_params=search_params
)
return [PurchaseOrderSummary.from_orm(order) for order in orders]
# Convert to response with supplier names
response = []
for order in orders:
order_dict = {
"id": order.id,
"po_number": order.po_number,
"supplier_id": order.supplier_id,
"supplier_name": order.supplier.name if order.supplier else None,
"status": order.status,
"priority": order.priority,
"order_date": order.order_date,
"required_delivery_date": order.required_delivery_date,
"total_amount": order.total_amount,
"currency": order.currency,
"created_at": order.created_at
}
response.append(PurchaseOrderSummary(**order_dict))
return response
except HTTPException:
raise
except Exception as e:
@@ -134,14 +154,11 @@ async def get_purchase_order(
try:
service = PurchaseOrderService(db)
purchase_order = await service.get_purchase_order(po_id)
if not purchase_order:
raise HTTPException(status_code=404, detail="Purchase order not found")
# Check tenant access
if purchase_order.tenant_id != current_user.tenant_id:
raise HTTPException(status_code=403, detail="Access denied")
# Tenant access control is handled by the gateway
return PurchaseOrderResponse.from_orm(purchase_order)
except HTTPException:
raise
@@ -168,13 +185,13 @@ async def update_purchase_order(
existing_order = await service.get_purchase_order(po_id)
if not existing_order:
raise HTTPException(status_code=404, detail="Purchase order not found")
if existing_order.tenant_id != current_user.tenant_id:
if existing_order.tenant_id != current_user["tenant_id"]:
raise HTTPException(status_code=403, detail="Access denied")
purchase_order = await service.update_purchase_order(
po_id=po_id,
po_data=po_data,
updated_by=current_user.user_id
updated_by=current_user["user_id"]
)
if not purchase_order:
@@ -206,7 +223,7 @@ async def delete_purchase_order(
existing_order = await service.get_purchase_order(po_id)
if not existing_order:
raise HTTPException(status_code=404, detail="Purchase order not found")
if existing_order.tenant_id != current_user.tenant_id:
if existing_order.tenant_id != current_user["tenant_id"]:
raise HTTPException(status_code=403, detail="Access denied")
# Capture PO data before deletion

View File

@@ -9,8 +9,10 @@ from uuid import UUID
import structlog
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.core.database import get_db
from app.services.supplier_service import SupplierService
from app.models.suppliers import SupplierPriceList
from app.schemas.suppliers import (
SupplierCreate, SupplierUpdate, SupplierResponse, SupplierSummary,
SupplierSearchParams
@@ -202,4 +204,54 @@ async def delete_supplier(
raise
except Exception as e:
logger.error("Error deleting supplier", supplier_id=str(supplier_id), error=str(e))
raise HTTPException(status_code=500, detail="Failed to delete supplier")
raise HTTPException(status_code=500, detail="Failed to delete supplier")
@router.get(
route_builder.build_resource_action_route("suppliers", "supplier_id", "products"),
response_model=List[Dict[str, Any]]
)
async def get_supplier_products(
supplier_id: UUID = Path(..., description="Supplier ID"),
tenant_id: str = Path(..., description="Tenant ID"),
is_active: bool = Query(True, description="Filter by active price lists"),
db: AsyncSession = Depends(get_db)
):
"""
Get list of product IDs that a supplier provides
Returns a list of inventory product IDs from the supplier's price list
"""
try:
# Query supplier price lists
query = select(SupplierPriceList).where(
SupplierPriceList.tenant_id == UUID(tenant_id),
SupplierPriceList.supplier_id == supplier_id
)
if is_active:
query = query.where(SupplierPriceList.is_active == True)
result = await db.execute(query)
price_lists = result.scalars().all()
# Extract unique product IDs
product_ids = list(set([str(pl.inventory_product_id) for pl in price_lists]))
logger.info(
"Retrieved supplier products",
supplier_id=str(supplier_id),
product_count=len(product_ids)
)
return [{"inventory_product_id": pid} for pid in product_ids]
except Exception as e:
logger.error(
"Error getting supplier products",
supplier_id=str(supplier_id),
error=str(e)
)
raise HTTPException(
status_code=500,
detail="Failed to retrieve supplier products"
)