sendou.ink/app/features/tournament-subs/loaders/to.$id.subs.server.ts

31 lines
1001 B
TypeScript

import { type LoaderFunctionArgs, redirect } from "@remix-run/node";
import { getUser } from "~/features/auth/core/user.server";
import { tournamentFromDB } from "~/features/tournament-bracket/core/Tournament.server";
import { parseParams } from "~/utils/remix.server";
import { tournamentRegisterPage } from "~/utils/urls";
import { idObject } from "~/utils/zod";
import * as TournamentSubRepository from "../TournamentSubRepository.server";
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
const user = await getUser(request);
const { id: tournamentId } = parseParams({
params,
schema: idObject,
});
const tournament = await tournamentFromDB({ tournamentId, user });
if (!tournament.subsFeatureEnabled) {
throw redirect(tournamentRegisterPage(tournamentId));
}
const subs = await TournamentSubRepository.findSubsVisibleToUser({
tournamentId,
userId: user?.id,
});
return {
subs,
hasOwnSubPost: subs.some((sub) => sub.userId === user?.id),
};
};