significant refactor
This commit is contained in:
@@ -1,46 +1,50 @@
|
||||
import type { Player, PlayerInput, PlayerUpdateInput } from '@/features/players/types';
|
||||
import { transformPlayer } from '@/lib/pocketbase/util/transform-types';
|
||||
import PocketBase from 'pocketbase';
|
||||
import type {
|
||||
Player,
|
||||
PlayerInput,
|
||||
PlayerUpdateInput,
|
||||
} from "@/features/players/types";
|
||||
import { transformPlayer } from "@/lib/pocketbase/util/transform-types";
|
||||
import PocketBase from "pocketbase";
|
||||
|
||||
export function createPlayersService(pb: PocketBase) {
|
||||
return {
|
||||
async getPlayerByAuthId(authId: string): Promise<Player | null> {
|
||||
const result = await pb.collection('players').getList<Player>(1, 1, {
|
||||
filter: `auth_id = "${authId}"`
|
||||
const result = await pb.collection("players").getList<Player>(1, 1, {
|
||||
filter: `auth_id = "${authId}"`,
|
||||
});
|
||||
return result.items[0] ? transformPlayer(result.items[0]) : null;
|
||||
},
|
||||
|
||||
async getPlayer(id: string): Promise<Player | null> {
|
||||
const result = await pb.collection('players').getOne(id, {
|
||||
expand: 'teams'
|
||||
async getPlayer(id: string): Promise<Player> {
|
||||
const result = await pb.collection("players").getOne(id, {
|
||||
expand: "teams",
|
||||
});
|
||||
return transformPlayer(result);
|
||||
},
|
||||
|
||||
async listPlayers(): Promise<Player[]> {
|
||||
const result = await pb.collection('players').getFullList<Player>({
|
||||
fields: 'id,first_name,last_name'
|
||||
const result = await pb.collection("players").getFullList<Player>({
|
||||
fields: "id,first_name,last_name",
|
||||
});
|
||||
return result.map(transformPlayer);
|
||||
},
|
||||
|
||||
async createPlayer(data: PlayerInput): Promise<Player> {
|
||||
const result = await pb.collection('players').create<Player>(data);
|
||||
const result = await pb.collection("players").create<Player>(data);
|
||||
return transformPlayer(result);
|
||||
},
|
||||
|
||||
async updatePlayer(id: string, data: PlayerUpdateInput): Promise<Player> {
|
||||
const result = await pb.collection('players').update<Player>(id, data);
|
||||
const result = await pb.collection("players").update<Player>(id, data);
|
||||
return transformPlayer(result);
|
||||
},
|
||||
|
||||
async getUnassociatedPlayers(): Promise<Player[]> {
|
||||
const result = await pb.collection('players').getFullList<Player>({
|
||||
const result = await pb.collection("players").getFullList<Player>({
|
||||
filter: 'auth_id = ""',
|
||||
fields: 'id,first_name,last_name'
|
||||
fields: "id,first_name,last_name",
|
||||
});
|
||||
return result.map(transformPlayer);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,15 +6,11 @@ import { Team } from "@/features/teams/types";
|
||||
export function createTeamsService(pb: PocketBase) {
|
||||
return {
|
||||
async getTeam(id: string): Promise<Team | null> {
|
||||
try {
|
||||
logger.info('PocketBase | Getting team', id);
|
||||
const result = await pb.collection('teams').getOne(id, {
|
||||
expand: 'players, tournaments'
|
||||
});
|
||||
return transformTeam(result);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
logger.info("PocketBase | Getting team", id);
|
||||
const result = await pb.collection("teams").getOne(id, {
|
||||
expand: "players, tournaments",
|
||||
});
|
||||
return transformTeam(result);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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