-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
143 lines (107 loc) · 4.59 KB
/
main.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from flask import Flask, request, jsonify
import logging
import ai_service
import lambda_function
import salesforce_service
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
app = Flask(__name__)
@app.route('/suggestion', methods=['POST'])
def suggestion():
request_data = request.get_json()
if not request_data:
return jsonify({
'error': 'Missing JSON body'
}), 400
user_ids = request_data.get('user_ids')
account_id = request_data.get('account_id')
product_ids = request_data.get('product_ids')
transcript = request_data.get('transcript')
salesforce_access_token = request_data.get('salesforce_access_token')
# Get opportunity products
raw_opportunity_products = salesforce_service.get_opportunity_products(salesforce_access_token, user_ids, account_id, product_ids, format = True)
opportunity_products = []
for opportunity_product in raw_opportunity_products:
opportunity_products.append({
'id': opportunity_product.get('Id'),
'opportunity_id': opportunity_product.get('OpportunityId'),
'product_id': opportunity_product.get('Product2Id'),
'product_name': opportunity_product.get('Product2').get('Name'),
'quantity': opportunity_product.get('Quantity')
})
# Get opportunities assigned to users
raw_opportunities = salesforce_service.get_opportunities_assigned_to_users(salesforce_access_token, user_ids, account_id, format = True)
opportunities = []
# Create a map of opportunity products
opportunity_products_map = {}
for op in opportunity_products:
opp_id = op['opportunity_id']
if opp_id not in opportunity_products_map:
opportunity_products_map[opp_id] = []
opportunity_products_map[opp_id].append(op)
for opportunity in raw_opportunities:
opp_products = opportunity_products_map.get(opportunity.get('Id'), [])
opportunity_rank = ai_service.rank_opportunity_score(
opportunity,
opp_products,
transcript
)
opportunity_to_be_added = {
'id': opportunity.get('Id'),
'name': opportunity.get('Name'),
'stage_name': opportunity.get('StageName'),
'rank': opportunity_rank
}
opportunities.append(opportunity_to_be_added)
# Sort opportunities by rank in descending order
opportunities.sort(key=lambda x: x['rank'], reverse=True)
return jsonify({
'opportunities': opportunities
})
@app.route('/opportunities', methods=['POST'])
def get_opportunities_by_user_ids():
request_data = request.get_json()
if not request_data:
return jsonify({
'error': 'Missing JSON body'
}), 400
user_ids = request_data.get('user_ids')
account_id = request_data.get('account_id')
salesforce_access_token = request_data.get('salesforce_access_token')
opportunities = salesforce_service.get_opportunities_assigned_to_users(salesforce_access_token, user_ids, account_id)
opportunities = opportunities.get('records') if opportunities.get('records') else []
return jsonify({
'opportunities': opportunities
})
@app.route('/opportunity_products', methods=['POST'])
def get_opportunity_products():
request_data = request.get_json()
if not request_data:
return jsonify({
'error': 'Missing JSON body'
}), 400
user_ids = request_data.get('user_ids')
account_id = request_data.get('account_id')
product_ids = request_data.get('product_ids')
salesforce_access_token = request_data.get('salesforce_access_token')
opportunity_products = salesforce_service.get_opportunity_products(salesforce_access_token, user_ids, account_id, product_ids)
opportunity_products = opportunity_products.get('records') if opportunity_products.get('records') else []
return jsonify({
'opportunity_products': opportunity_products
})
@app.route('/opportunity_suggestion', methods=['POST'])
def opportunity_suggestion():
request_data = request.get_json()
if not request_data or not request_data.get('data'):
return jsonify({
'error': 'Missing JSON body'
}), 400
data = request_data.get('data')
config = request_data.get('config') or {}
response = lambda_function.lambda_handler({
'body': data,
'config': config
}, {})
return response
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)