Merge branch 'bugfix/7-tournament-schedule-tests': Schedule generation, clickable matchups, and test fixes
Release / release (push) Failing after 11s
Release / release (push) Failing after 11s
Resolved conflict in common-steps.ts by combining player schedule step definitions (from bugfix/9 merge) with tournament schedule step definitions (from bugfix/7).
This commit is contained in:
@@ -73,7 +73,7 @@ export default function TournamentEntryPage({ params }: { params: Promise<{ id:
|
||||
const [team1Score, setTeam1Score] = useState("")
|
||||
const [team2Score, setTeam2Score] = useState("")
|
||||
|
||||
// Parse params and validate tournamentId
|
||||
// Parse params and validate tournamentId, check for matchup query param
|
||||
useEffect(() => {
|
||||
async function parseParams() {
|
||||
const { id } = await params
|
||||
@@ -87,6 +87,26 @@ export default function TournamentEntryPage({ params }: { params: Promise<{ id:
|
||||
parseParams()
|
||||
}, [params, router])
|
||||
|
||||
// Handle pre-selection of matchup from query param
|
||||
useEffect(() => {
|
||||
if (schedule && selectedMatchupId === null) {
|
||||
const searchParams = new URLSearchParams(window.location.search)
|
||||
const matchupIdParam = searchParams.get('matchup')
|
||||
if (matchupIdParam) {
|
||||
const matchupId = parseInt(matchupIdParam, 10)
|
||||
// Find which round contains this matchup
|
||||
for (const round of schedule.rounds) {
|
||||
const matchup = round.matchups.find(m => m.id === matchupId)
|
||||
if (matchup) {
|
||||
setSelectedRoundId(round.id)
|
||||
setSelectedMatchupId(matchupId)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [schedule, selectedMatchupId])
|
||||
|
||||
// Load tournament, schedule, and matches
|
||||
useEffect(() => {
|
||||
if (tournamentId) {
|
||||
|
||||
@@ -2,6 +2,8 @@ import { prisma } from "@/lib/prisma"
|
||||
import Navigation from "@/components/Navigation"
|
||||
import Link from "next/link"
|
||||
import { notFound } from "next/navigation"
|
||||
import { ScheduleGenerator } from "@/components/ScheduleGenerator"
|
||||
import { ScheduleDisplay } from "@/components/ScheduleDisplay"
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{
|
||||
@@ -9,7 +11,9 @@ interface PageProps {
|
||||
}>
|
||||
}
|
||||
|
||||
// Force dynamic rendering and revalidate on each request
|
||||
export const dynamic = "force-dynamic"
|
||||
export const revalidate = 0
|
||||
|
||||
export default async function TournamentSchedulePage({ params }: PageProps) {
|
||||
const { id } = await params
|
||||
@@ -19,6 +23,7 @@ export default async function TournamentSchedulePage({ params }: PageProps) {
|
||||
notFound()
|
||||
}
|
||||
|
||||
console.log(`[Schedule Page] Fetching tournament ${tournamentId}`);
|
||||
const tournament = await prisma.event.findUnique({
|
||||
where: { id: tournamentId },
|
||||
include: {
|
||||
@@ -27,13 +32,35 @@ export default async function TournamentSchedulePage({ params }: PageProps) {
|
||||
player: true,
|
||||
},
|
||||
},
|
||||
rounds: {
|
||||
orderBy: { roundNumber: "asc" },
|
||||
include: {
|
||||
bracketMatchups: {
|
||||
orderBy: { bracketPosition: "asc" },
|
||||
include: {
|
||||
player1P1: true,
|
||||
player1P2: true,
|
||||
player2P1: true,
|
||||
player2P2: true,
|
||||
match: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
console.log(`[Schedule Page] Tournament ${tournamentId} has ${tournament?.rounds?.length || 0} rounds`);
|
||||
if (tournament?.rounds && tournament.rounds.length > 0) {
|
||||
console.log(`[Schedule Page] First round:`, JSON.stringify(tournament.rounds[0]));
|
||||
}
|
||||
|
||||
if (!tournament) {
|
||||
notFound()
|
||||
}
|
||||
|
||||
const teamCount = tournament.participants.length
|
||||
const existingRounds = tournament.rounds.length
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<Navigation />
|
||||
@@ -53,22 +80,33 @@ export default async function TournamentSchedulePage({ params }: PageProps) {
|
||||
Schedule - {tournament.name}
|
||||
</h1>
|
||||
|
||||
<div className="bg-white shadow rounded-lg p-6">
|
||||
<div className="bg-white shadow rounded-lg p-6 mb-6">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<h2 className="text-xl font-bold text-gray-900">
|
||||
Tournament Schedule
|
||||
</h2>
|
||||
<button className="bg-green-600 text-white px-4 py-2 rounded-md text-sm font-medium hover:bg-green-700">
|
||||
Generate Schedule
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p className="text-gray-500">
|
||||
No schedule has been generated yet. Click "Generate Schedule" to create round matchups.
|
||||
</p>
|
||||
<div id="schedule-display">
|
||||
{existingRounds > 0 ? (
|
||||
<ScheduleDisplay rounds={tournament.rounds} tournamentId={tournamentId} />
|
||||
) : (
|
||||
<p className="text-gray-500 mb-6">
|
||||
No schedule has been generated yet. Click "Generate Schedule" to create round matchups.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mt-6 pt-6 border-t border-gray-200">
|
||||
<ScheduleGenerator
|
||||
tournamentId={tournamentId}
|
||||
teamCount={teamCount}
|
||||
existingRounds={existingRounds}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { canManageTournament } from "@/lib/permissions";
|
||||
import { generateRoundRobin, validateScheduleInput, generateVariableRoundRobin, expectedRounds } from "@/lib/schedule-generator";
|
||||
@@ -78,11 +79,15 @@ export async function GET(_request: Request, { params }: RouteParams) {
|
||||
* Creates TournamentRound and BracketMatchup records.
|
||||
*/
|
||||
export async function POST(_request: Request, { params }: RouteParams) {
|
||||
console.log(`[Schedule API] POST handler started`);
|
||||
try {
|
||||
const { id } = await params;
|
||||
const tournamentId = parseInt(id);
|
||||
|
||||
console.log(`[Schedule API] POST /api/tournaments/${tournamentId}/schedule`);
|
||||
|
||||
if (isNaN(tournamentId)) {
|
||||
console.log(`[Schedule API] Invalid tournament ID: ${id}`);
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid tournament ID" },
|
||||
{ status: 400 }
|
||||
@@ -90,6 +95,7 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
}
|
||||
|
||||
const permission = await canManageTournament(tournamentId);
|
||||
console.log(`[Schedule API] Permission check: ${permission.allowed}, reason: ${permission.reason}`);
|
||||
if (!permission.allowed) {
|
||||
return NextResponse.json(
|
||||
{ error: permission.reason || "Not authorized to manage this tournament" },
|
||||
@@ -98,6 +104,7 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
}
|
||||
|
||||
// Check tournament exists
|
||||
console.log(`[Schedule API] Looking up tournament ${tournamentId}`);
|
||||
const tournament = await prisma.event.findUnique({
|
||||
where: { id: tournamentId },
|
||||
include: {
|
||||
@@ -111,14 +118,17 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
console.log(`[Schedule API] Tournament ${tournamentId} not found`);
|
||||
return NextResponse.json(
|
||||
{ error: "Tournament not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
console.log(`[Schedule API] Found tournament ${tournamentId} with ${tournament.participants.length} participants and ${tournament.rounds.length} existing rounds`);
|
||||
|
||||
// Check if schedule already exists and delete it
|
||||
if (tournament.rounds.length > 0) {
|
||||
console.log(`[Schedule API] Deleting ${tournament.rounds.length} existing rounds`);
|
||||
// Delete existing rounds and matchups before regenerating
|
||||
await prisma.bracketMatchup.deleteMany({
|
||||
where: { eventId: tournamentId },
|
||||
@@ -135,6 +145,8 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
currentElo: p.player.currentElo,
|
||||
}));
|
||||
|
||||
console.log(`[Schedule API] Got ${participants.length} participants`);
|
||||
|
||||
// Check minimum participants
|
||||
if (participants.length < 2) {
|
||||
return NextResponse.json(
|
||||
@@ -147,20 +159,24 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
const teamDurability = tournament.teamDurability || "permanent";
|
||||
const partnerRotation = (tournament.partnerRotation || "none") as 'none' | 'minimize_repeat' | 'maximize_even' | 'elo_based';
|
||||
const allowByes = tournament.allowByes ?? true;
|
||||
console.log(`[Schedule API] Team durability: ${teamDurability}, partner rotation: ${partnerRotation}, allow byes: ${allowByes}`);
|
||||
|
||||
// Determine number of teams from participants
|
||||
const tempResult = generateTeams(participants, partnerRotation, allowByes);
|
||||
const teamCount = tempResult.teams.length;
|
||||
|
||||
console.log(`[Schedule API] Generated ${teamCount} teams from ${participants.length} participants`);
|
||||
|
||||
if (teamCount < 2) {
|
||||
console.log(`[Schedule API] Not enough teams: ${teamCount}`);
|
||||
return NextResponse.json(
|
||||
{ error: "At least 2 teams (4 players) are required to generate a schedule" },
|
||||
{ error: "At least 2 teams are required to generate a schedule" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Calculate number of rounds needed
|
||||
// Calculate expected rounds
|
||||
const numRounds = expectedRounds(teamCount);
|
||||
console.log(`[Schedule API] Expected rounds: ${numRounds}`);
|
||||
|
||||
if (teamDurability === "permanent") {
|
||||
// ============================================
|
||||
@@ -186,11 +202,14 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
|
||||
// Generate schedule using fixed teams
|
||||
const schedule = generateRoundRobin(teamPairings);
|
||||
console.log(`[Schedule API] Generated ${schedule.length} rounds for ${teamCount} teams (fixed)`);
|
||||
|
||||
// Create rounds and matchups in a transaction
|
||||
console.log(`[Schedule API] About to create ${schedule.length} rounds in transaction`);
|
||||
const created = await prisma.$transaction(
|
||||
schedule.map((round) =>
|
||||
prisma.tournamentRound.create({
|
||||
schedule.map((round) => {
|
||||
console.log(`[Schedule API] Creating round ${round.roundNumber} with ${round.matchups.length} matchups`);
|
||||
return prisma.tournamentRound.create({
|
||||
data: {
|
||||
eventId: tournamentId,
|
||||
roundNumber: round.roundNumber,
|
||||
@@ -218,8 +237,17 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
|
||||
console.log(`[Schedule API] Transaction complete. Created ${created.length} rounds`);
|
||||
console.log(`[Schedule API] Verifying in database:`);
|
||||
for (const round of created) {
|
||||
console.log(`[Schedule API] Round ${round.roundNumber}: id=${round.id}, eventId=${round.eventId}`);
|
||||
}
|
||||
|
||||
revalidatePath(`/admin/tournaments/${tournamentId}/schedule`);
|
||||
console.log(`[Schedule API] revalidatePath called for /admin/tournaments/${tournamentId}/schedule`);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
@@ -295,6 +323,8 @@ export async function POST(_request: Request, { params }: RouteParams) {
|
||||
)
|
||||
);
|
||||
|
||||
revalidatePath(`/admin/tournaments/${tournamentId}/schedule`);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
roundsCreated: created.length,
|
||||
|
||||
Reference in New Issue
Block a user