603cc238fa
- Add manual team entry for permanent teams in Matchups tab - Rename 'Teams' tab to 'Matchups' for clarity - Implement partner rotation strategies (minimize_repeat, maximize_even, elo_based) - Track partnerships across rounds to minimize repeat pairings - Fix unit tests for team configuration and ELO calculations - Add E2E test for 9+ participant tournaments with variable matchups Key changes: - TeamsSection.tsx: Added router.refresh(), manual team entry, and matchup display - schedule-generator.ts: Enhanced generateVariableRoundRobin to track partnerships - team-generator.ts: Fixed partnership frequency tracking across rounds - API routes: Updated to support variable team durability with rotation strategies
217 lines
5.5 KiB
TypeScript
217 lines
5.5 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { canManageTournament } from "@/lib/permissions";
|
|
|
|
/**
|
|
* GET /api/tournaments/[id]/participants
|
|
*
|
|
* Get all participants for a tournament
|
|
*/
|
|
export async function GET(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params
|
|
const tournamentId = parseInt(id, 10);
|
|
|
|
if (isNaN(tournamentId)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid tournament ID" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Check permissions
|
|
const permission = await canManageTournament(tournamentId);
|
|
if (!permission.allowed) {
|
|
return NextResponse.json(
|
|
{ error: permission.reason || 'Insufficient permissions' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// Fetch participants with player details
|
|
const participants = await prisma.eventParticipant.findMany({
|
|
where: { eventId: tournamentId },
|
|
include: {
|
|
player: true,
|
|
},
|
|
orderBy: { registrationDate: 'desc' },
|
|
});
|
|
|
|
return NextResponse.json({ participants });
|
|
} catch (error) {
|
|
console.error("Failed to fetch participants:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch participants" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params
|
|
const tournamentId = parseInt(id, 10);
|
|
|
|
if (isNaN(tournamentId)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid tournament ID" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { playerIds } = body;
|
|
|
|
if (!playerIds || !Array.isArray(playerIds)) {
|
|
return NextResponse.json(
|
|
{ error: "playerIds array is required" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Check permissions
|
|
const permission = await canManageTournament(tournamentId);
|
|
if (!permission.allowed) {
|
|
return NextResponse.json(
|
|
{ error: permission.reason || 'Insufficient permissions' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// Fetch tournament to check type
|
|
const tournament = await prisma.event.findUnique({
|
|
where: { id: tournamentId },
|
|
select: { tournamentType: true },
|
|
});
|
|
|
|
if (!tournament) {
|
|
return NextResponse.json(
|
|
{ error: "Tournament not found" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const isTeamTournament = tournament.tournamentType === "team";
|
|
|
|
// For team tournaments, validate even number of players
|
|
if (isTeamTournament && playerIds.length % 2 !== 0) {
|
|
return NextResponse.json(
|
|
{ error: "Team tournaments require an even number of players" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Add participants - teams will be generated later during schedule generation
|
|
const participants = await Promise.all(
|
|
playerIds.map(async (playerId: number) => {
|
|
try {
|
|
// Check if participant already exists
|
|
const existing = await prisma.eventParticipant.findFirst({
|
|
where: {
|
|
eventId: tournamentId,
|
|
playerId: playerId,
|
|
},
|
|
});
|
|
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
|
|
// Create new participant
|
|
return await prisma.eventParticipant.create({
|
|
data: {
|
|
eventId: tournamentId,
|
|
playerId: playerId,
|
|
status: "registered",
|
|
registrationDate: new Date(),
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error(`Failed to add participant ${playerId}:`, error);
|
|
return null;
|
|
}
|
|
})
|
|
);
|
|
|
|
const successfulParticipants = participants.filter(p => p !== null);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
added: successfulParticipants.length,
|
|
participants: successfulParticipants,
|
|
});
|
|
} catch (error) {
|
|
console.error("Failed to add participants:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to add participants" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/tournaments/[id]/participants
|
|
*
|
|
* Remove participants from a tournament
|
|
*/
|
|
export async function DELETE(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params
|
|
const tournamentId = parseInt(id, 10);
|
|
|
|
if (isNaN(tournamentId)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid tournament ID" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { playerIds } = body;
|
|
|
|
if (!playerIds || !Array.isArray(playerIds)) {
|
|
return NextResponse.json(
|
|
{ error: "playerIds array is required" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Check permissions
|
|
const permission = await canManageTournament(tournamentId);
|
|
if (!permission.allowed) {
|
|
return NextResponse.json(
|
|
{ error: permission.reason || 'Insufficient permissions' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// Remove participants
|
|
const deleted = await prisma.eventParticipant.deleteMany({
|
|
where: {
|
|
eventId: tournamentId,
|
|
playerId: { in: playerIds },
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
deleted: deleted.count,
|
|
});
|
|
} catch (error) {
|
|
console.error("Failed to remove participants:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to remove participants" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|