Skip to content

Commit f3e6609

Browse files
authored
Various tournament queries migrated to Kysely (#2978)
1 parent ef4fb64 commit f3e6609

27 files changed

+325
-515
lines changed

app/features/tournament-bracket/TournamentMatchRepository.server.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,78 @@
1+
import { sql } from "kysely";
2+
import { jsonArrayFrom } from "kysely/helpers/sqlite";
13
import { db } from "~/db/sql";
4+
import type { Unwrapped } from "~/utils/types";
5+
6+
export type FindMatchById = NonNullable<Unwrapped<typeof findMatchById>>;
7+
export async function findMatchById(id: number) {
8+
const row = await db
9+
.selectFrom("TournamentMatch")
10+
.innerJoin(
11+
"TournamentStage",
12+
"TournamentStage.id",
13+
"TournamentMatch.stageId",
14+
)
15+
.innerJoin(
16+
"TournamentRound",
17+
"TournamentRound.id",
18+
"TournamentMatch.roundId",
19+
)
20+
.innerJoin("Tournament", "Tournament.id", "TournamentStage.tournamentId")
21+
.select(({ eb }) => [
22+
"TournamentMatch.id",
23+
"TournamentMatch.groupId",
24+
"TournamentMatch.opponentOne",
25+
"TournamentMatch.opponentTwo",
26+
"TournamentMatch.chatCode",
27+
"TournamentMatch.startedAt",
28+
"TournamentMatch.status",
29+
"Tournament.mapPickingStyle",
30+
"TournamentRound.id as roundId",
31+
"TournamentRound.maps as roundMaps",
32+
jsonArrayFrom(
33+
eb
34+
.selectFrom("TournamentTeamMember")
35+
.innerJoin("User", "User.id", "TournamentTeamMember.userId")
36+
.select([
37+
"User.id",
38+
"User.username",
39+
"TournamentTeamMember.tournamentTeamId",
40+
sql<
41+
string | null
42+
>`coalesce("TournamentTeamMember"."inGameName", "User"."inGameName")`.as(
43+
"inGameName",
44+
),
45+
"User.discordId",
46+
"User.customUrl",
47+
"User.discordAvatar",
48+
"User.pronouns",
49+
])
50+
.where(({ or, eb: innerEb }) =>
51+
or([
52+
innerEb(
53+
"TournamentTeamMember.tournamentTeamId",
54+
"=",
55+
sql<number>`"TournamentMatch"."opponentOne" ->> '$.id'`,
56+
),
57+
innerEb(
58+
"TournamentTeamMember.tournamentTeamId",
59+
"=",
60+
sql<number>`"TournamentMatch"."opponentTwo" ->> '$.id'`,
61+
),
62+
]),
63+
),
64+
).as("players"),
65+
])
66+
.where("TournamentMatch.id", "=", id)
67+
.executeTakeFirst();
68+
69+
if (!row) return;
70+
71+
return {
72+
...row,
73+
bestOf: row.roundMaps.count,
74+
};
75+
}
276

377
export function findResultById(id: number) {
478
return db

app/features/tournament-bracket/actions/to.$id.brackets.server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,8 @@ export const action: ActionFunction = async ({ params, request }) => {
288288
`Checking in (bracket try): tournament team id: ${teamMemberOf.id} - user id: ${user.id} - tournament id: ${tournament.ctx.id} - bracket idx: ${data.bracketIdx}`,
289289
);
290290

291-
await TournamentTeamRepository.checkIn({
291+
await TournamentTeamRepository.checkIn(teamMemberOf.id, {
292292
bracketIdx: data.bracketIdx,
293-
tournamentTeamId: teamMemberOf.id,
294293
});
295294

296295
logger.info(

app/features/tournament-bracket/actions/to.$id.matches.$mid.server.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ import { deleteMatchPickBanEvents } from "../queries/deleteMatchPickBanEvents.se
2929
import { deleteParticipantsByMatchGameResultId } from "../queries/deleteParticipantsByMatchGameResultId.server";
3030
import { deletePickBanEvent } from "../queries/deletePickBanEvent.server";
3131
import { deleteTournamentMatchGameResultById } from "../queries/deleteTournamentMatchGameResultById.server";
32-
import {
33-
type FindMatchById,
34-
findMatchById,
35-
} from "../queries/findMatchById.server";
3632
import { findResultsByMatchId } from "../queries/findResultsByMatchId.server";
3733
import { insertTournamentMatchGameResult } from "../queries/insertTournamentMatchGameResult.server";
3834
import { insertTournamentMatchGameResultParticipant } from "../queries/insertTournamentMatchGameResultParticipant.server";
3935
import { updateMatchGameResultPoints } from "../queries/updateMatchGameResultPoints.server";
36+
import type { FindMatchById } from "../TournamentMatchRepository.server";
4037
import {
4138
matchPageParamsSchema,
4239
matchSchema,
@@ -56,7 +53,9 @@ export const action: ActionFunction = async ({ params, request }) => {
5653
params,
5754
schema: matchPageParamsSchema,
5855
});
59-
const match = notFoundIfFalsy(findMatchById(matchId));
56+
const match = notFoundIfFalsy(
57+
await TournamentMatchRepository.findMatchById(matchId),
58+
);
6059
const data = await parseRequestPayload({
6160
request,
6261
schema: matchSchema,

app/features/tournament-bracket/loaders/to.$id.matches.$mid.server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { executeRoll } from "../core/executeRoll.server";
1515
import { mapListFromResults, resolveMapList } from "../core/mapList.server";
1616
import * as PickBan from "../core/PickBan";
1717
import { tournamentFromDBCached } from "../core/Tournament.server";
18-
import { findMatchById } from "../queries/findMatchById.server";
1918
import { findResultsByMatchId } from "../queries/findResultsByMatchId.server";
19+
import * as TournamentMatchRepository from "../TournamentMatchRepository.server";
2020
import { matchPageParamsSchema } from "../tournament-bracket-schemas.server";
2121
import { matchEndedEarly } from "../tournament-bracket-utils";
2222

@@ -33,7 +33,9 @@ export const loader = async ({ params }: LoaderFunctionArgs) => {
3333
user: undefined,
3434
});
3535

36-
const match = notFoundIfFalsy(findMatchById(matchId));
36+
const match = notFoundIfFalsy(
37+
await TournamentMatchRepository.findMatchById(matchId),
38+
);
3739

3840
const isBye = !match.opponentOne || !match.opponentTwo;
3941
if (isBye) {

app/features/tournament-bracket/queries/findMatchById.server.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)