better brackets, info types

This commit is contained in:
yohlo
2025-09-07 00:52:28 -05:00
parent cb83ea06fa
commit 2396464a19
36 changed files with 678 additions and 657 deletions

View File

@@ -2,6 +2,7 @@ import PocketBase from "pocketbase";
import { createPlayersService } from "./services/players";
import { createTournamentsService } from "./services/tournaments";
import { createTeamsService } from "./services/teams";
import { createMatchesService } from "./services/matches";
class PocketBaseAdminClient {
private pb: PocketBase;
@@ -29,6 +30,7 @@ class PocketBaseAdminClient {
Object.assign(this, createPlayersService(this.pb));
Object.assign(this, createTeamsService(this.pb));
Object.assign(this, createTournamentsService(this.pb));
Object.assign(this, createMatchesService(this.pb));
});
}
@@ -46,7 +48,8 @@ interface AdminClient
extends PocketBaseAdminClient,
ReturnType<typeof createPlayersService>,
ReturnType<typeof createTeamsService>,
ReturnType<typeof createTournamentsService> {
ReturnType<typeof createTournamentsService>,
ReturnType<typeof createMatchesService> {
authPromise: Promise<void>;
}

View File

@@ -0,0 +1,4 @@
export interface DataFetchOptions {
includeRelations?: boolean;
expand?: string;
}

View File

@@ -0,0 +1,39 @@
import { logger } from "@/lib/logger";
import type { Match, MatchInput } from "@/features/matches/types";
import type PocketBase from "pocketbase";
export function createMatchesService(pb: PocketBase) {
return {
async createMatch(data: MatchInput): Promise<Match> {
logger.info("PocketBase | Creating match", data);
const result = await pb.collection("matches").create<Match>(data);
return result;
},
async createMatches(matches: MatchInput[]): Promise<Match[]> {
logger.info("PocketBase | Creating multiple matches", { count: matches.length });
const results = await Promise.all(
matches.map(match => pb.collection("matches").create<Match>(match))
);
return results;
},
async updateMatch(id: string, data: Partial<MatchInput>): Promise<Match> {
logger.info("PocketBase | Updating match", { id, data });
const result = await pb.collection("matches").update<Match>(id, data);
return result;
},
async deleteMatchesByTournament(tournamentId: string): Promise<void> {
logger.info("PocketBase | Deleting matches for tournament", tournamentId);
const matches = await pb.collection("matches").getFullList({
filter: `tournament = "${tournamentId}"`,
fields: "id",
});
await Promise.all(
matches.map(match => pb.collection("matches").delete(match.id))
);
},
};
}

View File

@@ -1,13 +1,28 @@
import type {
Player,
PlayerInfo,
PlayerInput,
PlayerUpdateInput,
} from "@/features/players/types";
import { transformPlayer } from "@/lib/pocketbase/util/transform-types";
import { transformPlayer, transformPlayerInfo } from "@/lib/pocketbase/util/transform-types";
import PocketBase from "pocketbase";
import { DataFetchOptions } from "./base";
export function createPlayersService(pb: PocketBase) {
return {
async getPlayerInfo(id: string): Promise<PlayerInfo> {
const result = await pb.collection("players").getOne(id, {
fields: "id,first_name,last_name"
});
return transformPlayerInfo(result);
},
async listPlayerInfos(): Promise<PlayerInfo[]> {
const result = await pb.collection("players").getFullList({
fields: "id,first_name,last_name",
});
return result.map(transformPlayerInfo);
},
async getPlayerByAuthId(authId: string): Promise<Player | null> {
const result = await pb.collection("players").getList<Player>(1, 1, {
filter: `auth_id = "${authId}"`,

View File

@@ -1,10 +1,26 @@
import { logger } from "@/lib/logger";
import PocketBase from "pocketbase";
import { transformTeam } from "@/lib/pocketbase/util/transform-types";
import { Team } from "@/features/teams/types";
import { transformTeam, transformTeamInfo } from "@/lib/pocketbase/util/transform-types";
import { Team, TeamInfo } from "@/features/teams/types";
import { DataFetchOptions } from "./base";
export function createTeamsService(pb: PocketBase) {
return {
async getTeamInfo(id: string): Promise<TeamInfo> {
logger.info("PocketBase | Getting team info", id);
const result = await pb.collection("teams").getOne(id, {
fields: "id,name,primary_color,accent_color"
});
return transformTeamInfo(result);
},
async listTeamInfos(): Promise<TeamInfo[]> {
logger.info("PocketBase | Listing team infos");
const result = await pb.collection("teams").getFullList({
fields: "id,name,primary_color,accent_color"
});
return result.map(transformTeamInfo);
},
async getTeam(id: string): Promise<Team | null> {
logger.info("PocketBase | Getting team", id);
const result = await pb.collection("teams").getOne(id, {

View File

@@ -1,32 +1,32 @@
import { logger } from "@/lib/logger";
import type {
Tournament,
TournamentInfo,
TournamentInput,
TournamentUpdateInput,
} from "@/features/tournaments/types";
import type { Team } from "@/features/teams/types";
import PocketBase from "pocketbase";
import { transformTournament } from "@/lib/pocketbase/util/transform-types";
import { transformTournament, transformTournamentInfo } from "@/lib/pocketbase/util/transform-types";
import { transformTeam } from "@/lib/pocketbase/util/transform-types";
export function createTournamentsService(pb: PocketBase) {
return {
async getTournament(id: string): Promise<Tournament | null> {
logger.info("PocketBase | Getting tournament", id);
async getTournament(id: string): Promise<Tournament> {
const result = await pb.collection("tournaments").getOne(id, {
expand: "teams, teams.players",
expand: "teams, teams.players, matches, matches.tournament",
});
return transformTournament(result);
},
async listTournaments(): Promise<Tournament[]> {
async listTournaments(): Promise<TournamentInfo[]> {
const result = await pb
.collection("tournaments")
.getFullList<Tournament>({
fields: "id,name,start_time,end_time,logo,created",
.getFullList({
fields: "id,name,location,start_time,end_time,logo",
sort: "-created",
});
return result.map(transformTournament);
return result.map(transformTournamentInfo);
},
async createTournament(data: TournamentInput): Promise<Tournament> {
const result = await pb
@@ -79,6 +79,18 @@ export function createTournamentsService(pb: PocketBase) {
return transformTournament(result);
},
async updateTournamentMatches(
tournamentId: string,
matchIds: string[]
): Promise<Tournament> {
logger.info("PocketBase | Updating tournament matches", { tournamentId, matchCount: matchIds.length });
const result = await pb
.collection("tournaments")
.update<Tournament>(tournamentId, {
matches: matchIds
});
return transformTournament(result);
},
async getUnenrolledTeams(tournamentId: string): Promise<Team[]> {
try {
logger.info(

View File

@@ -1,18 +1,75 @@
import { Player } from "@/features/players/types";
import { Team } from "@/features/teams/types";
import { Tournament } from "@/features/tournaments/types";
import { Match } from "@/features/matches/types";
import { Player, PlayerInfo } from "@/features/players/types";
import { Team, TeamInfo } from "@/features/teams/types";
import { Tournament, TournamentInfo } from "@/features/tournaments/types";
// pocketbase does this weird thing with relations where it puts them under a seperate "expand" field
// this file transforms raw pocketbase results to our types
export function transformPlayerInfo(record: any): PlayerInfo {
return {
id: record.id,
first_name: record.first_name,
last_name: record.last_name,
};
}
export function transformTeamInfo(record: any): TeamInfo {
const players = record.expand?.players?.map(transformPlayerInfo) ?? [];
return {
id: record.id,
name: record.name,
primary_color: record.primary_color,
accent_color: record.accent_color,
players,
};
}
export const transformMatch = (record: any): Match => {
return {
id: record.id,
order: record.name,
lid: record.lid,
reset: record.reset,
round: record.round,
home_cups: record.home_cups,
away_cups: record.away_cups,
ot_count: record.ot_count,
start_time: record.start_time,
end_time: record.end_time,
bye: record.bye,
home_from_lid: record.home_from_lid,
away_from_lid: record.away_from_lid,
home_from_loser: record.home_from_loser,
away_from_loser: record.away_from_loser,
is_losers_bracket: record.is_losers_bracket,
tournament: transformTournamentInfo(record.expand?.tournament),
home: record.expand?.home ? transformTeamInfo(record.expand.home) : undefined,
away: record.expand?.away ? transformTeamInfo(record.expand.away) : undefined,
created: record.created,
updated: record.updated,
home_seed: record.home_seed,
away_seed: record.away_seed,
};
}
export const transformTournamentInfo = (record: any): TournamentInfo => {
return {
id: record.id,
name: record.name,
location: record.location,
start_time: record.start_time,
logo: record.logo,
};
}
export function transformPlayer(record: any): Player {
const sadf: string[] = [];
const teams =
record.expand?.teams
?.sort((a: Team, b: Team) =>
?.sort((a: any, b: any) =>
new Date(a.created) < new Date(b.created) ? -1 : 0
)
?.map(transformTeam) ?? [];
?.map(transformTeamInfo) ?? [];
return {
id: record.id!,
@@ -28,16 +85,16 @@ export function transformPlayer(record: any): Player {
export function transformTeam(record: any): Team {
const players =
record.expand?.players
?.sort((a: Player, b: Player) =>
?.sort((a: any, b: any) =>
new Date(a.created!) < new Date(b.created!) ? -1 : 0
)
?.map(transformPlayer) ?? [];
?.map(transformPlayerInfo) ?? [];
const tournaments =
record.expand?.tournaments
?.sort((a: Tournament, b: Tournament) =>
?.sort((a: any, b: any) =>
new Date(a.created!) < new Date(b.created!) ? -1 : 0
)
?.map(transformTournament) ?? [];
?.map(transformTournamentInfo) ?? [];
return {
id: record.id,
@@ -63,11 +120,18 @@ export function transformTeam(record: any): Team {
export function transformTournament(record: any): Tournament {
const teams =
record.expand?.teams
?.sort((a: Team, b: Team) =>
?.sort((a: any, b: any) =>
new Date(a.created) < new Date(b.created) ? -1 : 0
)
?.map(transformTeam) ?? [];
?.map(transformTeamInfo) ?? [];
const matches =
record.expand?.matches
?.sort((a: any, b: any) =>
a.lid - b.lid ? -1 : 0
)
?.map(transformMatch) ?? [];
return {
id: record.id,
name: record.name,
@@ -81,5 +145,6 @@ export function transformTournament(record: any): Tournament {
created: record.created,
updated: record.updated,
teams,
matches
};
}