051b729451
Add API endpoints for team management: - POST /api/tournaments/[id]/teams/generate: Generate teams based on configuration - DELETE /api/tournaments/[id]/teams: Delete all teams - GET /api/tournaments/[id]/teams: Fetch teams and configuration Supports multiple generation strategies: - Random pairing - ELO-based pairing - Even matches - Minimize repeat partnerships Refs #22
136 lines
3.3 KiB
TypeScript
136 lines
3.3 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import { prisma } from "@/lib/prisma"
|
|
import { canManageTournament } from "@/lib/permissions"
|
|
|
|
interface RouteParams {
|
|
params: Promise<{
|
|
id: string
|
|
}>
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/tournaments/[id]/teams
|
|
*
|
|
* Delete all teams for a tournament.
|
|
* This will also delete any associated schedule.
|
|
*/
|
|
export async function DELETE(_request: Request, { params }: RouteParams) {
|
|
try {
|
|
const { id } = await params
|
|
const tournamentId = parseInt(id)
|
|
|
|
if (isNaN(tournamentId)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid tournament ID" },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const permission = await canManageTournament(tournamentId)
|
|
if (!permission.allowed) {
|
|
return NextResponse.json(
|
|
{ error: permission.reason || "Not authorized to manage this tournament" },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
// Check if tournament exists
|
|
const tournament = await prisma.event.findUnique({
|
|
where: { id: tournamentId },
|
|
include: {
|
|
teams: true,
|
|
rounds: true,
|
|
},
|
|
})
|
|
|
|
if (!tournament) {
|
|
return NextResponse.json(
|
|
{ error: "Tournament not found" },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
// Check if schedule exists
|
|
if (tournament.rounds.length > 0) {
|
|
return NextResponse.json(
|
|
{ error: "Cannot delete teams while schedule exists. Delete schedule first." },
|
|
{ status: 409 }
|
|
)
|
|
}
|
|
|
|
// Delete all teams
|
|
const deletedTeams = await prisma.team.deleteMany({
|
|
where: { eventId: tournamentId },
|
|
})
|
|
|
|
// Reset team configuration
|
|
await prisma.event.update({
|
|
where: { id: tournamentId },
|
|
data: {
|
|
teamDurability: "permanent",
|
|
partnerRotation: "none",
|
|
allowByes: true,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
deletedTeams: deletedTeams.count,
|
|
})
|
|
} catch (error: unknown) {
|
|
console.error("Error deleting teams:", error)
|
|
const message = error instanceof Error ? error.message : "Failed to delete teams"
|
|
return NextResponse.json({ error: message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/tournaments/[id]/teams
|
|
*
|
|
* Get all teams for a tournament.
|
|
*/
|
|
export async function GET(_request: Request, { params }: RouteParams) {
|
|
try {
|
|
const { id } = await params
|
|
const tournamentId = parseInt(id)
|
|
|
|
if (isNaN(tournamentId)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid tournament ID" },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Get tournament and teams
|
|
const tournament = await prisma.event.findUnique({
|
|
where: { id: tournamentId },
|
|
include: {
|
|
teams: {
|
|
include: {
|
|
player1: true,
|
|
player2: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
if (!tournament) {
|
|
return NextResponse.json(
|
|
{ error: "Tournament not found" },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
teams: tournament.teams,
|
|
teamDurability: tournament.teamDurability,
|
|
partnerRotation: tournament.partnerRotation,
|
|
allowByes: tournament.allowByes,
|
|
})
|
|
} catch (error: unknown) {
|
|
console.error("Error fetching teams:", error)
|
|
const message = error instanceof Error ? error.message : "Failed to fetch teams"
|
|
return NextResponse.json({ error: message }, { status: 500 })
|
|
}
|
|
}
|