mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-03-21 18:04:39 -05:00
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import type { LoaderFunctionArgs } from "react-router";
|
|
import { cors } from "remix-utils/cors";
|
|
import { z } from "zod/v4";
|
|
import type { Bracket } from "~/features/tournament-bracket/core/Bracket";
|
|
import { tournamentFromDB } from "~/features/tournament-bracket/core/Tournament.server";
|
|
import { notFoundIfFalsy, parseParams } from "~/utils/remix.server";
|
|
import { id } from "~/utils/zod";
|
|
import {
|
|
handleOptionsRequest,
|
|
requireBearerAuth,
|
|
} from "../api-public-utils.server";
|
|
import type { GetTournamentBracketResponse } from "../schema";
|
|
|
|
const paramsSchema = z.object({
|
|
id,
|
|
bidx: z.coerce.number().int(),
|
|
});
|
|
|
|
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
|
await handleOptionsRequest(request);
|
|
requireBearerAuth(request);
|
|
|
|
const { id, bidx } = parseParams({ params, schema: paramsSchema });
|
|
|
|
const tournament = await tournamentFromDB({
|
|
user: undefined,
|
|
tournamentId: id,
|
|
});
|
|
|
|
const bracket = notFoundIfFalsy(tournament.bracketByIdx(bidx));
|
|
|
|
const result: GetTournamentBracketResponse = {
|
|
data: bracket.data,
|
|
teams: teams(bracket),
|
|
meta: {
|
|
teamsPerGroup:
|
|
bracket.type === "round_robin"
|
|
? (bracket.settings?.teamsPerGroup ??
|
|
tournament.ctx.settings.teamsPerGroup)
|
|
: undefined,
|
|
groupCount:
|
|
bracket.type === "swiss"
|
|
? (bracket.settings?.groupCount ??
|
|
tournament.ctx.settings.swiss?.groupCount)
|
|
: undefined,
|
|
roundCount:
|
|
bracket.type === "swiss"
|
|
? (bracket.settings?.roundCount ??
|
|
tournament.ctx.settings.swiss?.roundCount)
|
|
: undefined,
|
|
},
|
|
};
|
|
|
|
return await cors(request, Response.json(result));
|
|
};
|
|
|
|
function teams(bracket: Bracket) {
|
|
const checkedIn = bracket.seeding ?? bracket.participantTournamentTeamIds;
|
|
const pending = bracket.teamsPendingCheckIn ?? [];
|
|
|
|
return checkedIn
|
|
.map((teamId) => ({
|
|
id: teamId,
|
|
checkedIn: true,
|
|
}))
|
|
.concat(
|
|
pending.map((teamId) => ({
|
|
id: teamId,
|
|
checkedIn: false,
|
|
})),
|
|
);
|
|
}
|