sendou.ink/app/features/api-public/routes/tournament.$id.casted.ts
2025-12-29 19:21:11 +02:00

52 lines
1.3 KiB
TypeScript

import type { LoaderFunctionArgs } from "react-router";
import { cors } from "remix-utils/cors";
import { z } from "zod/v4";
import { db } from "~/db/sql";
import { notFoundIfFalsy, parseParams } from "~/utils/remix.server";
import { id } from "~/utils/zod";
import {
handleOptionsRequest,
requireBearerAuth,
} from "../api-public-utils.server";
import type { GetCastedTournamentMatchesResponse } from "../schema";
const paramsSchema = z.object({
id,
});
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
await handleOptionsRequest(request);
requireBearerAuth(request);
const { id } = parseParams({
params,
schema: paramsSchema,
});
const tournament = notFoundIfFalsy(
await db
.selectFrom("Tournament")
.select(["Tournament.castedMatchesInfo"])
.where("Tournament.id", "=", id)
.executeTakeFirst(),
);
const result: GetCastedTournamentMatchesResponse = {
current:
tournament.castedMatchesInfo?.castedMatches.map((match) => ({
matchId: match.matchId,
channel: {
type: "TWITCH",
channelId: match.twitchAccount,
},
})) ?? [],
future:
tournament.castedMatchesInfo?.lockedMatches.map((matchId) => ({
matchId: matchId,
channel: null,
})) ?? [],
};
return await cors(request, Response.json(result));
};