reactions, match sse, etc

This commit is contained in:
yohlo
2025-09-19 14:08:36 -05:00
parent 602e6e3473
commit f99d6efaf9
20 changed files with 474 additions and 100 deletions

View File

@@ -3,6 +3,7 @@ import { createPlayersService } from "./services/players";
import { createTournamentsService } from "./services/tournaments";
import { createTeamsService } from "./services/teams";
import { createMatchesService } from "./services/matches";
import { createReactionsService } from "./services/reactions";
class PocketBaseAdminClient {
private pb: PocketBase;
@@ -31,6 +32,7 @@ class PocketBaseAdminClient {
Object.assign(this, createTeamsService(this.pb));
Object.assign(this, createTournamentsService(this.pb));
Object.assign(this, createMatchesService(this.pb));
Object.assign(this, createReactionsService(this.pb));
});
}
@@ -49,7 +51,8 @@ interface AdminClient
ReturnType<typeof createPlayersService>,
ReturnType<typeof createTeamsService>,
ReturnType<typeof createTournamentsService>,
ReturnType<typeof createMatchesService> {
ReturnType<typeof createMatchesService>,
ReturnType<typeof createReactionsService> {
authPromise: Promise<void>;
}

View File

@@ -6,7 +6,6 @@ import { transformMatch } from "../util/transform-types";
export function createMatchesService(pb: PocketBase) {
return {
async getMatch(id: string): Promise<Match | null> {
logger.info("PocketBase | Getting match", id);
const result = await pb.collection("matches").getOne(id, {
expand: "tournament, home, away",
});
@@ -15,7 +14,6 @@ export function createMatchesService(pb: PocketBase) {
// match Ids where the current lid is home_from_lid or away_from_lid
async getChildMatches(matchId: string): Promise<{ winner: Match | undefined, loser: Match | undefined }> {
logger.info("PocketBase | Getting child matches", matchId);
const match = await this.getMatch(matchId);
if (!match) throw new Error("Match not found")
@@ -52,7 +50,7 @@ export function createMatchesService(pb: PocketBase) {
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, {
expand: 'home, away'
expand: 'home, away, tournament'
});
return transformMatch(result);
},

View File

@@ -80,7 +80,9 @@ export function createPlayersService(pb: PocketBase) {
},
async getPlayerMatches(playerId: string): Promise<Match[]> {
const player = await pb.collection("players").getOne(playerId, {
console.log('----------------')
console.log(playerId)
const player = await pb.collection("players").getOne(playerId.trim(), {
expand: "teams",
});

View File

@@ -0,0 +1,35 @@
import PocketBase from "pocketbase";
import { transformReaction } from "../util/transform-types";
export const createReactionsService = (pb: PocketBase) => ({
async getReactionsForMatch(matchId: string) {
const reactions = await pb.collection('reactions').getFullList({
filter: `match="${matchId}"`,
expand: 'player',
});
return reactions.map(transformReaction)
},
async getUserReaction(matchId: string, userId: string, emoji: string) {
try {
return await pb.collection('reactions').getFirstListItem(`match="${matchId}" && player="${userId}" && emoji="${emoji}"`);
} catch (error) {
return;
}
},
async createReaction(matchId: string, userId: string, emoji: string) {
const reaction = await pb.collection('reactions').create({
match: matchId,
player: userId,
emoji: emoji,
}, {
expand: 'player'
});
return transformReaction(reaction)
},
async deleteReaction(reactionId: string) {
return await pb.collection('reactions').delete(reactionId);
},
});

View File

@@ -7,7 +7,6 @@ import { Match } from "@/features/matches/types";
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,logo",
expand: "players"
@@ -25,7 +24,6 @@ export function createTeamsService(pb: PocketBase) {
},
async getTeam(id: string): Promise<Team | null> {
logger.info("PocketBase | Getting team", id);
const result = await pb.collection("teams").getOne(id, {
expand: "players, tournaments",
});
@@ -84,7 +82,6 @@ export function createTeamsService(pb: PocketBase) {
},
async getTeamStats(id: string): Promise<TeamStats | null> {
logger.info("PocketBase | Getting team stats", id);
try {
const result = await pb.collection("team_stats").getFirstListItem(`team_id="${id}"`);
return result as unknown as TeamStats;

View File

@@ -23,7 +23,7 @@ export function createTournamentsService(pb: PocketBase) {
.collection("tournaments")
.getFirstListItem('',
{
expand: "teams, teams.players, matches, matches.tournament, matches.home, matches.away",
expand: "teams, teams.players, matches, matches.tournament, matches.home, matches.away, matches.home.players, matches.away.players",
sort: "-created",
}
);
@@ -104,10 +104,6 @@ export function createTournamentsService(pb: PocketBase) {
},
async getUnenrolledTeams(tournamentId: string): Promise<Team[]> {
try {
logger.info(
"PocketBase | Getting unenrolled teams for tournament",
tournamentId
);
const tournament = await pb
.collection("tournaments")
.getOne(tournamentId, {

View File

@@ -1,3 +1,4 @@
import { Reaction } from "@/features/matches/server";
import { Match } from "@/features/matches/types";
import { Player, PlayerInfo } from "@/features/players/types";
import { Team, TeamInfo } from "@/features/teams/types";
@@ -149,3 +150,12 @@ export function transformTournament(record: any): Tournament {
matches
};
}
export function transformReaction(record: any) {
return {
id: record.id,
emoji: record.emoji,
player: transformPlayerInfo(record.expand.player),
match: record.match
};
}