lokeshloki143 commited on
Commit
8b43ef0
·
verified ·
1 Parent(s): 60bf485

Update combined_summary.py

Browse files
Files changed (1) hide show
  1. combined_summary.py +68 -12
combined_summary.py CHANGED
@@ -1,10 +1,5 @@
1
- from flask import Blueprint, render_template, session, redirect, url_for
2
  from salesforce import get_salesforce_connection
3
- from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
4
- import os
5
- import re
6
- from salesforce import get_salesforce_connection
7
-
8
 
9
  combined_summary_blueprint = Blueprint('combined_summary', __name__)
10
 
@@ -76,7 +71,7 @@ def combined_summary():
76
  order = order_result["records"][0]
77
  order_details = order.get("Order_Details__c", "")
78
  order_items = []
79
- sector_names = set() # Use a set to ensure sector names are unique
80
 
81
  for line in order_details.split('\n'):
82
  item_parts = line.split('|')
@@ -86,7 +81,7 @@ def combined_summary():
86
  safe_item_name = escape_soql(item_name)
87
 
88
  menu_query = f"""
89
- SELECT Name, Price__c, Image1__c,
90
  Ingredient_1__r.Ingredient_Name__c, Ingredient_1__r.Ingredient_Image__c,
91
  Ingredient_1__r.Health_Benefits__c, Ingredient_1__r.Fun_Facts__c,
92
  Ingredient_2__r.Ingredient_Name__c, Ingredient_2__r.Ingredient_Image__c,
@@ -121,10 +116,11 @@ def combined_summary():
121
 
122
  # Process the Sector__c field from Menu_Item__c
123
  if menu_item.get('Sector__c'):
124
- sector_names.update(menu_item['Sector__c'].split(',')) # Add sectors to the set
125
 
126
- # Only add the item if ingredients are present
127
  order_items.append({
 
128
  "name": item_name,
129
  "price": menu_item.get("Price__c", 0),
130
  "image_url": menu_item.get("Image1__c", ''),
@@ -158,9 +154,69 @@ def combined_summary():
158
  progress_percentage=round(progress_percentage),
159
  points_needed_for_next_tier=round(points_needed_for_next_tier),
160
  order_items=order_items,
161
- sector_details=sector_details
 
162
  )
163
 
164
  except Exception as e:
165
  print(f"Error in combined_summary: {str(e)}")
166
- return f"Error: {str(e)}", 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Blueprint, render_template, session, redirect, url_for, request, jsonify
2
  from salesforce import get_salesforce_connection
 
 
 
 
 
3
 
4
  combined_summary_blueprint = Blueprint('combined_summary', __name__)
5
 
 
71
  order = order_result["records"][0]
72
  order_details = order.get("Order_Details__c", "")
73
  order_items = []
74
+ sector_names = set()
75
 
76
  for line in order_details.split('\n'):
77
  item_parts = line.split('|')
 
81
  safe_item_name = escape_soql(item_name)
82
 
83
  menu_query = f"""
84
+ SELECT Id, Name, Price__c, Image1__c,
85
  Ingredient_1__r.Ingredient_Name__c, Ingredient_1__r.Ingredient_Image__c,
86
  Ingredient_1__r.Health_Benefits__c, Ingredient_1__r.Fun_Facts__c,
87
  Ingredient_2__r.Ingredient_Name__c, Ingredient_2__r.Ingredient_Image__c,
 
116
 
117
  # Process the Sector__c field from Menu_Item__c
118
  if menu_item.get('Sector__c'):
119
+ sector_names.update(menu_item['Sector__c'].split(','))
120
 
121
+ # Add item with Salesforce Id
122
  order_items.append({
123
+ "id": menu_item.get("Id", ''),
124
  "name": item_name,
125
  "price": menu_item.get("Price__c", 0),
126
  "image_url": menu_item.get("Image1__c", ''),
 
154
  progress_percentage=round(progress_percentage),
155
  points_needed_for_next_tier=round(points_needed_for_next_tier),
156
  order_items=order_items,
157
+ sector_details=sector_details,
158
+ validity_year=2025 # Add validity_year for template
159
  )
160
 
161
  except Exception as e:
162
  print(f"Error in combined_summary: {str(e)}")
163
+ return f"Error: {str(e)}", 500
164
+
165
+ @combined_summary_blueprint.route('/reorder', methods=['POST'])
166
+ def reorder():
167
+ email = session.get('user_email')
168
+ if not email:
169
+ return jsonify({'success': False, 'message': 'Please log in to reorder.'}), 401
170
+
171
+ try:
172
+ data = request.get_json()
173
+ item_id = data.get('item_id')
174
+ if not item_id:
175
+ return jsonify({'success': False, 'message': 'Item ID is required.'}), 400
176
+
177
+ # Sanitize item_id for SOQL
178
+ safe_item_id = escape_soql(item_id)
179
+
180
+ # Verify item exists in Menu_Item__c
181
+ item_query = f"""
182
+ SELECT Id, Name, Price__c
183
+ FROM Menu_Item__c
184
+ WHERE Id = '{safe_item_id}'
185
+ """
186
+ item_result = sf.query_all(item_query)
187
+ if not item_result.get("records"):
188
+ return jsonify({'success': False, 'message': 'Item not found.'}), 404
189
+
190
+ item = item_result["records"][0]
191
+ item_name = item.get("Name", "Item")
192
+
193
+ # Check if item is already in cart
194
+ safe_email = escape_soql(email)
195
+ cart_query = f"""
196
+ SELECT Id, Quantity__c
197
+ FROM Cart__c
198
+ WHERE Customer_Email__c = '{safe_email}' AND Menu_Item_Id__c = '{safe_item_id}'
199
+ """
200
+ cart_result = sf.query_all(cart_query)
201
+
202
+ if cart_result.get("records"):
203
+ # Update existing cart item
204
+ cart_id = cart_result["records"][0]["Id"]
205
+ current_quantity = cart_result["records"][0].get("Quantity__c", 1)
206
+ sf.Cart__c.update(cart_id, {'Quantity__c': current_quantity + 1})
207
+ else:
208
+ # Create new cart item
209
+ sf.Cart__c.create({
210
+ 'Customer_Email__c': email,
211
+ 'Menu_Item_Id__c': item_id,
212
+ 'Quantity__c': 1
213
+ })
214
+
215
+ # Set session variable for menu page popup
216
+ session['reorder_success'] = f"{item_name} added to cart!"
217
+
218
+ return jsonify({'success': True, 'message': f'{item_name} added to cart!'}), 200
219
+
220
+ except Exception as e:
221
+ print(f"Error in reorder: {str(e)}")
222
+ return jsonify({'success': False, 'message': 'An error occurred while reordering.'}), 500