way better auth middleware, enroll team server fn and pb fn

This commit is contained in:
yohlo
2025-08-24 00:10:09 -05:00
parent 4427bd5494
commit 53276cc18e
6 changed files with 164 additions and 70 deletions

View File

@@ -1,5 +1,9 @@
import { logger } from "@/lib/logger";
import type { Tournament, TournamentInput, TournamentUpdateInput } from "@/features/tournaments/types";
import type {
Tournament,
TournamentInput,
TournamentUpdateInput,
} from "@/features/tournaments/types";
import PocketBase from "pocketbase";
import { transformTournament } from "@/lib/pocketbase/util/transform-types";
@@ -7,9 +11,9 @@ 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'
logger.info("PocketBase | Getting tournament", id);
const result = await pb.collection("tournaments").getOne(id, {
expand: "teams, teams.players",
});
return transformTournament(result);
} catch {
@@ -17,20 +21,40 @@ export function createTournamentsService(pb: PocketBase) {
}
},
async listTournaments(): Promise<Tournament[]> {
const result = await pb.collection('tournaments').getFullList<Tournament>({
fields: 'id,name,start_time,end_time,logo_url,created',
sort: '-created'
});
const result = await pb
.collection("tournaments")
.getFullList<Tournament>({
fields: "id,name,start_time,end_time,logo_url,created",
sort: "-created",
});
console.log(result)
console.log(result);
return result.map(transformTournament);
},
async createTournament(data: TournamentInput): Promise<Tournament> {
const result = await pb.collection('tournaments').create<Tournament>(data);
const result = await pb
.collection("tournaments")
.create<Tournament>(data);
return transformTournament(result);
},
async updateTournament(id: string, data: TournamentUpdateInput): Promise<Tournament> {
const result = await pb.collection('tournaments').update<Tournament>(id, data);
async updateTournament(
id: string,
data: TournamentUpdateInput
): Promise<Tournament> {
const result = await pb
.collection("tournaments")
.update<Tournament>(id, data);
return transformTournament(result);
},
async enrollTeam(
tournamentId: string,
teamId: string
): Promise<Tournament> {
const result = await pb.collection("tournaments").update<Tournament>(
tournamentId,
{ "teams+": teamId },
{ expand: "teams, teams.players" }
);
return transformTournament(result);
},
};