groups init
This commit is contained in:
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);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user