Subbu1304 commited on
Commit
a9f1700
·
verified ·
1 Parent(s): de10225

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -18
app.py CHANGED
@@ -934,33 +934,67 @@ def get_addons():
934
  except Exception as e:
935
  print(f"Error fetching add-ons: {e}")
936
  return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
937
- @app.route('/cart/update_quantity', methods=['POST'])
938
  def update_quantity():
939
  try:
940
- data = request.get_json() # Get JSON data from the frontend
941
- item_name = data['item_name']
942
- customer_email = data['email']
943
- quantity = int(data['quantity']) # Ensure quantity is cast to an integer
944
-
945
- # Update the quantity in Salesforce or your database
946
- cart_item = find_cart_item(item_name, customer_email) # Custom function to find the cart item
 
 
947
 
948
- if cart_item:
949
- # Update the quantity in Salesforce or your database
950
- cart_item['Quantity__c'] = quantity
951
- cart_item['Price__c'] = cart_item['Base_Price__c'] * quantity # Calculate new price based on updated quantity
952
 
953
- # Save changes to Salesforce
954
- update_cart_item_in_salesforce(cart_item) # Custom function to update Salesforce
 
 
 
955
 
956
- # Return a success response with the updated data
957
- return jsonify({"success": True, "new_item_price": cart_item['Price__c']})
958
- else:
959
- return jsonify({"success": False, "error": "Cart item not found."}), 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960
 
961
  except Exception as e:
 
 
962
  return jsonify({"success": False, "error": str(e)}), 500
963
 
 
964
  # @app.route("/cart/update_quantity", methods=["POST"])
965
  # def update_quantity():
966
  # data = request.json # Extract JSON data from the request
 
934
  except Exception as e:
935
  print(f"Error fetching add-ons: {e}")
936
  return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
937
+ @app.route("/cart/update_quantity", methods=["POST"])
938
  def update_quantity():
939
  try:
940
+ data = request.json # Extract JSON data from the request
941
+ email = data.get('email')
942
+ item_name = data.get('item_name')
943
+
944
+ # Convert quantity to an integer and handle invalid data
945
+ try:
946
+ quantity = int(data.get('quantity'))
947
+ except (ValueError, TypeError):
948
+ return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
949
 
950
+ # Validate inputs
951
+ if not email or not item_name or quantity is None:
952
+ return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
 
953
 
954
+ # Query the cart item in Salesforce
955
+ cart_items = sf.query(
956
+ f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
957
+ f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
958
+ )['records']
959
 
960
+ if not cart_items:
961
+ return jsonify({"success": False, "error": "Cart item not found."}), 404
962
+
963
+ # Retrieve the first matching record
964
+ cart_item_id = cart_items[0]['Id']
965
+ base_price = cart_items[0]['Base_Price__c']
966
+ addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
967
+
968
+ # Calculate the new item price (base price * quantity + add-ons price)
969
+ new_item_price = (base_price * quantity) + addons_price
970
+
971
+ # Update the record in Salesforce
972
+ sf.Cart_Item__c.update(cart_item_id, {
973
+ "Quantity__c": quantity,
974
+ "Price__c": new_item_price, # Update base price
975
+ })
976
+
977
+ # Recalculate the subtotal for all items in the cart
978
+ cart_items = sf.query(f"""
979
+ SELECT Price__c, Add_Ons_Price__c
980
+ FROM Cart_Item__c
981
+ WHERE Customer_Email__c = '{email}'
982
+ """)['records']
983
+ new_subtotal = sum(item['Price__c'] for item in cart_items)
984
+
985
+ # Return updated item price and subtotal
986
+ return jsonify({
987
+ "success": True,
988
+ "new_item_price": round(new_item_price, 2), # Ensure two decimal places for pricing
989
+ "subtotal": round(new_subtotal, 2) # Ensure two decimal places for subtotal
990
+ })
991
 
992
  except Exception as e:
993
+ # Error handling
994
+ print(f"Error updating quantity: {str(e)}")
995
  return jsonify({"success": False, "error": str(e)}), 500
996
 
997
+
998
  # @app.route("/cart/update_quantity", methods=["POST"])
999
  # def update_quantity():
1000
  # data = request.json # Extract JSON data from the request