significant refactor
This commit is contained in:
@@ -12,15 +12,11 @@ import { transformTeam } from "@/lib/pocketbase/util/transform-types";
|
||||
export function createTournamentsService(pb: PocketBase) {
|
||||
return {
|
||||
async getTournament(id: string): Promise<Tournament | null> {
|
||||
try {
|
||||
logger.info("PocketBase | Getting tournament", id);
|
||||
const result = await pb.collection("tournaments").getOne(id, {
|
||||
expand: "teams, teams.players",
|
||||
});
|
||||
return transformTournament(result);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
logger.info("PocketBase | Getting tournament", id);
|
||||
const result = await pb.collection("tournaments").getOne(id, {
|
||||
expand: "teams, teams.players",
|
||||
});
|
||||
return transformTournament(result);
|
||||
},
|
||||
async listTournaments(): Promise<Tournament[]> {
|
||||
const result = await pb
|
||||
@@ -51,60 +47,67 @@ export function createTournamentsService(pb: PocketBase) {
|
||||
tournamentId: string,
|
||||
teamId: string
|
||||
): Promise<Tournament> {
|
||||
const result = await pb.collection("tournaments").update<Tournament>(
|
||||
tournamentId,
|
||||
{ "teams+": teamId },
|
||||
{ expand: "teams, teams.players" }
|
||||
);
|
||||
|
||||
await pb.collection("teams").update(
|
||||
teamId,
|
||||
{ "tournaments+": tournamentId }
|
||||
);
|
||||
|
||||
const result = await pb
|
||||
.collection("tournaments")
|
||||
.update<Tournament>(
|
||||
tournamentId,
|
||||
{ "teams+": teamId },
|
||||
{ expand: "teams, teams.players" }
|
||||
);
|
||||
|
||||
await pb
|
||||
.collection("teams")
|
||||
.update(teamId, { "tournaments+": tournamentId });
|
||||
|
||||
return transformTournament(result);
|
||||
},
|
||||
async unenrollTeam(
|
||||
tournamentId: string,
|
||||
teamId: string
|
||||
): Promise<Tournament> {
|
||||
const result = await pb.collection("tournaments").update<Tournament>(
|
||||
tournamentId,
|
||||
{ "teams-": teamId },
|
||||
{ expand: "teams, teams.players" }
|
||||
);
|
||||
|
||||
await pb.collection("teams").update(
|
||||
teamId,
|
||||
{ "tournaments-": tournamentId }
|
||||
);
|
||||
|
||||
const result = await pb
|
||||
.collection("tournaments")
|
||||
.update<Tournament>(
|
||||
tournamentId,
|
||||
{ "teams-": teamId },
|
||||
{ expand: "teams, teams.players" }
|
||||
);
|
||||
|
||||
await pb
|
||||
.collection("teams")
|
||||
.update(teamId, { "tournaments-": tournamentId });
|
||||
|
||||
return transformTournament(result);
|
||||
},
|
||||
async getUnenrolledTeams(tournamentId: string): Promise<Team[]> {
|
||||
try {
|
||||
logger.info("PocketBase | Getting unenrolled teams for tournament", tournamentId);
|
||||
const tournament = await pb.collection("tournaments").getOne(tournamentId, {
|
||||
fields: "teams"
|
||||
});
|
||||
|
||||
logger.info(
|
||||
"PocketBase | Getting unenrolled teams for tournament",
|
||||
tournamentId
|
||||
);
|
||||
const tournament = await pb
|
||||
.collection("tournaments")
|
||||
.getOne(tournamentId, {
|
||||
fields: "teams",
|
||||
});
|
||||
|
||||
const enrolledTeamIds = tournament.teams || [];
|
||||
if (enrolledTeamIds.length === 0) {
|
||||
const allTeams = await pb.collection("teams").getFullList({
|
||||
expand: "players"
|
||||
expand: "players",
|
||||
});
|
||||
return allTeams.map(transformTeam);
|
||||
}
|
||||
|
||||
|
||||
const filter = enrolledTeamIds
|
||||
.map((teamId: string) => `id != "${teamId}"`)
|
||||
.join(" && ");
|
||||
|
||||
|
||||
const availableTeams = await pb.collection("teams").getFullList({
|
||||
filter,
|
||||
expand: "players"
|
||||
expand: "players",
|
||||
});
|
||||
|
||||
|
||||
return availableTeams.map(transformTeam);
|
||||
} catch (error) {
|
||||
logger.error("PocketBase | Error getting unenrolled teams", error);
|
||||
|
||||
Reference in New Issue
Block a user