Skip to content
Open
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
22 changes: 22 additions & 0 deletions order_service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ def create_order():
shipping_address_id = data.get('shippingAddressId')
idmp_key = request.headers.get('Idempotency-Key')

# --- BUG FIX: Check idempotency key FIRST, before any external service calls ---
# Previously, the key was only enforced at DB INSERT (via UNIQUE KEY constraint),
# meaning duplicate requests would still call User/Product services and reserve
# stock before failing. Now we short-circuit immediately if the key already exists.
if idmp_key:
conn = get_db_connection()
if not conn:
return jsonify({'error': 'Database connection failed'}), 500
try:
cur = conn.cursor(dictionary=True)
cur.execute(
"SELECT id, status FROM orders WHERE idempotency_key = %s LIMIT 1",
(idmp_key,)
)
existing = cur.fetchone()
finally:
conn.close()
if existing:
# Return the original order response — idempotent, no side effects
return jsonify({'id': existing['id'], 'status': existing['status']}), 200
# --- END BUG FIX ---

ok, err = _validate_items(items)
if not ok:
return jsonify({'error': err}), 400
Expand Down