yoursdvniel commited on
Commit
7c7295a
·
verified ·
1 Parent(s): a97fbfc

Update resolver.py

Browse files
Files changed (1) hide show
  1. resolver.py +21 -16
resolver.py CHANGED
@@ -1,5 +1,7 @@
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):
@@ -10,24 +12,27 @@ def resolve_user_context(uid: str, company_code: str):
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}
 
1
+ # resolver.py
2
  from firestore_client import get_firestore_client
3
+ from google.cloud import firestore
4
+
5
  db = get_firestore_client()
6
 
7
  def resolve_user_context(uid: str, company_code: str):
 
12
  participant_id = None
13
 
14
  # 1) users/{uid} -> email
15
+ user_snap = db.collection("users").document(uid).get()
16
+ if user_snap.exists:
17
+ user = user_snap.to_dict() or {}
18
  email = user.get("email")
19
 
20
+ # 2) applications where companyCode == ctx AND email == user.email -> participantId
21
  if email:
22
+ app_query = (
23
+ db.collection("applications")
24
+ .where(filter=firestore.FieldFilter("companyCode", "==", company_code))
25
+ .where(filter=firestore.FieldFilter("email", "==", email))
26
+ .limit(1)
27
+ )
28
+ app_docs = list(app_query.stream())
29
+ if app_docs:
30
+ participant_id = (app_docs[0].to_dict() or {}).get("participantId")
31
 
32
+ # 3) Verify the participant doc exists, but do NOT filter by companyCode on participants.
33
+ if participant_id:
34
+ p_snap = db.collection("participants").document(participant_id).get()
35
+ if not p_snap.exists:
36
+ participant_id = None
37
 
38
  return {"uid": uid, "email": email, "participantId": participant_id}