SmartInc-API / resolver.py
yoursdvniel's picture
Update resolver.py
7c7295a verified
# resolver.py
from firestore_client import get_firestore_client
from google.cloud import firestore
db = get_firestore_client()
def resolve_user_context(uid: str, company_code: str):
"""
Returns: {"uid", "email", "participantId"} — participantId may be None if not found.
"""
email = None
participant_id = None
# 1) users/{uid} -> email
user_snap = db.collection("users").document(uid).get()
if user_snap.exists:
user = user_snap.to_dict() or {}
email = user.get("email")
# 2) applications where companyCode == ctx AND email == user.email -> participantId
if email:
app_query = (
db.collection("applications")
.where(filter=firestore.FieldFilter("companyCode", "==", company_code))
.where(filter=firestore.FieldFilter("email", "==", email))
.limit(1)
)
app_docs = list(app_query.stream())
if app_docs:
participant_id = (app_docs[0].to_dict() or {}).get("participantId")
# 3) Verify the participant doc exists, but do NOT filter by companyCode on participants.
if participant_id:
p_snap = db.collection("participants").document(participant_id).get()
if not p_snap.exists:
participant_id = None
return {"uid": uid, "email": email, "participantId": participant_id}