nagasurendra commited on
Commit
83e9459
·
verified ·
1 Parent(s): 22533a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -82,12 +82,12 @@ def generate_custom_dish():
82
  query = f"SELECT Id, Name, Total_Ordered__c FROM Custom_Dish__c WHERE Name = '{dish_name}'"
83
  result = sf.query(query)
84
 
85
- # If the dish exists, use the existing record ID and increment Total_Ordered__c
86
  if result['records']:
87
  # Custom dish already exists
88
  custom_dish = result['records'][0] # Get the existing custom dish record
89
- custom_dish_id = custom_dish['Id']
90
- total_ordered = custom_dish.get('Total_Ordered__c', 0)
91
 
92
  # Increment the Total_Ordered__c by 1
93
  new_total_ordered = total_ordered + 1
@@ -100,12 +100,11 @@ def generate_custom_dish():
100
  # Update the existing custom dish in Salesforce
101
  update_result = sf.Custom_Dish__c.update(custom_dish_id, update_data)
102
 
103
- # Check if update was successful
104
- if not update_result.get('success'):
105
  return jsonify({"success": False, "error": "Failed to update custom dish in Salesforce"}), 500
106
 
107
  # Set up cart item using the existing custom dish details
108
- price = custom_dish['Price__c'] # Use the existing price
109
  email = session.get('user_email') # Assuming you have the user's email in session
110
 
111
  # Create the cart item with custom dish details
@@ -125,7 +124,7 @@ def generate_custom_dish():
125
  # Insert the cart item as a Cart_Item__c record in Salesforce
126
  cart_result = sf.Cart_Item__c.create(cart_item)
127
 
128
- if cart_result.get('success'):
129
  return redirect(url_for("cart")) # Redirect to the cart page after adding to the cart
130
  else:
131
  return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
@@ -156,7 +155,7 @@ def generate_custom_dish():
156
  # Insert the custom dish into Salesforce
157
  result = sf.Custom_Dish__c.create(custom_dish)
158
 
159
- if result.get('success'):
160
  # After creating the custom dish, add it to the Cart_Item__c
161
  email = session.get('user_email') # Assuming you have the user's email in session
162
 
@@ -169,14 +168,13 @@ def generate_custom_dish():
169
  'Quantity__c': 1, # Assuming a default quantity of 1 for the custom dish
170
  'Add_Ons__c': '', # Set Add_ons__c to empty
171
  'Add_Ons_Price__c': 0, # Set Add_Ons_Price__c to 0
172
- 'Instructions__c': '', # Use the custom dish description as the instructions
173
  'Customer_Email__c': email # Associate the custom dish with the logged-in user
174
  }
175
 
176
  # Insert the custom dish as a Cart_Item__c record in Salesforce
177
  cart_result = sf.Cart_Item__c.create(cart_item)
178
 
179
- if cart_result.get('success'):
180
  return redirect(url_for("cart")) # Redirect to the cart page after adding to the cart
181
  else:
182
  return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
 
82
  query = f"SELECT Id, Name, Total_Ordered__c FROM Custom_Dish__c WHERE Name = '{dish_name}'"
83
  result = sf.query(query)
84
 
85
+ # If there are no records returned, result['records'] will be empty
86
  if result['records']:
87
  # Custom dish already exists
88
  custom_dish = result['records'][0] # Get the existing custom dish record
89
+ custom_dish_id = custom_dish.get('Id') # Safe way to access 'Id'
90
+ total_ordered = custom_dish.get('Total_Ordered__c', 0) # Safe way to access 'Total_Ordered__c'
91
 
92
  # Increment the Total_Ordered__c by 1
93
  new_total_ordered = total_ordered + 1
 
100
  # Update the existing custom dish in Salesforce
101
  update_result = sf.Custom_Dish__c.update(custom_dish_id, update_data)
102
 
103
+ if not update_result.get('success', False): # Ensure 'success' exists
 
104
  return jsonify({"success": False, "error": "Failed to update custom dish in Salesforce"}), 500
105
 
106
  # Set up cart item using the existing custom dish details
107
+ price = custom_dish.get('Price__c', 0) # Safe way to access 'Price__c'
108
  email = session.get('user_email') # Assuming you have the user's email in session
109
 
110
  # Create the cart item with custom dish details
 
124
  # Insert the cart item as a Cart_Item__c record in Salesforce
125
  cart_result = sf.Cart_Item__c.create(cart_item)
126
 
127
+ if cart_result.get('success', False):
128
  return redirect(url_for("cart")) # Redirect to the cart page after adding to the cart
129
  else:
130
  return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
 
155
  # Insert the custom dish into Salesforce
156
  result = sf.Custom_Dish__c.create(custom_dish)
157
 
158
+ if result.get('success', False):
159
  # After creating the custom dish, add it to the Cart_Item__c
160
  email = session.get('user_email') # Assuming you have the user's email in session
161
 
 
168
  'Quantity__c': 1, # Assuming a default quantity of 1 for the custom dish
169
  'Add_Ons__c': '', # Set Add_ons__c to empty
170
  'Add_Ons_Price__c': 0, # Set Add_Ons_Price__c to 0
 
171
  'Customer_Email__c': email # Associate the custom dish with the logged-in user
172
  }
173
 
174
  # Insert the custom dish as a Cart_Item__c record in Salesforce
175
  cart_result = sf.Cart_Item__c.create(cart_item)
176
 
177
+ if cart_result.get('success', False):
178
  return redirect(url_for("cart")) # Redirect to the cart page after adding to the cart
179
  else:
180
  return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500