work on team enrollment

This commit is contained in:
yohlo
2025-09-16 09:24:21 -05:00
parent 9a105b30c6
commit cde74a04d5
45 changed files with 1244 additions and 457 deletions

View File

@@ -65,10 +65,10 @@ export function createPlayersService(pb: PocketBase) {
return result.map(transformPlayer);
},
async getPlayerStats(playerId: string): Promise<PlayerStats[]> {
const result = await pb.collection("player_stats").getFullList<PlayerStats>({
filter: `player_id = "${playerId}"`,
});
async getPlayerStats(playerId: string): Promise<PlayerStats> {
const result = await pb.collection("player_stats").getFirstListItem<PlayerStats>(
`player_id = "${playerId}"`
);
return result;
},
@@ -102,5 +102,56 @@ export function createPlayersService(pb: PocketBase) {
return result.map(transformMatch);
},
async getUnenrolledPlayers(tournamentId: string): Promise<Player[]> {
try {
// Get the tournament with its enrolled teams
const tournament = await pb.collection("tournaments").getOne(tournamentId, {
fields: "teams",
expand: "teams,teams.players"
});
// Extract player IDs from all enrolled teams
const enrolledPlayerIds: string[] = [];
if (tournament.expand?.teams) {
const teams = Array.isArray(tournament.expand.teams) ? tournament.expand.teams : [tournament.expand.teams];
teams.forEach((team: any) => {
if (team.expand?.players) {
const players = Array.isArray(team.expand.players) ? team.expand.players : [team.expand.players];
players.forEach((player: any) => {
enrolledPlayerIds.push(player.id);
});
}
});
}
// If no players are enrolled, return all players
if (enrolledPlayerIds.length === 0) {
const allPlayers = await pb.collection("players").getFullList<Player>({
fields: "id,first_name,last_name,email",
});
return allPlayers.map(transformPlayer);
}
// Build filter to exclude enrolled players
const filter = enrolledPlayerIds
.map((playerId: string) => `id != "${playerId}"`)
.join(" && ");
const availablePlayers = await pb.collection("players").getFullList<Player>({
filter,
fields: "id,first_name,last_name,email",
});
return availablePlayers.map(transformPlayer);
} catch (error) {
console.error("Error getting unenrolled players:", error);
// Fallback to all players if there's an error
const allPlayers = await pb.collection("players").getFullList<Player>({
fields: "id,first_name,last_name,email",
});
return allPlayers.map(transformPlayer);
}
},
};
}