sendou.ink/app/features/tournament-bracket/TournamentMatchRepository.server.ts
cesaregarza 2ab24f64fa
Feature/add tournament active players endpoint (#2497)
Co-authored-by: Kalle <38327916+Sendouc@users.noreply.github.com>
2025-08-28 18:53:33 +03:00

41 lines
1.2 KiB
TypeScript

import { db } from "~/db/sql";
export function findResultById(id: number) {
return db
.selectFrom("TournamentMatchGameResult")
.select([
"TournamentMatchGameResult.id",
"TournamentMatchGameResult.opponentOnePoints",
"TournamentMatchGameResult.opponentTwoPoints",
])
.where("TournamentMatchGameResult.id", "=", id)
.executeTakeFirst();
}
export async function userParticipationByTournamentId(tournamentId: number) {
return db
.with("playerMatches", (db) =>
db
.selectFrom("TournamentMatchGameResultParticipant as Participant")
.innerJoin(
"TournamentMatchGameResult as GameResult",
"GameResult.id",
"Participant.matchGameResultId",
)
.innerJoin("TournamentMatch as Match", "Match.id", "GameResult.matchId")
.innerJoin("TournamentStage as Stage", "Stage.id", "Match.stageId")
.select(["Participant.userId", "GameResult.matchId"])
.where("Stage.tournamentId", "=", tournamentId)
.distinct(),
)
.selectFrom("playerMatches")
.select(({ fn, ref }) => [
"playerMatches.userId",
fn
.agg<number[]>("json_group_array", [ref("playerMatches.matchId")])
.as("matchIds"),
])
.groupBy("playerMatches.userId")
.execute();
}