New alert service

This commit is contained in:
Urtzi Alfaro
2025-12-05 20:07:01 +01:00
parent 1fe3a73549
commit 667e6e0404
393 changed files with 26002 additions and 61033 deletions

View File

@@ -62,17 +62,54 @@ class SuppliersServiceClient(BaseServiceClient):
params["search_term"] = search
if category:
params["supplier_type"] = category
result = await self.get("suppliers", tenant_id=tenant_id, params=params)
suppliers = result if result else []
logger.info("Searched suppliers from suppliers service",
logger.info("Searched suppliers from suppliers service",
search_term=search, suppliers_count=len(suppliers), tenant_id=tenant_id)
return suppliers
except Exception as e:
logger.error("Error searching suppliers",
logger.error("Error searching suppliers",
error=str(e), tenant_id=tenant_id)
return []
async def get_suppliers_batch(self, tenant_id: str, supplier_ids: List[str]) -> Optional[List[Dict[str, Any]]]:
"""
Get multiple suppliers in a single request for performance optimization.
This method eliminates N+1 query patterns when fetching supplier data
for multiple purchase orders or other entities.
Args:
tenant_id: Tenant ID
supplier_ids: List of supplier IDs to fetch
Returns:
List of supplier dictionaries or empty list if error
"""
try:
if not supplier_ids:
return []
# Join IDs as comma-separated string
ids_param = ",".join(supplier_ids)
params = {"ids": ids_param}
result = await self.get("suppliers/batch", tenant_id=tenant_id, params=params)
suppliers = result if result else []
logger.info("Batch retrieved suppliers from suppliers service",
requested_count=len(supplier_ids),
found_count=len(suppliers),
tenant_id=tenant_id)
return suppliers
except Exception as e:
logger.error("Error batch retrieving suppliers",
error=str(e),
requested_count=len(supplier_ids),
tenant_id=tenant_id)
return []
# ================================================================
# SUPPLIER RECOMMENDATIONS
# ================================================================
@@ -107,186 +144,7 @@ class SuppliersServiceClient(BaseServiceClient):
logger.error("Error getting best supplier for ingredient",
error=str(e), ingredient_id=ingredient_id, tenant_id=tenant_id)
return None
# ================================================================
# PURCHASE ORDER MANAGEMENT
# ================================================================
async def create_purchase_order(self, tenant_id: str, order_data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Create a new purchase order"""
try:
result = await self.post("suppliers/purchase-orders", data=order_data, tenant_id=tenant_id)
if result:
logger.info("Created purchase order",
order_id=result.get('id'),
supplier_id=order_data.get('supplier_id'),
tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error creating purchase order",
error=str(e), tenant_id=tenant_id)
return None
async def get_purchase_orders(self, tenant_id: str, status: Optional[str] = None, supplier_id: Optional[str] = None) -> Optional[List[Dict[str, Any]]]:
"""Get purchase orders with optional filtering"""
try:
params = {}
if status:
params["status"] = status
if supplier_id:
params["supplier_id"] = supplier_id
result = await self.get("suppliers/purchase-orders", tenant_id=tenant_id, params=params)
orders = result.get('orders', []) if result else []
logger.info("Retrieved purchase orders from suppliers service",
orders_count=len(orders), tenant_id=tenant_id)
return orders
except Exception as e:
logger.error("Error getting purchase orders",
error=str(e), tenant_id=tenant_id)
return []
async def update_purchase_order_status(self, tenant_id: str, order_id: str, status: str) -> Optional[Dict[str, Any]]:
"""Update purchase order status"""
try:
data = {"status": status}
result = await self.put(f"suppliers/purchase-orders/{order_id}/status", data=data, tenant_id=tenant_id)
if result:
logger.info("Updated purchase order status",
order_id=order_id, status=status, tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error updating purchase order status",
error=str(e), order_id=order_id, tenant_id=tenant_id)
return None
async def approve_purchase_order(
self,
tenant_id: str,
po_id: str,
approval_data: Dict[str, Any]
) -> Optional[Dict[str, Any]]:
"""
Auto-approve a purchase order
Args:
tenant_id: Tenant ID
po_id: Purchase Order ID
approval_data: Approval data including:
- approved_by: User ID or "system" for auto-approval
- approval_notes: Notes about the approval
- auto_approved: Boolean flag indicating auto-approval
- approval_reasons: List of reasons for auto-approval
Returns:
Updated purchase order data or None
"""
try:
# Format the approval request payload
payload = {
"action": "approve",
"notes": approval_data.get("approval_notes", "Auto-approved by system")
}
result = await self.post(
f"suppliers/purchase-orders/{po_id}/approve",
data=payload,
tenant_id=tenant_id
)
if result:
logger.info("Auto-approved purchase order",
po_id=po_id,
tenant_id=tenant_id,
auto_approved=approval_data.get("auto_approved", True))
return result
except Exception as e:
logger.error("Error auto-approving purchase order",
error=str(e),
po_id=po_id,
tenant_id=tenant_id)
return None
async def get_supplier(self, tenant_id: str, supplier_id: str) -> Optional[Dict[str, Any]]:
"""
Get supplier details with performance metrics
Args:
tenant_id: Tenant ID
supplier_id: Supplier ID
Returns:
Supplier data including performance metrics or None
"""
try:
# Use the existing get_supplier_by_id method which returns full supplier data
result = await self.get_supplier_by_id(tenant_id, supplier_id)
if result:
logger.info("Retrieved supplier data for auto-approval",
supplier_id=supplier_id,
tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error getting supplier data",
error=str(e),
supplier_id=supplier_id,
tenant_id=tenant_id)
return None
# ================================================================
# DELIVERY MANAGEMENT
# ================================================================
async def get_deliveries(self, tenant_id: str, status: Optional[str] = None, date: Optional[str] = None) -> Optional[List[Dict[str, Any]]]:
"""Get deliveries with optional filtering"""
try:
params = {}
if status:
params["status"] = status
if date:
params["date"] = date
result = await self.get("suppliers/deliveries", tenant_id=tenant_id, params=params)
deliveries = result.get('deliveries', []) if result else []
logger.info("Retrieved deliveries from suppliers service",
deliveries_count=len(deliveries), tenant_id=tenant_id)
return deliveries
except Exception as e:
logger.error("Error getting deliveries",
error=str(e), tenant_id=tenant_id)
return []
async def update_delivery_status(self, tenant_id: str, delivery_id: str, status: str, notes: Optional[str] = None) -> Optional[Dict[str, Any]]:
"""Update delivery status"""
try:
data = {"status": status}
if notes:
data["notes"] = notes
result = await self.put(f"suppliers/deliveries/{delivery_id}/status", data=data, tenant_id=tenant_id)
if result:
logger.info("Updated delivery status",
delivery_id=delivery_id, status=status, tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error updating delivery status",
error=str(e), delivery_id=delivery_id, tenant_id=tenant_id)
return None
async def get_supplier_order_summaries(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Get supplier order summaries for central bakery dashboard"""
try:
result = await self.get("suppliers/dashboard/order-summaries", tenant_id=tenant_id)
if result:
logger.info("Retrieved supplier order summaries from suppliers service",
tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error getting supplier order summaries",
error=str(e), tenant_id=tenant_id)
return None
# ================================================================
# PERFORMANCE TRACKING
# ================================================================