nagasurendra commited on
Commit
f85d242
·
verified ·
1 Parent(s): 527d54a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -268,35 +268,47 @@ def get_addons():
268
  def update_quantity():
269
  data = request.json # Extract JSON data from the request
270
  email = data.get('email') # Customer email
271
- item_name = data.get('item_name') # Item name (Cart Item Name in Salesforce)
272
  quantity = data.get('quantity') # New quantity
273
 
274
  # Validate inputs
275
- if not email or not item_name:
276
- return jsonify({"success": False, "error": "Email and item name are required."}), 400
277
 
278
  try:
279
- # Query the cart item using the correct fields
280
  cart_items = sf.query(
281
- f"SELECT Id, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
 
282
  )['records']
283
 
284
- if cart_items:
285
- cart_item_id = cart_items[0]['Id']
286
- base_price = cart_items[0]['Base_Price__c']
287
- addons_price = sum([float(price) for price in cart_items[0].get('Add_Ons__c', "0").split("$")[1::2]]) # Example parsing logic
288
-
289
- new_price = (base_price + addons_price) * quantity
290
-
291
- # Update Salesforce record
292
- sf.Cart_Item__c.update(cart_item_id, {
293
- "Quantity__c": quantity,
294
- "Price__c": new_price
295
- })
296
-
297
- return jsonify({"success": True, "new_quantity": quantity, "new_price": new_price})
 
 
 
 
 
 
 
 
 
298
 
 
299
  except Exception as e:
 
300
  return jsonify({"success": False, "error": str(e)}), 500
301
 
302
 
 
268
  def update_quantity():
269
  data = request.json # Extract JSON data from the request
270
  email = data.get('email') # Customer email
271
+ item_name = data.get('item_name') # Item name
272
  quantity = data.get('quantity') # New quantity
273
 
274
  # Validate inputs
275
+ if not email or not item_name or quantity is None:
276
+ return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
277
 
278
  try:
279
+ # Query the cart item in Salesforce
280
  cart_items = sf.query(
281
+ f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons__c FROM Cart_Item__c "
282
+ f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
283
  )['records']
284
 
285
+ if not cart_items:
286
+ return jsonify({"success": False, "error": "Cart item not found."}), 404
287
+
288
+ # Retrieve the first matching record
289
+ cart_item_id = cart_items[0]['Id']
290
+ base_price = cart_items[0]['Base_Price__c']
291
+ addons_string = cart_items[0].get('Add_Ons__c', "None")
292
+
293
+ # Calculate the add-ons price
294
+ addons_price = 0
295
+ if addons_string and addons_string != "None":
296
+ addons_price = sum(
297
+ [float(price) for price in addons_string.split("$")[1::2]] # Example parsing logic
298
+ )
299
+
300
+ # Calculate the new price
301
+ new_price = (base_price + addons_price) * quantity
302
+
303
+ # Update the record in Salesforce
304
+ sf.Cart_Item__c.update(cart_item_id, {
305
+ "Quantity__c": quantity,
306
+ "Price__c": new_price
307
+ })
308
 
309
+ return jsonify({"success": True, "new_quantity": quantity, "new_price": new_price})
310
  except Exception as e:
311
+ print(f"Error updating quantity: {str(e)}")
312
  return jsonify({"success": False, "error": str(e)}), 500
313
 
314