sendou.ink/app/features/tournament-subs/loaders/to.$id.subs.server.ts
2025-10-27 21:36:12 +02:00

52 lines
1.4 KiB
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 { assertUnreachable } from "~/utils/types";
import { tournamentRegisterPage } from "~/utils/urls";
import { idObject } from "~/utils/zod";
import { findSubsByTournamentId } from "../queries/findSubsByTournamentId.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 = findSubsByTournamentId({
tournamentId,
userId: user?.id,
// biome-ignore lint/suspicious/useIterableCallbackReturn: Biome 2.3.1 upgrade
}).filter((sub) => {
if (sub.visibility === "ALL") return true;
const userPlusTier = user?.plusTier ?? 4;
switch (sub.visibility) {
case "+1": {
return userPlusTier === 1;
}
case "+2": {
return userPlusTier <= 2;
}
case "+3": {
return userPlusTier <= 3;
}
default: {
assertUnreachable(sub.visibility);
}
}
});
return {
subs,
hasOwnSubPost: subs.some((sub) => sub.userId === user?.id),
};
};