nagasurendra commited on
Commit
8797416
·
verified ·
1 Parent(s): adfe5ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -5
app.py CHANGED
@@ -466,14 +466,14 @@ def update_quantity():
466
  def checkout():
467
  email = session.get('user_email')
468
  user_id = session.get('user_id')
469
-
470
  if not email or not user_id:
471
  return jsonify({"success": False, "message": "User not logged in"})
472
 
473
  try:
474
  data = request.json
475
  use_reward_points = data.get("useRewardPoints", False)
476
- selected_coupon = data.get("selectedCoupon", "").strip()
477
 
478
  # Fetch cart items
479
  result = sf.query(f"""
@@ -504,12 +504,34 @@ def checkout():
504
  else:
505
  return jsonify({"success": False, "message": "Insufficient reward points to apply."})
506
 
507
- # Apply coupon discount (assume 15% discount for now)
508
  if selected_coupon:
509
  discount += total_price * 0.15 # Example: 15% discount for coupon
510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511
  total_bill = total_price - discount
512
 
 
513
  order_data = {
514
  "Customer_Name__c": user_id,
515
  "Customer_Email__c": email,
@@ -522,7 +544,7 @@ def checkout():
522
 
523
  sf.Order__c.create(order_data)
524
 
525
- # Clear cart
526
  for item in cart_items:
527
  sf.Cart_Item__c.delete(item["Id"])
528
 
@@ -532,7 +554,6 @@ def checkout():
532
  return jsonify({"success": False, "error": str(e)})
533
 
534
 
535
-
536
  @app.route("/order", methods=["GET"])
537
  def order_summary():
538
  email = session.get('user_email') # Fetch logged-in user's email
 
466
  def checkout():
467
  email = session.get('user_email')
468
  user_id = session.get('user_id')
469
+
470
  if not email or not user_id:
471
  return jsonify({"success": False, "message": "User not logged in"})
472
 
473
  try:
474
  data = request.json
475
  use_reward_points = data.get("useRewardPoints", False)
476
+ selected_coupon = data.get("selectedCoupon", "").strip() # Get selected coupon
477
 
478
  # Fetch cart items
479
  result = sf.query(f"""
 
504
  else:
505
  return jsonify({"success": False, "message": "Insufficient reward points to apply."})
506
 
507
+ # Apply coupon discount and remove used coupon
508
  if selected_coupon:
509
  discount += total_price * 0.15 # Example: 15% discount for coupon
510
 
511
+ # Fetch the user's existing coupons
512
+ coupon_query = sf.query(f"""
513
+ SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
514
+ """)
515
+
516
+ if coupon_query["records"]:
517
+ coupon_record = coupon_query["records"][0]
518
+ referral_coupon_id = coupon_record["Id"]
519
+ existing_coupons = coupon_record["Coupon_Code__c"].split("\n") # Convert string to list
520
+
521
+ # Remove only the selected coupon
522
+ updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
523
+
524
+ # Convert list back to a string with newlines
525
+ updated_coupons_str = "\n".join(updated_coupons).strip()
526
+
527
+ # Update the Referral_Coupon__c record with the remaining coupons
528
+ sf.Referral_Coupon__c.update(referral_coupon_id, {
529
+ "Coupon_Code__c": updated_coupons_str
530
+ })
531
+
532
  total_bill = total_price - discount
533
 
534
+ # Store the order details
535
  order_data = {
536
  "Customer_Name__c": user_id,
537
  "Customer_Email__c": email,
 
544
 
545
  sf.Order__c.create(order_data)
546
 
547
+ # Clear cart after successful order
548
  for item in cart_items:
549
  sf.Cart_Item__c.delete(item["Id"])
550
 
 
554
  return jsonify({"success": False, "error": str(e)})
555
 
556
 
 
557
  @app.route("/order", methods=["GET"])
558
  def order_summary():
559
  email = session.get('user_email') # Fetch logged-in user's email