feat: AI coordination protocol and advanced audit operations
This commit is contained in:
@@ -60,3 +60,65 @@ def check_out_item(op: schemas.OperationCreate, db: Session = Depends(get_db)):
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
return item
|
||||
|
||||
@router.post("/trash", response_model=schemas.Item)
|
||||
def trash_item(op: schemas.TrashOperationCreate, db: Session = Depends(get_db)):
|
||||
if op.quantity <= 0:
|
||||
raise HTTPException(status_code=400, detail="Quantity must be greater than zero")
|
||||
|
||||
item = db.query(models.Item).filter(models.Item.barcode == op.barcode).first()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
|
||||
if item.quantity < op.quantity:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Insufficient stock to trash")
|
||||
|
||||
# Update quantity
|
||||
item.quantity -= op.quantity
|
||||
|
||||
# Create Mandatory Audit Log with TRASH action and reason
|
||||
audit = models.AuditLog(
|
||||
user_id=op.user_id,
|
||||
action=f"TRASH: {op.reason}",
|
||||
target_item_id=item.id,
|
||||
quantity_change=-op.quantity
|
||||
)
|
||||
|
||||
db.add(audit)
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
return item
|
||||
|
||||
@router.post("/bulk-check-out")
|
||||
def bulk_check_out(bulk_op: schemas.BulkOperationCreate, db: Session = Depends(get_db)):
|
||||
results = {"success": [], "errors": []}
|
||||
|
||||
for op in bulk_op.items:
|
||||
try:
|
||||
item = db.query(models.Item).filter(models.Item.barcode == op.barcode).first()
|
||||
if not item:
|
||||
results["errors"].append({"barcode": op.barcode, "error": "Not found"})
|
||||
continue
|
||||
|
||||
if item.quantity < op.quantity:
|
||||
results["errors"].append({"barcode": op.barcode, "error": f"Insufficient stock (Available: {item.quantity})"})
|
||||
continue
|
||||
|
||||
# Update quantity
|
||||
item.quantity -= op.quantity
|
||||
|
||||
# Log individual audit for this item
|
||||
audit = models.AuditLog(
|
||||
user_id=bulk_op.user_id,
|
||||
action="BULK_CHECK_OUT",
|
||||
target_item_id=item.id,
|
||||
quantity_change=-op.quantity
|
||||
)
|
||||
db.add(audit)
|
||||
results["success"].append({"barcode": op.barcode, "new_quantity": item.quantity})
|
||||
|
||||
except Exception as e:
|
||||
results["errors"].append({"barcode": op.barcode, "error": str(e)})
|
||||
|
||||
db.commit()
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user