Okidi Norbert commited on
Commit
9ab3de6
·
1 Parent(s): 6093754

fix: ensure organization dependencies always fetch latest org_id from DB

Browse files
Files changed (1) hide show
  1. app/dependencies.py +36 -4
app/dependencies.py CHANGED
@@ -94,9 +94,41 @@ async def get_current_user(
94
  "organization_id": payload.get("organization_id")
95
  }
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  async def require_team_account(
99
- current_user: dict = Depends(get_current_user),
100
  ) -> dict:
101
  """
102
  Dependency that requires a TEAM or COACH account type.
@@ -112,7 +144,7 @@ async def require_team_account(
112
 
113
 
114
  async def require_organization_admin(
115
- current_user: dict = Depends(get_current_user),
116
  ) -> dict:
117
  """
118
  Dependency that requires a TEAM account type (Organization Owner).
@@ -126,7 +158,7 @@ async def require_organization_admin(
126
  return current_user
127
 
128
  async def require_personal_account(
129
- current_user: dict = Depends(get_current_user),
130
  ) -> dict:
131
  """
132
  Dependency that requires a PERSONAL account type.
@@ -141,7 +173,7 @@ async def require_personal_account(
141
 
142
 
143
  async def require_linked_account(
144
- current_user: dict = Depends(get_current_user),
145
  ) -> dict:
146
  """
147
  Dependency that requires the user to be linked to an organization.
 
94
  "organization_id": payload.get("organization_id")
95
  }
96
 
97
+ async def get_current_user_with_db(
98
+ current_user: dict = Depends(get_current_user),
99
+ supabase: SupabaseService = Depends(get_supabase)
100
+ ) -> dict:
101
+ """
102
+ Enhanced version of get_current_user that fetches organization_id from DB
103
+ if it's missing in the JWT payload (e.g. newly linked account).
104
+ """
105
+ if current_user.get("organization_id"):
106
+ return current_user
107
+
108
+ # If missing, check database
109
+ user_id = current_user["id"]
110
+ account_type = current_user["account_type"]
111
+
112
+ if account_type == AccountType.TEAM.value:
113
+ orgs = await supabase.select("organizations", filters={"owner_id": user_id})
114
+ if orgs:
115
+ current_user["organization_id"] = orgs[0]["id"]
116
+ elif account_type == AccountType.COACH.value:
117
+ # Coaches are in organizations_staff (assuming that table exists, if not, check users table)
118
+ # Actually, let's check users table first as it has organization_id column
119
+ user_record = await supabase.select_one("users", str(user_id))
120
+ if user_record and user_record.get("organization_id"):
121
+ current_user["organization_id"] = user_record["organization_id"]
122
+ elif account_type == AccountType.PLAYER.value:
123
+ user_record = await supabase.select_one("users", str(user_id))
124
+ if user_record and user_record.get("organization_id"):
125
+ current_user["organization_id"] = user_record["organization_id"]
126
+
127
+ return current_user
128
+
129
 
130
  async def require_team_account(
131
+ current_user: dict = Depends(get_current_user_with_db),
132
  ) -> dict:
133
  """
134
  Dependency that requires a TEAM or COACH account type.
 
144
 
145
 
146
  async def require_organization_admin(
147
+ current_user: dict = Depends(get_current_user_with_db),
148
  ) -> dict:
149
  """
150
  Dependency that requires a TEAM account type (Organization Owner).
 
158
  return current_user
159
 
160
  async def require_personal_account(
161
+ current_user: dict = Depends(get_current_user_with_db),
162
  ) -> dict:
163
  """
164
  Dependency that requires a PERSONAL account type.
 
173
 
174
 
175
  async def require_linked_account(
176
+ current_user: dict = Depends(get_current_user_with_db),
177
  ) -> dict:
178
  """
179
  Dependency that requires the user to be linked to an organization.