match h2h

This commit is contained in:
yohlo
2025-10-11 13:40:12 -05:00
parent 14c2eb2c02
commit 43972b6a06
6 changed files with 383 additions and 23 deletions

View File

@@ -71,5 +71,58 @@ export function createMatchesService(pb: PocketBase) {
matches.map((match) => pb.collection("matches").delete(match.id))
);
},
async getMatchesBetweenPlayers(player1Id: string, player2Id: string): Promise<Match[]> {
logger.info("PocketBase | Getting matches between players", { player1Id, player2Id });
const player1Teams = await pb.collection("teams").getFullList({
filter: `players ~ "${player1Id}"`,
fields: "id",
});
const player2Teams = await pb.collection("teams").getFullList({
filter: `players ~ "${player2Id}"`,
fields: "id",
});
const player1TeamIds = player1Teams.map(t => t.id);
const player2TeamIds = player2Teams.map(t => t.id);
if (player1TeamIds.length === 0 || player2TeamIds.length === 0) {
return [];
}
const filterConditions: string[] = [];
player1TeamIds.forEach(team1Id => {
player2TeamIds.forEach(team2Id => {
filterConditions.push(`(home="${team1Id}" && away="${team2Id}")`);
filterConditions.push(`(home="${team2Id}" && away="${team1Id}")`);
});
});
const filter = filterConditions.join(" || ");
const results = await pb.collection("matches").getFullList({
filter,
expand: "tournament, home, away, home.players, away.players",
sort: "-created",
});
return results.map(match => transformMatch(match));
},
async getMatchesBetweenTeams(team1Id: string, team2Id: string): Promise<Match[]> {
logger.info("PocketBase | Getting matches between teams", { team1Id, team2Id });
const filter = `(home="${team1Id}" && away="${team2Id}") || (home="${team2Id}" && away="${team1Id}")`;
const results = await pb.collection("matches").getFullList({
filter,
expand: "tournament, home, away, home.players, away.players",
sort: "-created",
});
return results.map(match => transformMatch(match));
},
};
}