Create utils/db.py
Browse files- utils/db.py +102 -0
utils/db.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# backend/utils/db.py
|
2 |
+
from simple_salesforce import Salesforce
|
3 |
+
import os
|
4 |
+
from datetime import datetime
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# Load environment variables from .env file
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Salesforce credentials
|
11 |
+
SF_USERNAME = os.getenv("SF_USERNAME")
|
12 |
+
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
13 |
+
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
14 |
+
SF_CLIENT_ID = os.getenv("SF_CLIENT_ID")
|
15 |
+
SF_CLIENT_SECRET = os.getenv("SF_CLIENT_SECRET")
|
16 |
+
SF_DOMAIN = os.getenv("SF_DOMAIN", "login") # Default to login
|
17 |
+
|
18 |
+
# Validate credentials
|
19 |
+
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN, SF_CLIENT_ID, SF_CLIENT_SECRET]):
|
20 |
+
raise ValueError("Missing Salesforce credentials in environment variables")
|
21 |
+
|
22 |
+
# Initialize Salesforce client
|
23 |
+
sf = Salesforce(
|
24 |
+
username=SF_USERNAME,
|
25 |
+
password=SF_PASSWORD,
|
26 |
+
security_token=SF_SECURITY_TOKEN,
|
27 |
+
client_id=SF_CLIENT_ID,
|
28 |
+
client_secret=SF_CLIENT_SECRET,
|
29 |
+
domain=SF_DOMAIN
|
30 |
+
)
|
31 |
+
|
32 |
+
def update_leaderboard(player_name: str, drs_out: bool = False, runs: float = 0):
|
33 |
+
"""
|
34 |
+
Update the player's leaderboard stats in User_Profile__c.
|
35 |
+
"""
|
36 |
+
try:
|
37 |
+
query = f"SELECT Id, Name__c, Matches__c, LBW_Dismissals__c, Score__c FROM User_Profile__c WHERE Name__c = '{player_name}'"
|
38 |
+
result = sf.query(query)
|
39 |
+
|
40 |
+
if result["totalSize"] > 0:
|
41 |
+
player = result["records"][0]
|
42 |
+
player_id = player["Id"]
|
43 |
+
sf.User_Profile__c.update(player_id, {
|
44 |
+
"Matches__c": player["Matches__c"] + 1,
|
45 |
+
"LBW_Dismissals__c": player["LBW_Dismissals__c"] + 1 if drs_out else player["LBW_Dismissals__c"],
|
46 |
+
"Score__c": player["Score__c"] + runs
|
47 |
+
})
|
48 |
+
else:
|
49 |
+
sf.User_Profile__c.create({
|
50 |
+
"Name__c": player_name,
|
51 |
+
"Matches__c": 1,
|
52 |
+
"LBW_Dismissals__c": 1 if drs_out else 0,
|
53 |
+
"Score__c": runs
|
54 |
+
})
|
55 |
+
except Exception as e:
|
56 |
+
raise Exception(f"Failed to update leaderboard: {str(e)}")
|
57 |
+
|
58 |
+
def get_leaderboard():
|
59 |
+
"""
|
60 |
+
Retrieve the leaderboard sorted by score from User_Profile__c.
|
61 |
+
"""
|
62 |
+
try:
|
63 |
+
query = "SELECT Id, Name__c, Matches__c, LBW_Dismissals__c, Score__c FROM User_Profile__c ORDER BY Score__c DESC"
|
64 |
+
result = sf.query(query)
|
65 |
+
return result["records"]
|
66 |
+
except Exception as e:
|
67 |
+
raise Exception(f"Failed to retrieve leaderboard: {str(e)}")
|
68 |
+
|
69 |
+
def store_drs_result(video_url: str, verdict: str, speed: float, replay_link: str, player_name: str) -> str:
|
70 |
+
"""
|
71 |
+
Store DRS analysis result in Match__c and DRS_Result__c.
|
72 |
+
Returns the Match__c record ID.
|
73 |
+
"""
|
74 |
+
try:
|
75 |
+
# Find player (User_Profile__c)
|
76 |
+
query = f"SELECT Id FROM User_Profile__c WHERE Name__c = '{player_name}'"
|
77 |
+
result = sf.query(query)
|
78 |
+
if result["totalSize"] == 0:
|
79 |
+
raise Exception(f"Player {player_name} not found")
|
80 |
+
player_id = result["records"][0]["Id"]
|
81 |
+
|
82 |
+
# Create Match__c record
|
83 |
+
match = sf.Match__c.create({
|
84 |
+
"Video_URL__c": video_url,
|
85 |
+
"Match_Date__c": datetime.now().isoformat(),
|
86 |
+
"Player__c": player_id,
|
87 |
+
"Match_Type__c": "Friendly",
|
88 |
+
"Result__c": verdict
|
89 |
+
})
|
90 |
+
match_id = match["id"]
|
91 |
+
|
92 |
+
# Create DRS_Result__c record
|
93 |
+
sf.DRS_Result__c.create({
|
94 |
+
"Verdict__c": verdict,
|
95 |
+
"Speed__c": speed,
|
96 |
+
"Replay_Link__c": replay_link,
|
97 |
+
"Match__c": match_id
|
98 |
+
})
|
99 |
+
|
100 |
+
return match_id
|
101 |
+
except Exception as e:
|
102 |
+
raise Exception(f"Failed to store DRS result: {str(e)}")
|