hisham-cmd commited on
Commit
37cfa24
·
verified ·
1 Parent(s): a00e821

Update backend with high-quality catalog and fixes

Browse files
Files changed (2) hide show
  1. app/api/orders.py +16 -4
  2. app/schemas/order.py +1 -0
app/api/orders.py CHANGED
@@ -129,9 +129,10 @@ def checkout(
129
  )
130
  db.add(payment)
131
 
132
- # 6. Clear Cart
133
- db.query(CartItem).filter(CartItem.cart_id == cart.id).delete()
134
- cart.coupon_id = None
 
135
 
136
  db.commit()
137
  # Eagerly load the order with all needed relations for the response
@@ -350,7 +351,8 @@ def upload_payment_receipt(
350
  order_id: int,
351
  file: UploadFile = File(...),
352
  current_user_id: Optional[int] = Depends(get_optional_current_user),
353
- db: Session = Depends(get_db)
 
354
  ):
355
  if current_user_id:
356
  order = db.query(Order).filter(Order.id == order_id, Order.user_id == current_user_id).first()
@@ -383,6 +385,16 @@ def upload_payment_receipt(
383
  payment.receipt_url = receipt_url
384
  payment.status = PaymentStatus.PENDING # Awaiting admin approval
385
 
 
 
 
 
 
 
 
 
 
 
386
  db.commit()
387
 
388
  return {
 
129
  )
130
  db.add(payment)
131
 
132
+ # 6. Clear Cart (Conditional)
133
+ if request.clear_cart:
134
+ db.query(CartItem).filter(CartItem.cart_id == cart.id).delete()
135
+ cart.coupon_id = None
136
 
137
  db.commit()
138
  # Eagerly load the order with all needed relations for the response
 
351
  order_id: int,
352
  file: UploadFile = File(...),
353
  current_user_id: Optional[int] = Depends(get_optional_current_user),
354
+ db: Session = Depends(get_db),
355
+ x_cart_id: Optional[str] = Header(None)
356
  ):
357
  if current_user_id:
358
  order = db.query(Order).filter(Order.id == order_id, Order.user_id == current_user_id).first()
 
385
  payment.receipt_url = receipt_url
386
  payment.status = PaymentStatus.PENDING # Awaiting admin approval
387
 
388
+ # 5. Finally Clear Cart (if requested via bank transfer follow-up)
389
+ # This ensures the cart is cleared once the final proof of payment is submitted
390
+ from app.api.cart import get_or_create_cart
391
+ try:
392
+ cart = get_or_create_cart(db, user_id=current_user_id, session_id=x_cart_id)
393
+ db.query(CartItem).filter(CartItem.cart_id == cart.id).delete()
394
+ cart.coupon_id = None
395
+ except:
396
+ pass # Cart might already be empty or not found
397
+
398
  db.commit()
399
 
400
  return {
app/schemas/order.py CHANGED
@@ -76,6 +76,7 @@ class OrderCreate(BaseModel):
76
  payment_method: str = "card"
77
  bank_account_id: Optional[str] = None
78
  crypto_network_id: Optional[str] = None
 
79
 
80
  class OrderUpdateStatus(BaseModel):
81
  status: OrderStatus
 
76
  payment_method: str = "card"
77
  bank_account_id: Optional[str] = None
78
  crypto_network_id: Optional[str] = None
79
+ clear_cart: bool = True
80
 
81
  class OrderUpdateStatus(BaseModel):
82
  status: OrderStatus