mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-03-21 18:04:39 -05:00
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import type { ActionFunction } from "react-router";
|
|
import { requireUser } from "~/features/auth/core/user.server";
|
|
import {
|
|
clearTournamentDataCache,
|
|
tournamentFromDB,
|
|
} from "~/features/tournament-bracket/core/Tournament.server";
|
|
import {
|
|
errorToastIfFalsy,
|
|
parseParams,
|
|
parseRequestPayload,
|
|
successToast,
|
|
} from "~/utils/remix.server";
|
|
import { idObject } from "~/utils/zod";
|
|
import { updateTeamSeeds } from "../queries/updateTeamSeeds.server";
|
|
import * as TournamentTeamRepository from "../TournamentTeamRepository.server";
|
|
import { seedsActionSchema } from "../tournament-schemas.server";
|
|
|
|
export const action: ActionFunction = async ({ request, params }) => {
|
|
const data = await parseRequestPayload({
|
|
request,
|
|
schema: seedsActionSchema,
|
|
});
|
|
const user = requireUser();
|
|
const { id: tournamentId } = parseParams({
|
|
params,
|
|
schema: idObject,
|
|
});
|
|
const tournament = await tournamentFromDB({ tournamentId, user });
|
|
|
|
errorToastIfFalsy(tournament.isOrganizer(user), "Not an organizer");
|
|
errorToastIfFalsy(!tournament.hasStarted, "Tournament has started");
|
|
|
|
switch (data._action) {
|
|
case "UPDATE_SEEDS": {
|
|
updateTeamSeeds({ tournamentId, teamIds: data.seeds });
|
|
clearTournamentDataCache(tournamentId);
|
|
return successToast("Seeds saved successfully");
|
|
}
|
|
case "UPDATE_STARTING_BRACKETS": {
|
|
const validBracketIdxs =
|
|
tournament.ctx.settings.bracketProgression.flatMap(
|
|
(bracket, bracketIdx) => (!bracket.sources ? [bracketIdx] : []),
|
|
);
|
|
|
|
errorToastIfFalsy(
|
|
data.startingBrackets.every((t) =>
|
|
validBracketIdxs.includes(t.startingBracketIdx),
|
|
),
|
|
"Invalid starting bracket idx",
|
|
);
|
|
|
|
await TournamentTeamRepository.updateStartingBrackets(
|
|
data.startingBrackets,
|
|
);
|
|
break;
|
|
}
|
|
}
|
|
|
|
clearTournamentDataCache(tournamentId);
|
|
|
|
return null;
|
|
};
|