This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Improve checkout process with error handling and inventory management #33
Draft
seer-by-sentry
wants to merge
1
commit into
master
Choose a base branch
from
seer/checkout-improvements-hfluAi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import os | ||
from flask import Flask, request, json, abort | ||
from flask import Flask, request, json, abort, jsonify | ||
from flask_cors import CORS | ||
|
||
import sentry_sdk | ||
from sentry_sdk.integrations.flask import FlaskIntegration | ||
|
||
class InsufficientInventoryError(Exception): | ||
def __init__(self, message, product_id=None): | ||
super().__init__(message) | ||
self.product_id = product_id | ||
|
||
sentry_sdk.init( | ||
dsn="https://[email protected]/1316515", | ||
integrations=[FlaskIntegration()], | ||
|
@@ -30,20 +35,32 @@ | |
obj['keyDoesntExist'] | ||
|
||
Inventory = { | ||
'wrench': 1, | ||
'nails': 1, | ||
'hammer': 1 | ||
'wrench': 10, | ||
'nails': 100, | ||
'hammer': 5, | ||
'3': 1, # Plant Mood | ||
'4': 2, # Botana Voice | ||
'5': 1, # Plant Stroller | ||
'6': 1, # Plant Nodes | ||
'8': 1 # Plant Mood5 | ||
} | ||
|
||
def process_order(cart): | ||
def process_order(cart_items, cart_quantities): | ||
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 in cart_items: | ||
product_id = str(item_in_cart['id']) | ||
if product_id not in tempInventory: | ||
raise ValueError(f"Product ID '{product_id}' not found.") | ||
requested_quantity = cart_quantities.get(product_id) | ||
if not isinstance(requested_quantity, int) or requested_quantity <= 0: | ||
raise ValueError(f"Invalid quantity for product ID '{product_id}'.") | ||
available_stock = tempInventory[product_id] | ||
if available_stock < requested_quantity: | ||
raise InsufficientInventoryError( | ||
f"Not enough inventory for product ID '{product_id}'.", product_id=product_id) | ||
tempInventory[product_id] -= requested_quantity | ||
print(f'Success: {product_id} was purchased, remaining stock is {tempInventory[product_id]}') | ||
Inventory = tempInventory | ||
|
||
@app.before_request | ||
|
@@ -65,11 +82,27 @@ | |
|
||
@app.route('/checkout', methods=['POST']) | ||
def checkout(): | ||
|
||
order = json.loads(request.data) | ||
print "Processing order for: " + order["email"] | ||
cart = order["cart"] | ||
|
||
process_order(cart) | ||
|
||
return 'Success' | ||
try: | ||
order_data = request.get_json() | ||
print(f"Processing order for: {order_data['form']['email']}") | ||
cart = order_data["cart"] | ||
|
||
# Extract cart items and quantities from the cart object | ||
cart_items = cart.get("items", []) | ||
cart_quantities = cart.get("quantities", {}) | ||
|
||
process_order(cart_items, cart_quantities) | ||
|
||
return jsonify({"message": "Order successful"}), 200 | ||
except InsufficientInventoryError as e: | ||
return jsonify({ | ||
"error": "InsufficientInventory", | ||
"message": str(e), | ||
"product_id": e.product_id | ||
}), 409 # Use 409 Conflict | ||
except ValueError as e: | ||
return jsonify({"error": "BadRequest", "message": str(e)}), 400 # Use 400 Bad Request | ||
Check warningCode scanning / CodeQL Information exposure through an exception Medium Stack trace information Error loading related location Loading |
||
except Exception as e: | ||
# Fallback for unexpected errors | ||
sentry_sdk.capture_exception(e) | ||
return jsonify({"error": "InternalServerError", "message": "An unexpected error occurred."}), 500 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium