@@ -258,6 +258,9 @@ async def update_matcherino_teams(self, teams_data):
258258 # Process each team
259259 for team in teams_data :
260260 team_name = team ['name' ]
261+ team_id_from_api = team .get ('team_id' ) # Use API-provided team ID
262+
263+ logger .info (f"Processing team: { team_name } (ID from API: { team_id_from_api } )" )
261264
262265 # Insert or update team
263266 team_id = await conn .fetchval (
@@ -279,15 +282,13 @@ async def update_matcherino_teams(self, teams_data):
279282
280283 # Insert team members
281284 if team .get ('members' ):
285+ matched_count = 0
282286 for member_name in team ['members' ]:
283- # Try to find matching Discord user
284- discord_user_id = await conn .fetchval (
285- """
286- SELECT user_id FROM registrations
287- WHERE matcherino_username = $1
288- """ ,
289- member_name
290- )
287+ # Try to find matching Discord user with various matching strategies
288+ discord_user_id = await self ._find_discord_user_for_member (conn , member_name )
289+
290+ if discord_user_id :
291+ matched_count += 1
291292
292293 # Insert team member with Discord user ID if found
293294 await conn .execute (
@@ -298,12 +299,39 @@ async def update_matcherino_teams(self, teams_data):
298299 """ ,
299300 team_id , member_name , discord_user_id
300301 )
302+
303+ logger .info (f"Team { team_name } : matched { matched_count } /{ len (team ['members' ])} members to Discord users" )
301304
302305 logger .info (f"Successfully updated { len (teams_data )} teams in database" )
303306 except Exception as e :
304307 logger .error (f"Error updating Matcherino teams in database: { e } " )
305308 raise
306309
310+ async def _find_discord_user_for_member (self , conn , member_name ):
311+ """
312+ Helper method to find a Discord user ID for a Matcherino member name using
313+ exact matching only.
314+
315+ Args:
316+ conn: Database connection
317+ member_name: The member name from the Matcherino API
318+
319+ Returns:
320+ int: Discord user ID if found, None otherwise
321+ """
322+ # Only use exact match on matcherino_username
323+ discord_user_id = await conn .fetchval (
324+ "SELECT user_id FROM registrations WHERE matcherino_username = $1" ,
325+ member_name
326+ )
327+
328+ if discord_user_id :
329+ return discord_user_id
330+
331+ # No match found
332+ logger .info (f"No Discord user match found for Matcherino member: { member_name } " )
333+ return None
334+
307335 async def get_matcherino_teams (self , active_only = True ):
308336 """
309337 Get all teams from the database with their members.
@@ -394,7 +422,7 @@ async def get_user_team(self, user_id):
394422 # Get team for this user
395423 team = await conn .fetchrow (
396424 """
397- SELECT t.*, tm.member_name
425+ SELECT t.team_id, t.team_name, t.last_updated
398426 FROM matcherino_teams t
399427 JOIN team_members tm ON t.team_id = tm.team_id
400428 WHERE tm.discord_user_id = $1 AND t.is_active = TRUE
@@ -405,21 +433,27 @@ async def get_user_team(self, user_id):
405433 if not team :
406434 return None
407435
408- # Get all teammates
409- teammates = await conn .fetch (
436+ # Get all members of the team, including the user
437+ members = await conn .fetch (
410438 """
411439 SELECT tm.member_name, tm.discord_user_id, r.username AS discord_username
412440 FROM team_members tm
413441 LEFT JOIN registrations r ON tm.discord_user_id = r.user_id
414- WHERE tm.team_id = $1 AND tm.discord_user_id != $2
415- ORDER BY tm.member_name
442+ WHERE tm.team_id = $1
443+ ORDER BY
444+ CASE WHEN tm.discord_user_id = $2 THEN 0 ELSE 1 END,
445+ tm.member_name
416446 """ ,
417447 team ['team_id' ], user_id
418448 )
419449
420450 # Convert to dictionary
421- result = dict (team )
422- result ['teammates' ] = [dict (teammate ) for teammate in teammates ]
451+ result = {
452+ 'team_id' : team ['team_id' ],
453+ 'team_name' : team ['team_name' ],
454+ 'last_updated' : team ['last_updated' ],
455+ 'members' : [dict (member ) for member in members ]
456+ }
423457
424458 return result
425459 except Exception as e :
0 commit comments