75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
|
|
import { eq, and } from "drizzle-orm";
|
||
|
|
import { database } from "~/database/context";
|
||
|
|
import * as schema from "~/database/schema";
|
||
|
|
import { getTournamentById } from "./tournament";
|
||
|
|
import { findSportsSeasonById } from "./sports-season";
|
||
|
|
|
||
|
|
export type SportsSeasonTournament = typeof schema.sportsSeasonTournaments.$inferSelect;
|
||
|
|
export type NewSportsSeasonTournament = typeof schema.sportsSeasonTournaments.$inferInsert;
|
||
|
|
|
||
|
|
export async function linkTournamentToSportsSeason(
|
||
|
|
sportsSeasonId: string,
|
||
|
|
tournamentId: string
|
||
|
|
): Promise<SportsSeasonTournament> {
|
||
|
|
const [sportsSeason, tournament] = await Promise.all([
|
||
|
|
findSportsSeasonById(sportsSeasonId),
|
||
|
|
getTournamentById(tournamentId),
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (!sportsSeason) {
|
||
|
|
throw new Error(`Sports season ${sportsSeasonId} not found`);
|
||
|
|
}
|
||
|
|
if (!tournament) {
|
||
|
|
throw new Error(`Tournament ${tournamentId} not found`);
|
||
|
|
}
|
||
|
|
if (sportsSeason.sportId !== tournament.sportId) {
|
||
|
|
throw new Error("Tournament and sports season must belong to the same sport");
|
||
|
|
}
|
||
|
|
|
||
|
|
const db = database();
|
||
|
|
const [link] = await db
|
||
|
|
.insert(schema.sportsSeasonTournaments)
|
||
|
|
.values({ sportsSeasonId, tournamentId })
|
||
|
|
.returning();
|
||
|
|
return link;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function unlinkTournamentFromSportsSeason(
|
||
|
|
sportsSeasonId: string,
|
||
|
|
tournamentId: string
|
||
|
|
): Promise<void> {
|
||
|
|
const db = database();
|
||
|
|
await db
|
||
|
|
.delete(schema.sportsSeasonTournaments)
|
||
|
|
.where(
|
||
|
|
and(
|
||
|
|
eq(schema.sportsSeasonTournaments.sportsSeasonId, sportsSeasonId),
|
||
|
|
eq(schema.sportsSeasonTournaments.tournamentId, tournamentId)
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function findTournamentsBySportsSeason(sportsSeasonId: string) {
|
||
|
|
const db = database();
|
||
|
|
return await db.query.sportsSeasonTournaments.findMany({
|
||
|
|
where: eq(schema.sportsSeasonTournaments.sportsSeasonId, sportsSeasonId),
|
||
|
|
with: {
|
||
|
|
tournament: true,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function findSportsSeasonsByTournament(tournamentId: string) {
|
||
|
|
const db = database();
|
||
|
|
return await db.query.sportsSeasonTournaments.findMany({
|
||
|
|
where: eq(schema.sportsSeasonTournaments.tournamentId, tournamentId),
|
||
|
|
with: {
|
||
|
|
sportsSeason: {
|
||
|
|
with: {
|
||
|
|
sport: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|