This commit is contained in:
Urtzi Alfaro
2026-01-01 19:01:33 +01:00
parent d6728b4bd8
commit 93c9475239
10 changed files with 286 additions and 84 deletions

View File

@@ -342,11 +342,67 @@ class InventoryService:
logger.info("Stock added successfully", stock_id=stock.id, quantity=stock.current_quantity)
return response
except Exception as e:
logger.error("Failed to add stock", error=str(e), tenant_id=tenant_id)
raise
async def bulk_add_stock(
self,
bulk_data: 'BulkStockCreate',
tenant_id: UUID,
user_id: Optional[UUID] = None
) -> 'BulkStockResponse':
"""Bulk add stock entries for efficient batch operations"""
import uuid as uuid_lib
from app.schemas.inventory import BulkStockCreate, BulkStockResult, BulkStockResponse
transaction_id = str(uuid_lib.uuid4())
results = []
total_created = 0
total_failed = 0
for index, stock_data in enumerate(bulk_data.stocks):
try:
stock_response = await self.add_stock(stock_data, tenant_id, user_id)
results.append(BulkStockResult(
index=index,
success=True,
stock=stock_response,
error=None
))
total_created += 1
except Exception as e:
logger.warning(
"Failed to create stock in bulk operation",
index=index,
ingredient_id=stock_data.ingredient_id,
error=str(e)
)
results.append(BulkStockResult(
index=index,
success=False,
stock=None,
error=str(e)
))
total_failed += 1
logger.info(
"Bulk stock operation completed",
transaction_id=transaction_id,
total_requested=len(bulk_data.stocks),
total_created=total_created,
total_failed=total_failed
)
return BulkStockResponse(
total_requested=len(bulk_data.stocks),
total_created=total_created,
total_failed=total_failed,
results=results,
transaction_id=transaction_id
)
async def consume_stock(
self,
ingredient_id: UUID,