File size: 4,098 Bytes
ac4334c
9b35d16
71978cc
 
728ec2a
253a332
ac4334c
4be2e38
ac4334c
b658d64
c32c1af
4be2e38
84a5b18
71978cc
84a5b18
 
 
 
9b35d16
84a5b18
 
 
ac4334c
71978cc
 
84a5b18
a2f1a4d
4be2e38
 
 
 
ac4334c
 
 
 
a2f1a4d
 
ac4334c
 
b658d64
b9667b8
1d1bd66
b9667b8
 
1d1bd66
 
 
 
ac4334c
 
a2f1a4d
 
b9667b8
84a5b18
5119cc5
b9667b8
5119cc5
 
5f420b7
a2f1a4d
84a5b18
ac4334c
84a5b18
a634135
b81fa5c
 
 
 
 
 
 
91e44c8
b81fa5c
91e44c8
b81fa5c
91e44c8
b81fa5c
 
 
 
 
 
 
91e44c8
 
 
 
 
 
124b94c
b81fa5c
5f420b7
b81fa5c
 
 
 
728ec2a
ac4334c
 
 
 
 
 
 
 
 
 
 
4be2e38
91e44c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from flask import Flask, render_template, send_from_directory, request, jsonify
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
import logging

logging.basicConfig(level=logging.DEBUG)

load_dotenv()

app = Flask(__name__, template_folder='templates', static_folder='static')

def get_salesforce_connection():
    try:
        sf = Salesforce(
            username=os.getenv('SFDC_USERNAME'),
            password=os.getenv('SFDC_PASSWORD'),
            security_token=os.getenv('SFDC_SECURITY_TOKEN'),
            domain=os.getenv('SFDC_DOMAIN', 'login')
        )
        return sf
    except Exception as e:
        print(f"Error connecting to Salesforce: {e}")
        return None

sf = get_salesforce_connection()

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/static/<path:filename>')
def serve_static(filename):
    return send_from_directory('static', filename)

@app.route('/get_ingredients', methods=['POST'])
def get_ingredients():
    dietary_preference = request.json.get('dietary_preference', '').strip().lower()
    logging.debug(f"Received dietary preference: {dietary_preference}")

    # Map dietary preference to SOQL condition for Menu_Item__c
    preference_map = {
        'vegetarian': "Category__c = 'Veg'",
        'non-vegetarian': "Category__c IN ('Chicken', 'Mutton', 'Fish')"
    }
    condition = preference_map.get(dietary_preference)

    if not condition:
        logging.debug("Invalid dietary preference received.")
        return jsonify({"error": "Invalid dietary preference."}), 400

    try:
        soql = f"SELECT Name, Image1__c FROM Menu_Item__c WHERE {condition} LIMIT 200"
        result = sf.query(soql)
        ingredients = [
            {"name": record['Name'], "image_url": record.get('Image1__c', '')}
            for record in result['records'] if 'Name' in record
        ]
        logging.debug(f"Fetched ingredients: {ingredients}")
        return jsonify({"ingredients": ingredients})
    except Exception as e:
        logging.error(f"Error while fetching ingredients: {str(e)}")
        return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500

@app.route('/get_menu_items', methods=['POST'])
def get_menu_items():
    category = request.json.get('category', '').strip().lower()
    logging.debug(f"Received category: {category}")

    soql = ""
    if category == 'chicken':
        soql = "SELECT Name, Image1__c, Price__c, Ingredients__c FROM Menu_Item__c WHERE Category__c = 'Chicken' LIMIT 200"
    elif category == 'mutton':
        soql = "SELECT Name, Image1__c, Price__c, Ingredients__c FROM Menu_Item__c WHERE Category__c = 'Mutton' LIMIT 200"
    elif category == 'non-vegetarian':
        soql = "SELECT Name, Image1__c, Price__c, Ingredients__c FROM Menu_Item__c WHERE Category__c IN ('Chicken', 'Mutton', 'Fish') LIMIT 200"
    else:
        logging.debug("Invalid category received.")
        return jsonify({"error": "Invalid category."}), 400

    try:
        result = sf.query(soql)
        menu_items = [
            {
                "name": record['Name'],
                "image_url": record.get('Image1__c', ''),
                "price": record.get('Price__c', 0.0),
                "ingredients": record.get('Ingredients__c', '')
            }
            for record in result['records'] if 'Name' in record
        ]
        logging.debug(f"Fetched menu items: {menu_items}")
        return jsonify({"menu_items": menu_items})
    except Exception as e:
        logging.error(f"Error while fetching menu items: {str(e)}")
        return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500

@app.route('/submit_ingredients', methods=['POST'])
def submit_ingredients():
    data = request.json
    ingredients = data.get('ingredients', [])
    
    if not ingredients:
        return jsonify({'error': 'No ingredients selected'}), 400
    
    logging.debug(f"Ingredients submitted: {ingredients}")
    return jsonify({'success': True})

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=7860)