bb6be245b7
## Summary This PR implements the tournament schedule tab functionality and fixes all remaining E2E test failures. ### Changes Included 1. **Tournament Schedule Feature** - Added tournament schedule page at `/admin/tournaments/[id]/schedule` - Implemented "Generate Schedule" button functionality - Added schedule generation logic for round-robin tournaments 2. **E2E Test Fixes** - Fixed database connection issues in production builds - Improved test reliability with better error handling and debugging - Updated test infrastructure to use environment variables instead of hardcoded values 3. **CI/CD Updates** - Added E2E test job to PR workflow - Configured tests to run against development database - Moved database password to Gitea secrets 4. **Code Quality** - Removed hardcoded passwords from codebase - Improved Prisma client configuration - Enhanced authentication and navigation components ### Test Results All 16 E2E test scenarios are now passing: - Authentication tests: ✅ - Registration tests: ✅ - Tournament schedule tests: ✅ - Player schedule tests: ✅ - Admin navigation tests: ✅ ### Database Configuration - Tests run against `euchre_camp_dev` database - Production builds use environment variables for database configuration - Database password stored in Gitea secrets as `DB_PASSWORD` ### CI Pipeline The PR workflow now includes: 1. Unit tests 2. E2E tests (using production build) 3. Version bump analysis E2E tests must pass before PR can be merged. Reviewed-on: #27 Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
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 }
|
|
);
|
|
}
|
|
}
|