Skip to content

Implement inventory management and error handling for checkout #18

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

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
72 changes: 57 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

class InsufficientInventoryError(Exception):
def __init__(self, item_id, requested, available):
self.item_id = item_id
self.requested = requested
self.available = available
super().__init__(f"Not enough inventory for item ID {item_id}. Requested: {requested}, Available: {available}")

sentry_sdk.init(
dsn="https://[email protected]/1316515",
integrations=[FlaskIntegration()],
Expand Down Expand Up @@ -35,15 +42,25 @@ def unhandled_exception():
'hammer': 1
}

def process_order(cart):
def process_order(cart_items_with_quantity):
global Inventory
tempInventory = Inventory
for item in cart:
if Inventory[item['id']] <= 0:
raise Exception("Not enough inventory for " + item['id'])
else:
tempInventory[item['id']] -= 1
print 'Success: ' + item['id'] + ' was purchased, remaining stock is ' + str(tempInventory[item['id']])
tempInventory = Inventory.copy()
for item in cart_items_with_quantity:
item_id_str = str(item['id'])
quantity_requested = item['quantity']

# Check if item exists in inventory
if item_id_str not in tempInventory:
raise Exception(f"Item {item_id_str} not found in inventory")

# Check if we have enough inventory
if tempInventory[item_id_str] < quantity_requested:
raise InsufficientInventoryError(item_id_str, quantity_requested, tempInventory[item_id_str])

# Decrement inventory by requested quantity
tempInventory[item_id_str] -= quantity_requested
print('Success: ' + item_id_str + ' was purchased, remaining stock is ' + str(tempInventory[item_id_str]))

Inventory = tempInventory

@app.before_request
Expand All @@ -65,11 +82,36 @@ def sentry_event_context():

@app.route('/checkout', methods=['POST'])
def checkout():

order = json.loads(request.data)
print "Processing order for: " + order["email"]
cart = order["cart"]
try:
order_data = json.loads(request.data)
print("Processing order for: " + order_data.get("form", {}).get("email", "unknown"))

cart = order_data.get("cart", {})
cart_items = cart.get("items", [])

# Process the cart items for the order
processed_cart_for_order = []
for item in cart_items:
processed_cart_for_order.append({
'id': item['id'],
'quantity': item['quantity']
})

process_order(processed_cart_for_order)

return json.dumps({"message": "Checkout successful"}), 200, {'ContentType':'application/json'}

process_order(cart)

return 'Success'
except InsufficientInventoryError as e:
sentry_sdk.capture_exception(e)
response_data = {
"error": "Insufficient inventory",
"itemId": str(e.item_id),
"requested": e.requested,
"available": e.available,
"message": str(e)
}
return json.dumps(response_data), 409, {'ContentType':'application/json'}

except Exception as e:
sentry_sdk.capture_exception(e)
return json.dumps({"error": "An unexpected error occurred during checkout."}), 500, {'ContentType':'application/json'}
Loading