diff --git a/app.py b/app.py index 75b5cd2..568cad1 100644 --- a/app.py +++ b/app.py @@ -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://2ba68720d38e42079b243c9c5774e05c@sentry.io/1316515", integrations=[FlaskIntegration()], @@ -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 @@ -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'}