groups init
This commit is contained in:
@@ -6,6 +6,7 @@ import { createMatchesService } from "./services/matches";
|
||||
import { createReactionsService } from "./services/reactions";
|
||||
import { createActivitiesService } from "./services/activities";
|
||||
import { createBadgesService } from "./services/badges";
|
||||
import { createGroupsService } from "./services/groups";
|
||||
|
||||
class PocketBaseAdminClient {
|
||||
private pb: PocketBase;
|
||||
@@ -46,6 +47,7 @@ class PocketBaseAdminClient {
|
||||
Object.assign(this, createReactionsService(this.pb));
|
||||
Object.assign(this, createActivitiesService(this.pb));
|
||||
Object.assign(this, createBadgesService(this.pb));
|
||||
Object.assign(this, createGroupsService(this.pb));
|
||||
|
||||
this.authPromise = this.authenticate();
|
||||
this.authPromise.then(() => {
|
||||
@@ -123,7 +125,8 @@ interface AdminClient
|
||||
ReturnType<typeof createMatchesService>,
|
||||
ReturnType<typeof createReactionsService>,
|
||||
ReturnType<typeof createActivitiesService>,
|
||||
ReturnType<typeof createBadgesService> {
|
||||
ReturnType<typeof createBadgesService>,
|
||||
ReturnType<typeof createGroupsService> {
|
||||
authPromise: Promise<void>;
|
||||
}
|
||||
|
||||
|
||||
46
src/lib/pocketbase/services/groups.ts
Normal file
46
src/lib/pocketbase/services/groups.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { logger } from "@/lib/logger";
|
||||
import PocketBase from "pocketbase";
|
||||
import { Group } from "@/features/tournaments/types";
|
||||
|
||||
export interface GroupInput {
|
||||
tournament: string;
|
||||
name: string;
|
||||
order: number;
|
||||
teams: string[];
|
||||
}
|
||||
|
||||
export function createGroupsService(pb: PocketBase) {
|
||||
return {
|
||||
async createGroup(data: GroupInput): Promise<Group> {
|
||||
logger.info("PocketBase | Creating group", data);
|
||||
const result = await pb.collection("groups").create(data);
|
||||
return result as unknown as Group;
|
||||
},
|
||||
|
||||
async getGroupsByTournament(tournamentId: string): Promise<Group[]> {
|
||||
logger.info("PocketBase | Getting groups for tournament", { tournamentId });
|
||||
const result = await pb.collection("groups").getFullList({
|
||||
filter: `tournament = "${tournamentId}"`,
|
||||
sort: "order",
|
||||
expand: "teams,teams.players"
|
||||
});
|
||||
return result as unknown as Group[];
|
||||
},
|
||||
|
||||
async deleteGroup(groupId: string): Promise<void> {
|
||||
logger.info("PocketBase | Deleting group", { groupId });
|
||||
await pb.collection("groups").delete(groupId);
|
||||
},
|
||||
|
||||
async deleteGroupsByTournament(tournamentId: string): Promise<void> {
|
||||
logger.info("PocketBase | Deleting all groups for tournament", { tournamentId });
|
||||
const groups = await pb.collection("groups").getFullList({
|
||||
filter: `tournament = "${tournamentId}"`
|
||||
});
|
||||
|
||||
for (const group of groups) {
|
||||
await pb.collection("groups").delete(group.id);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -14,17 +14,23 @@ import { PlayerInfo } from "@/features/players/types";
|
||||
export function createTournamentsService(pb: PocketBase) {
|
||||
return {
|
||||
async getTournament(id: string, isAdmin: boolean = false): Promise<Tournament> {
|
||||
const [tournamentResult, teamStatsResult] = await Promise.all([
|
||||
const [tournamentResult, teamStatsResult, groupsResult] = await Promise.all([
|
||||
pb.collection("tournaments").getOne(id, {
|
||||
expand: "teams, teams.players, matches, matches.tournament, matches.home, matches.away, matches.home.players, matches.away.players",
|
||||
}),
|
||||
pb.collection("team_stats_per_tournament").getFullList({
|
||||
filter: `tournament_id = "${id}"`,
|
||||
sort: "-wins,-total_cups_made"
|
||||
}),
|
||||
pb.collection("groups").getFullList({
|
||||
filter: `tournament = "${id}"`,
|
||||
sort: "order",
|
||||
expand: "teams, teams.players"
|
||||
})
|
||||
]);
|
||||
|
||||
tournamentResult.team_stats = teamStatsResult;
|
||||
tournamentResult.groups = groupsResult;
|
||||
|
||||
return transformTournament(tournamentResult, isAdmin);
|
||||
},
|
||||
|
||||
@@ -55,6 +55,8 @@ export const transformMatch = (record: any, isAdmin: boolean = false): Match =>
|
||||
updated: record.updated,
|
||||
home_seed: record.home_seed,
|
||||
away_seed: record.away_seed,
|
||||
match_type: record.match_type,
|
||||
group: record.group,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -247,6 +249,16 @@ export function transformTournament(record: any, isAdmin: boolean = false): Tour
|
||||
}
|
||||
}
|
||||
|
||||
const groups = (record.groups || record.expand?.groups)?.map((group: any) => ({
|
||||
id: group.id,
|
||||
tournament: group.tournament,
|
||||
name: group.name,
|
||||
order: group.order,
|
||||
teams: group.expand?.teams?.map(transformTeamInfo) ?? [],
|
||||
created: group.created,
|
||||
updated: group.updated,
|
||||
})) ?? [];
|
||||
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
@@ -261,8 +273,12 @@ export function transformTournament(record: any, isAdmin: boolean = false): Tour
|
||||
created: record.created,
|
||||
updated: record.updated,
|
||||
regional: record.regional || false,
|
||||
format: record.format,
|
||||
phase: record.phase,
|
||||
group_config: record.group_config,
|
||||
teams,
|
||||
matches,
|
||||
groups,
|
||||
first_place,
|
||||
second_place,
|
||||
third_place,
|
||||
|
||||
Reference in New Issue
Block a user