File size: 1,364 Bytes
7c7295a
600fb13
7c7295a
 
600fb13
 
 
 
 
 
 
 
 
 
7c7295a
 
 
600fb13
 
7c7295a
600fb13
7c7295a
 
 
 
 
 
 
 
 
600fb13
7c7295a
 
 
 
 
600fb13
 
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
# 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}