Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,15 +86,15 @@ def index():
|
|
| 86 |
def dashboard():
|
| 87 |
return render_template("dashboard.html") # Render the dashboard template
|
| 88 |
|
|
|
|
| 89 |
@app.route('/login', methods=['POST'])
|
| 90 |
def login():
|
| 91 |
# Get data from voice bot (name, email, phone number)
|
| 92 |
data = request.json # Assuming voice bot sends JSON data
|
| 93 |
-
name = data.get('name')
|
| 94 |
email = data.get('email')
|
| 95 |
phone_number = data.get('phone_number')
|
| 96 |
|
| 97 |
-
if not
|
| 98 |
return jsonify({'error': 'Missing required fields'}), 400
|
| 99 |
|
| 100 |
try:
|
|
@@ -103,8 +103,9 @@ def login():
|
|
| 103 |
result = sf.query(query)
|
| 104 |
|
| 105 |
if result['totalSize'] > 0:
|
| 106 |
-
# User exists and credentials match, proceed to
|
| 107 |
-
|
|
|
|
| 108 |
else:
|
| 109 |
# User not found or credentials don't match
|
| 110 |
return jsonify({'error': 'Invalid email or phone number'}), 401 # Unauthorized
|
|
@@ -149,6 +150,7 @@ def submit():
|
|
| 149 |
return jsonify({'error': str(e)}), 500
|
| 150 |
|
| 151 |
|
|
|
|
| 152 |
@app.route("/menu", methods=["GET"])
|
| 153 |
def menu_page():
|
| 154 |
try:
|
|
@@ -156,47 +158,61 @@ def menu_page():
|
|
| 156 |
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
| 157 |
result = sf.query(query)
|
| 158 |
|
| 159 |
-
#
|
| 160 |
return render_template("menu_page.html", menu_items=result['records'])
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
return jsonify({"error": f"Failed to fetch menu items from Salesforce: {str(e)}"}), 500
|
| 164 |
|
| 165 |
|
|
|
|
| 166 |
@app.route("/order", methods=["POST"])
|
| 167 |
def place_order():
|
| 168 |
order_data = request.json
|
| 169 |
item_name = order_data.get('item')
|
| 170 |
quantity = order_data.get('quantity')
|
|
|
|
| 171 |
|
| 172 |
-
if not item_name or not quantity:
|
| 173 |
-
return jsonify({"error": "Item and
|
| 174 |
|
| 175 |
try:
|
| 176 |
-
#
|
| 177 |
customer_login = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{user_email}' LIMIT 1")
|
|
|
|
|
|
|
|
|
|
| 178 |
customer_id = customer_login['records'][0]['Id']
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
order = {
|
| 181 |
'Order_Item__c': item_name,
|
| 182 |
'Quantity__c': quantity,
|
| 183 |
-
'Customer__c': customer_id, # Linking the order to the customer
|
| 184 |
'Status__c': 'Pending',
|
| 185 |
-
'Total_Amount__c':
|
| 186 |
}
|
| 187 |
|
| 188 |
order_response = sf.Order__c.create(order)
|
| 189 |
return jsonify({"success": True, "order_id": order_response['id']}), 200
|
|
|
|
| 190 |
except Exception as e:
|
| 191 |
return jsonify({"error": f"Error placing the order: {str(e)}"}), 500
|
| 192 |
|
| 193 |
-
|
| 194 |
-
# Helper function to calculate the total price (implement as per your pricing logic)
|
| 195 |
def calculate_total_price(item_name, quantity):
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# Route for the order summary page
|
| 202 |
@app.route("/order-summary", methods=["GET"])
|
|
|
|
| 86 |
def dashboard():
|
| 87 |
return render_template("dashboard.html") # Render the dashboard template
|
| 88 |
|
| 89 |
+
# Login endpoint: Authenticate the user by email and phone number
|
| 90 |
@app.route('/login', methods=['POST'])
|
| 91 |
def login():
|
| 92 |
# Get data from voice bot (name, email, phone number)
|
| 93 |
data = request.json # Assuming voice bot sends JSON data
|
|
|
|
| 94 |
email = data.get('email')
|
| 95 |
phone_number = data.get('phone_number')
|
| 96 |
|
| 97 |
+
if not email or not phone_number:
|
| 98 |
return jsonify({'error': 'Missing required fields'}), 400
|
| 99 |
|
| 100 |
try:
|
|
|
|
| 103 |
result = sf.query(query)
|
| 104 |
|
| 105 |
if result['totalSize'] > 0:
|
| 106 |
+
# User exists and credentials match, proceed to the menu
|
| 107 |
+
user_name = result['records'][0]['Name']
|
| 108 |
+
return jsonify({'success': True, 'name': user_name, 'email': email}) # Returning name for greeting
|
| 109 |
else:
|
| 110 |
# User not found or credentials don't match
|
| 111 |
return jsonify({'error': 'Invalid email or phone number'}), 401 # Unauthorized
|
|
|
|
| 150 |
return jsonify({'error': str(e)}), 500
|
| 151 |
|
| 152 |
|
| 153 |
+
# Menu endpoint: Fetch and display the menu
|
| 154 |
@app.route("/menu", methods=["GET"])
|
| 155 |
def menu_page():
|
| 156 |
try:
|
|
|
|
| 158 |
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
| 159 |
result = sf.query(query)
|
| 160 |
|
| 161 |
+
# Render the menu page template with menu items fetched from Salesforce
|
| 162 |
return render_template("menu_page.html", menu_items=result['records'])
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
return jsonify({"error": f"Failed to fetch menu items from Salesforce: {str(e)}"}), 500
|
| 166 |
|
| 167 |
|
| 168 |
+
# Order endpoint: Place the order and link it to the customer
|
| 169 |
@app.route("/order", methods=["POST"])
|
| 170 |
def place_order():
|
| 171 |
order_data = request.json
|
| 172 |
item_name = order_data.get('item')
|
| 173 |
quantity = order_data.get('quantity')
|
| 174 |
+
user_email = order_data.get('email')
|
| 175 |
|
| 176 |
+
if not item_name or not quantity or not user_email:
|
| 177 |
+
return jsonify({"error": "Item, quantity, and email are required."}), 400
|
| 178 |
|
| 179 |
try:
|
| 180 |
+
# Query Salesforce for the customer using their email
|
| 181 |
customer_login = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{user_email}' LIMIT 1")
|
| 182 |
+
if customer_login['totalSize'] == 0:
|
| 183 |
+
return jsonify({"error": "Customer not found"}), 404
|
| 184 |
+
|
| 185 |
customer_id = customer_login['records'][0]['Id']
|
| 186 |
|
| 187 |
+
# Assuming you have a method to calculate the total price based on item and quantity
|
| 188 |
+
total_price = calculate_total_price(item_name, quantity)
|
| 189 |
+
|
| 190 |
+
# Create the order in Salesforce
|
| 191 |
order = {
|
| 192 |
'Order_Item__c': item_name,
|
| 193 |
'Quantity__c': quantity,
|
| 194 |
+
'Customer__c': customer_id, # Linking the order to the customer
|
| 195 |
'Status__c': 'Pending',
|
| 196 |
+
'Total_Amount__c': total_price
|
| 197 |
}
|
| 198 |
|
| 199 |
order_response = sf.Order__c.create(order)
|
| 200 |
return jsonify({"success": True, "order_id": order_response['id']}), 200
|
| 201 |
+
|
| 202 |
except Exception as e:
|
| 203 |
return jsonify({"error": f"Error placing the order: {str(e)}"}), 500
|
| 204 |
|
| 205 |
+
# Helper function to calculate the total price (implement this function as needed)
|
|
|
|
| 206 |
def calculate_total_price(item_name, quantity):
|
| 207 |
+
try:
|
| 208 |
+
# Fetch item price from Salesforce and calculate total price
|
| 209 |
+
item = sf.query(f"SELECT Price__c FROM Menu_Item__c WHERE Name = '{item_name}' LIMIT 1")
|
| 210 |
+
if item['totalSize'] == 0:
|
| 211 |
+
raise ValueError("Item not found")
|
| 212 |
+
price = item['records'][0]['Price__c']
|
| 213 |
+
return price * quantity
|
| 214 |
+
except Exception as e:
|
| 215 |
+
raise ValueError(f"Error calculating total price: {str(e)}")
|
| 216 |
|
| 217 |
# Route for the order summary page
|
| 218 |
@app.route("/order-summary", methods=["GET"])
|