nagasurendra commited on
Commit
d7eda21
·
verified ·
1 Parent(s): b531c61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -118,28 +118,49 @@ def generate_custom_dish():
118
  if not result.get('success'):
119
  return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
120
 
121
- # After ensuring the dish exists, add it to the Cart_Item__c
122
  email = session.get('user_email') # Assuming you have the user's email in session
123
-
124
- # Create the cart item with custom dish details and set other fields to default values
125
- cart_item = {
126
- 'Name': dish_name,
127
- 'Price__c': price,
128
- 'Base_Price__c': price,
129
- 'Image1__c': item_image_url,
130
- 'Quantity__c': 1, # Assuming a default quantity of 1 for the custom dish
131
- 'Add_Ons__c': '', # Set Add_ons__c to empty
132
- 'Add_Ons_Price__c': 0, # Set Add_ons_Price__c to 0
133
- 'Customer_Email__c': email # Associate the custom dish with the logged-in user
134
- }
 
 
 
135
 
136
- # Insert the custom dish as a Cart_Item__c record in Salesforce
137
- cart_result = sf.Cart_Item__c.create(cart_item)
138
 
139
- if cart_result.get('success'):
140
- return redirect(url_for("cart")) # Redirect to the cart page after adding to the cart
141
  else:
142
- return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  except Exception as e:
145
  return jsonify({"success": False, "error": str(e)}), 500
@@ -147,7 +168,6 @@ def generate_custom_dish():
147
 
148
 
149
 
150
-
151
  import re
152
  @app.route("/edit_profile", methods=["GET", "POST"])
153
  def edit_profile():
 
118
  if not result.get('success'):
119
  return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
120
 
121
+ # Query to check if the cart item already exists for the user
122
  email = session.get('user_email') # Assuming you have the user's email in session
123
+ existing_cart_item_query = f"SELECT Id, Name, Quantity__c, Price__c, Base_Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name = '{dish_name}'"
124
+ existing_cart_item_result = sf.query(existing_cart_item_query)
125
+
126
+ if existing_cart_item_result['totalSize'] > 0:
127
+ # If the cart item exists, update the quantity and price
128
+ existing_cart_item = existing_cart_item_result['records'][0]
129
+ new_quantity = existing_cart_item['Quantity__c'] + 1
130
+ new_price = existing_cart_item['Base_Price__c'] * new_quantity
131
+
132
+ # Update the cart item with new quantity and price
133
+ updated_cart_item = {
134
+ 'Id': existing_cart_item['Id'],
135
+ 'Quantity__c': new_quantity,
136
+ 'Price__c': new_price
137
+ }
138
 
139
+ # Update the existing Cart_Item__c record in Salesforce
140
+ cart_update_result = sf.Cart_Item__c.update(updated_cart_item)
141
 
142
+ if not cart_update_result.get('success'):
143
+ return jsonify({"success": False, "error": "Failed to update cart item in Salesforce"}), 500
144
  else:
145
+ # If the cart item does not exist, create a new one
146
+ cart_item = {
147
+ 'Name': dish_name,
148
+ 'Price__c': price,
149
+ 'Base_Price__c': price,
150
+ 'Image1__c': item_image_url,
151
+ 'Quantity__c': 1, # Set the quantity to 1 for the new cart item
152
+ 'Add_Ons__c': '', # Set Add_ons__c to empty
153
+ 'Add_Ons_Price__c': 0, # Set Add_ons_Price__c to 0
154
+ 'Customer_Email__c': email # Associate the custom dish with the logged-in user
155
+ }
156
+
157
+ # Insert the custom dish as a Cart_Item__c record in Salesforce
158
+ cart_result = sf.Cart_Item__c.create(cart_item)
159
+
160
+ if not cart_result.get('success'):
161
+ return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
162
+
163
+ return redirect(url_for("cart")) # Redirect to the cart page after adding to the cart
164
 
165
  except Exception as e:
166
  return jsonify({"success": False, "error": str(e)}), 500
 
168
 
169
 
170
 
 
171
  import re
172
  @app.route("/edit_profile", methods=["GET", "POST"])
173
  def edit_profile():