lokesh341 commited on
Commit
0d2fb64
·
verified ·
1 Parent(s): 95eaa6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -21
app.py CHANGED
@@ -47,32 +47,63 @@ def get_menu_items():
47
  return jsonify({"error": "Unable to connect to Salesforce"}), 500
48
 
49
  data = request.json
50
- dietary_preference = data.get('dietary_preference', '').lower()
51
  search_term = data.get('search_term', '').strip()
52
  try:
53
- soql = "SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  if search_term:
55
- soql += f" WHERE Name LIKE '%{search_term}%'"
56
  elif dietary_preference == 'vegetarian':
57
- soql += " WHERE Category__c = 'Veg'"
58
  elif dietary_preference == 'non-vegetarian':
59
- soql += " WHERE Category__c = 'Non-Veg'"
60
- soql += " LIMIT 1" # Return only one item
61
- logger.info(f"Executing SOQL query for Sector_Detail__c: {soql}")
62
- result = sf.query(soql)
63
-
64
- items = [
65
  {
66
  "name": record['Name'],
67
- "image_url": record.get('Image_URL__c', ''),
68
- "category": record.get('Category__c', ''),
69
  "description": record.get('Description__c', 'No description available'),
70
- "source": "Sector_Detail__c"
 
 
 
 
 
 
 
 
 
71
  }
72
- for record in result['records'] if 'Name' in record
73
  ]
 
74
 
75
- logger.info(f"Fetched {len(items)} items for {search_term or dietary_preference}")
76
  return jsonify({"menu_items": items})
77
  except Exception as e:
78
  logger.error(f"Failed to fetch items: {str(e)}")
@@ -112,6 +143,64 @@ def get_sector_item_details():
112
  logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
113
  return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  @app.route('/submit_items', methods=['POST'])
116
  def submit_items():
117
  global sf
@@ -127,16 +216,17 @@ def submit_items():
127
 
128
  try:
129
  ingredient_name = custom_order_name or f"Order_{datetime.now().strftime('%Y%m%d')}_{uuid.uuid4().hex[:8]}"
130
- for item in items:
131
- custom_ingredients = item.get('custom_ingredients', [])
132
- description = f"{item.get('description', 'No description')} - Contains: {item['name']}"
133
- if custom_ingredients:
134
- description += f" with custom ingredients: {', '.join(custom_ingredients)}"
135
 
 
 
 
 
136
  sf.Ingredient_Object__c.create({
137
  'Ingredient_Name__c': ingredient_name,
138
  'Category__c': item.get('category', ''),
139
- 'Description__c': description,
140
  'Image_URL__c': item.get('image_url', ''),
141
  'Quantity__c': item.get('quantity', 1)
142
  })
 
47
  return jsonify({"error": "Unable to connect to Salesforce"}), 500
48
 
49
  data = request.json
50
+ dietary_preference = data.get('dietary_preference', 'both').lower()
51
  search_term = data.get('search_term', '').strip()
52
  try:
53
+ items = []
54
+ # Query Sector_Detail__c for dietary preferences
55
+ if not search_term:
56
+ soql_sector = "SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c"
57
+ if dietary_preference == 'vegetarian':
58
+ soql_sector += " WHERE Category__c = 'Veg'"
59
+ elif dietary_preference == 'non-vegetarian':
60
+ soql_sector += " WHERE Category__c = 'Non-Veg'"
61
+ soql_sector += " LIMIT 200"
62
+ logger.info(f"Executing SOQL query for Sector_Detail__c: {soql_sector}")
63
+ result_sector = sf.query(soql_sector)
64
+ sector_items = [
65
+ {
66
+ "name": record['Name'],
67
+ "image_url": record.get('Image_URL__c', ''),
68
+ "category": record.get('Category__c', ''),
69
+ "description": record.get('Description__c', 'No description available'),
70
+ "source": "Sector_Detail__c"
71
+ }
72
+ for record in result_sector['records'] if 'Name' in record
73
+ ]
74
+ items.extend(sector_items)
75
+
76
+ # Query Menu_Item__c
77
+ soql_menu = "SELECT Name, Description__c, Image1__c, Ingredientsinfo__c, NutritionalInfo__c, Price__c, Sector__c, Spice_Levels__c, Veg_NonVeg__c, Category__c, Dynamic_Dish__c FROM Menu_Item__c"
78
  if search_term:
79
+ soql_menu += f" WHERE Name LIKE '%{search_term}%'"
80
  elif dietary_preference == 'vegetarian':
81
+ soql_menu += " WHERE Veg_NonVeg__c = 'Vegetarian'"
82
  elif dietary_preference == 'non-vegetarian':
