Improve the frontend and fix TODOs
This commit is contained in:
@@ -159,9 +159,37 @@ async def get_supplier_performance_metrics(
|
||||
):
|
||||
"""Get performance metrics for a supplier"""
|
||||
try:
|
||||
# TODO: Implement get_supplier_performance_metrics in service
|
||||
# For now, return empty list
|
||||
metrics = []
|
||||
from app.models.performance import SupplierPerformanceMetric
|
||||
from sqlalchemy import select, and_, desc
|
||||
|
||||
# Build query for performance metrics
|
||||
query = select(SupplierPerformanceMetric).where(
|
||||
and_(
|
||||
SupplierPerformanceMetric.supplier_id == supplier_id,
|
||||
SupplierPerformanceMetric.tenant_id == tenant_id
|
||||
)
|
||||
)
|
||||
|
||||
# Apply filters
|
||||
if metric_type:
|
||||
query = query.where(SupplierPerformanceMetric.metric_type == metric_type)
|
||||
|
||||
if date_from:
|
||||
query = query.where(SupplierPerformanceMetric.calculated_at >= date_from)
|
||||
|
||||
if date_to:
|
||||
query = query.where(SupplierPerformanceMetric.calculated_at <= date_to)
|
||||
|
||||
# Order by most recent and apply limit
|
||||
query = query.order_by(desc(SupplierPerformanceMetric.calculated_at)).limit(limit)
|
||||
|
||||
result = await db.execute(query)
|
||||
metrics = result.scalars().all()
|
||||
|
||||
logger.info("Retrieved performance metrics",
|
||||
tenant_id=str(tenant_id),
|
||||
supplier_id=str(supplier_id),
|
||||
count=len(metrics))
|
||||
|
||||
return metrics
|
||||
|
||||
@@ -227,9 +255,39 @@ async def get_supplier_alerts(
|
||||
):
|
||||
"""Get supplier alerts with filtering"""
|
||||
try:
|
||||
# TODO: Implement get_supplier_alerts in service
|
||||
# For now, return empty list
|
||||
alerts = []
|
||||
from app.models.performance import SupplierAlert
|
||||
from sqlalchemy import select, and_, desc
|
||||
|
||||
# Build query for alerts
|
||||
query = select(SupplierAlert).where(
|
||||
SupplierAlert.tenant_id == tenant_id
|
||||
)
|
||||
|
||||
# Apply filters
|
||||
if supplier_id:
|
||||
query = query.where(SupplierAlert.supplier_id == supplier_id)
|
||||
|
||||
if alert_type:
|
||||
query = query.where(SupplierAlert.alert_type == alert_type)
|
||||
|
||||
if severity:
|
||||
query = query.where(SupplierAlert.severity == severity)
|
||||
|
||||
if date_from:
|
||||
query = query.where(SupplierAlert.created_at >= date_from)
|
||||
|
||||
if date_to:
|
||||
query = query.where(SupplierAlert.created_at <= date_to)
|
||||
|
||||
# Order by most recent and apply limit
|
||||
query = query.order_by(desc(SupplierAlert.created_at)).limit(limit)
|
||||
|
||||
result = await db.execute(query)
|
||||
alerts = result.scalars().all()
|
||||
|
||||
logger.info("Retrieved supplier alerts",
|
||||
tenant_id=str(tenant_id),
|
||||
count=len(alerts))
|
||||
|
||||
return alerts
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ Service-to-service endpoint for cloning supplier and procurement data
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Header
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, delete, func
|
||||
import structlog
|
||||
import uuid
|
||||
from datetime import datetime, timezone, timedelta, date
|
||||
@@ -575,3 +575,51 @@ async def clone_health_check(_: bool = Depends(verify_internal_api_key)):
|
||||
"clone_endpoint": "available",
|
||||
"version": "2.0.0"
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/tenant/{virtual_tenant_id}")
|
||||
async def delete_demo_data(
|
||||
virtual_tenant_id: str,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: bool = Depends(verify_internal_api_key)
|
||||
):
|
||||
"""Delete all supplier data for a virtual demo tenant"""
|
||||
logger.info("Deleting supplier data for virtual tenant", virtual_tenant_id=virtual_tenant_id)
|
||||
start_time = datetime.now(timezone.utc)
|
||||
|
||||
try:
|
||||
virtual_uuid = uuid.UUID(virtual_tenant_id)
|
||||
|
||||
# Count records
|
||||
supplier_count = await db.scalar(select(func.count(Supplier.id)).where(Supplier.tenant_id == virtual_uuid))
|
||||
po_count = await db.scalar(select(func.count(PurchaseOrder.id)).where(PurchaseOrder.tenant_id == virtual_uuid))
|
||||
|
||||
# Delete in order (child tables first)
|
||||
await db.execute(delete(SupplierInvoice).where(SupplierInvoice.tenant_id == virtual_uuid))
|
||||
await db.execute(delete(SupplierQualityReview).where(SupplierQualityReview.tenant_id == virtual_uuid))
|
||||
await db.execute(delete(DeliveryItem).where(DeliveryItem.tenant_id == virtual_uuid))
|
||||
await db.execute(delete(Delivery).where(Delivery.tenant_id == virtual_uuid))
|
||||
await db.execute(delete(PurchaseOrderItem).where(PurchaseOrderItem.tenant_id == virtual_uuid))
|
||||
await db.execute(delete(PurchaseOrder).where(PurchaseOrder.tenant_id == virtual_uuid))
|
||||
await db.execute(delete(SupplierPriceList).where(SupplierPriceList.tenant_id == virtual_uuid))
|
||||
await db.execute(delete(Supplier).where(Supplier.tenant_id == virtual_uuid))
|
||||
await db.commit()
|
||||
|
||||
duration_ms = int((datetime.now(timezone.utc) - start_time).total_seconds() * 1000)
|
||||
logger.info("Supplier data deleted successfully", virtual_tenant_id=virtual_tenant_id, duration_ms=duration_ms)
|
||||
|
||||
return {
|
||||
"service": "suppliers",
|
||||
"status": "deleted",
|
||||
"virtual_tenant_id": virtual_tenant_id,
|
||||
"records_deleted": {
|
||||
"suppliers": supplier_count,
|
||||
"purchase_orders": po_count,
|
||||
"total": supplier_count + po_count
|
||||
},
|
||||
"duration_ms": duration_ms
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error("Failed to delete supplier data", error=str(e), exc_info=True)
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@@ -208,7 +208,7 @@ async def delete_supplier(
|
||||
|
||||
|
||||
@router.get(
|
||||
route_builder.build_base_route("suppliers/count"),
|
||||
route_builder.build_base_route("count"),
|
||||
response_model=dict
|
||||
)
|
||||
async def count_suppliers(
|
||||
@@ -219,8 +219,8 @@ async def count_suppliers(
|
||||
try:
|
||||
service = SupplierService(db)
|
||||
|
||||
# Use search with high limit to get all suppliers
|
||||
search_params = SupplierSearchParams(limit=10000)
|
||||
# Use search with maximum allowed limit to get all suppliers
|
||||
search_params = SupplierSearchParams(limit=1000)
|
||||
suppliers = await service.search_suppliers(
|
||||
tenant_id=UUID(tenant_id),
|
||||
search_params=search_params
|
||||
|
||||
Reference in New Issue
Block a user