Spaces:
Sleeping
Sleeping
Create resolver.py
Browse files- resolver.py +33 -0
resolver.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from firestore_client import get_firestore_client
|
| 3 |
+
db = get_firestore_client()
|
| 4 |
+
|
| 5 |
+
def resolve_user_context(uid: str, company_code: str):
|
| 6 |
+
"""
|
| 7 |
+
Returns: {"uid", "email", "participantId"} — participantId may be None if not found.
|
| 8 |
+
"""
|
| 9 |
+
email = None
|
| 10 |
+
participant_id = None
|
| 11 |
+
|
| 12 |
+
# 1) users/{uid} -> email
|
| 13 |
+
user_doc = db.collection("users").document(uid).get()
|
| 14 |
+
if user_doc.exists:
|
| 15 |
+
user = user_doc.to_dict()
|
| 16 |
+
email = user.get("email")
|
| 17 |
+
|
| 18 |
+
# 2) participants where email == user.email and companyCode == ctx
|
| 19 |
+
if email:
|
| 20 |
+
q = (db.collection("participants")
|
| 21 |
+
.where("email", "==", email)
|
| 22 |
+
.where("hub", "==", company_code)) # if you store companyCode on participants, use "companyCode" here
|
| 23 |
+
try:
|
| 24 |
+
# Prefer companyCode; fall back to hub if that's what you use. Adjust the field to your actual schema.
|
| 25 |
+
q = db.collection("participants").where("email", "==", email).where("companyCode", "==", company_code)
|
| 26 |
+
except Exception:
|
| 27 |
+
pass
|
| 28 |
+
|
| 29 |
+
docs = list(q.limit(1).stream())
|
| 30 |
+
if docs:
|
| 31 |
+
participant_id = docs[0].id
|
| 32 |
+
|
| 33 |
+
return {"uid": uid, "email": email, "participantId": participant_id}
|