83
+ soql_menu += " WHERE Veg_NonVeg__c = 'Non-Vegetarian'"
84
+ soql_menu += " LIMIT 200"
85
+ logger.info(f"Executing SOQL query for Menu_Item__c: {soql_menu}")
86
+ result_menu = sf.query(soql_menu)
87
+ menu_items = [
 
88
  {
89
  "name": record['Name'],
 
 
90
  "description": record.get('Description__c', 'No description available'),
91
+ "image_url": record.get('Image1__c', ''),
92
+ "ingredients": record.get('Ingredientsinfo__c', ''),
93
+ "nutritional_info": record.get('NutritionalInfo__c', ''),
94
+ "price": record.get('Price__c', 0.0),
95
+ "sector": record.get('Sector__c', ''),
96
+ "spice_levels": record.get('Spice_Levels__c', ''),
97
+ "veg_nonveg": record.get('Veg_NonVeg__c', ''),
98
+ "category": record.get('Category__c', ''),
99
+ "dynamic_dish": record.get('Dynamic_Dish__c', False),
100
+ "source": "Menu_Item__c"
101
  }
102
+ for record in result_menu['records'] if 'Name' in record
103
  ]
104
+ items.extend(menu_items)
105
 
106
+ logger.info(f"Fetched {len(items)} items (Sector_Detail__c: {len([i for i in items if i['source'] == 'Sector_Detail__c'])}, Menu_Item__c: {len([i for i in items if i['source'] == 'Menu_Item__c'])}) for {dietary_preference or search_term}")
107
  return jsonify({"menu_items": items})
108
  except Exception as e:
109
  logger.error(f"Failed to fetch items: {str(e)}")
 
143
  logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
144
  return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
145
 
146
+ @app.route('/suggest_items', methods=['POST'])
147
+ def suggest_items():
148
+ global sf
149
+ if not sf:
150
+ sf = get_salesforce_connection()
151
+ if not sf:
152
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
153
+
154
+ search_term = request.json.get('search_term', '').strip()
155
+ if not search_term:
156
+ return jsonify({"error": "Search term is required"}), 400
157
+
158
+ try:
159
+ soql = f"SELECT Ingredient_Name__c, Image_URL__c FROM Ingredient_Object__c WHERE Ingredient_Name__c LIKE '%{search_term}%' LIMIT 10"
160
+ logger.info(f"Executing SOQL query: {soql}")
161
+ result = sf.query(soql)
162
+ suggestions = [
163
+ {"name": record['Ingredient_Name__c'], "image_url": record.get('Image_URL__c', '')}
164
+ for record in result['records'] if 'Ingredient_Name__c' in record
165
+ ]
166
+ logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
167
+ return jsonify({"suggestions": suggestions})
168
+ except Exception as e:
169
+ logger.error(f"Failed to fetch suggestions: {str(e)}")
170
+ return jsonify({"error": f"Failed to fetch suggestions: {str(e)}"}), 500
171
+
172
+ @app.route('/get_item_details', methods=['POST'])
173
+ def get_item_details():
174
+ global sf
175
+ if not sf:
176
+ sf = get_salesforce_connection()
177
+ if not sf:
178
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
179
+
180
+ item_name = request.json.get('item_name', '').strip()
181
+ if not item_name:
182
+ return jsonify({"error": "Item name is required"}), 400
183
+
184
+ try:
185
+ soql = f"SELECT Ingredient_Name__c, Image_URL__c FROM Ingredient_Object__c WHERE Ingredient_Name__c LIKE '%{item_name}%' LIMIT 1"
186
+ logger.info(f"Executing SOQL query: {soql}")
187
+ result = sf.query(soql)
188
+
189
+ if result['totalSize'] == 0:
190
+ return jsonify({"error": f"No item found matching '{item_name}' in Ingredient_Object__c"}), 404
191
+
192
+ record = result['records'][0]
193
+ item_details = {
194
+ "name": record.get('Ingredient_Name__c', ''),
195
+ "image_url": record.get('Image_URL__c', ''),
196
+ "source": "Ingredient_Object__c"
197
+ }
198
+ logger.info(f"Fetched details for '{item_name}' from Ingredient_Object__c")
199
+ return jsonify({"item_details": item_details})
200
+ except Exception as e:
201
+ logger.error(f"Failed to fetch item details: {str(e)}")
202
+ return jsonify({"error": f"Failed to fetch item details: {str(e)}"}), 500
203
+
204
  @app.route('/submit_items', methods=['POST'])
205
  def submit_items():
206
  global sf
 
216
 
217
  try:
218
  ingredient_name = custom_order_name or f"Order_{datetime.now().strftime('%Y%m%d')}_{uuid.uuid4().hex[:8]}"
219
+ item_names = ', '.join(item['name'] for item in items)
220
+ description = f"Contains: {item_names}"
 
 
 
221
 
222
+ for item in items:
223
+ item_description = item['description']
224
+ if item.get('additionalIngredients'):
225
+ item_description += f", with {', '.join(item['additionalIngredients'])}"
226
  sf.Ingredient_Object__c.create({
227
  'Ingredient_Name__c': ingredient_name,
228
  'Category__c': item.get('category', ''),
229
+ 'Description__c': f"{item_description} - {description}",
230
  'Image_URL__c': item.get('image_url', ''),
231
  'Quantity__c': item.get('quantity', 1)
232
  })