Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Enhance Inventory Validation and Error Handling in Order Processing #627

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions flask/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,24 @@ def checkout():
print("> validate_inventory", validate_inventory)

with sentry_sdk.start_span(op="process_order", description="function"):
if len(inventory) == 0 or len(quantities) == 0:
sentry_sdk.metrics.incr(key="checkout.failed")
raise Exception("No inventory data available for the requested products")

quantities = cart['quantities']
for cartItem in quantities:
for inventoryItem in inventory:
print("> inventoryItem.count", inventoryItem['count'])
if (validate_inventory and (inventoryItem.count < quantities[cartItem] or quantities[cartItem] >= inventoryItem.count)):
if not isinstance(inventoryItem, (list, tuple)) or len(inventoryItem) < 3:
sentry_sdk.metrics.incr(key="checkout.failed")
raise Exception("Not enough inventory for product")
if len(inventory) == 0 or len(quantities) == 0:
raise Exception("Not enough inventory for product")
raise Exception("Invalid inventory data structure")

inventory_count = int(inventoryItem[2])
requested_quantity = int(quantities[cartItem])

print("> inventory_count", inventory_count)
if validate_inventory and (inventory_count < requested_quantity):
sentry_sdk.metrics.incr(key="checkout.failed")
raise Exception(f"Not enough inventory. Requested: {requested_quantity}, Available: {inventory_count}")

response = make_response("success")
return response
Expand Down
Loading