File size: 2,571 Bytes
ac4334c
9b35d16
71978cc
 
728ec2a
253a332
ac4334c
4be2e38
ac4334c
b658d64
c32c1af
4be2e38
f22c5db
84a5b18
71978cc
84a5b18
 
 
 
9b35d16
84a5b18
 
 
ac4334c
71978cc
 
f22c5db
84a5b18
a2f1a4d
4be2e38
 
 
 
ac4334c
 
 
 
f22c5db
 
 
a2f1a4d
f22c5db
 
84a5b18
ac4334c
f22c5db
 
 
 
ac4334c
f22c5db
 
ba57252
f22c5db
 
 
ac4334c
f22c5db
ac4334c
f22c5db
 
 
 
 
 
 
 
 
 
 
ac4334c
4be2e38
f22c5db
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
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')

# Salesforce Connection
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

# Initialize Salesforce connection
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)

# Sample endpoint for Salesforce queries (non-restaurant)
@app.route('/get_salesforce_data', methods=['GET'])
def get_salesforce_data():
    try:
        # Example SOQL query for retrieving data from a Salesforce object
        soql = "SELECT Id, Name FROM Account LIMIT 10"
        result = sf.query(soql)

        # Format the result as needed
        records = [{"id": record['Id'], "name": record['Name']} for record in result['records']]
        logging.debug(f"Fetched {len(records)} records from Salesforce.")
        return jsonify({"data": records})
    except Exception as e:
        logging.error(f"Error while fetching data from Salesforce: {str(e)}")
        return jsonify({"error": f"Failed to fetch data: {str(e)}"}), 500

# Example of a POST endpoint for submitting data (non-restaurant)
@app.route('/submit_salesforce_data', methods=['POST'])
def submit_salesforce_data():
    data = request.json
    record_data = data.get('record', {})
    
    if not record_data:
        return jsonify({'error': 'No data provided'}), 400

    try:
        # Example of creating a new Salesforce record
        account = sf.Account.create(record_data)
        logging.debug(f"Created new Salesforce account with Id: {account['id']}")
        return jsonify({'success': True, 'account_id': account['id']})
    except Exception as e:
        logging.error(f"Error while submitting data to Salesforce: {str(e)}")
        return jsonify({"error": f"Failed to submit data: {str(e)}"}), 500

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