feat: add site_admin role, casual match support, and tournament deletion

This commit is contained in:
2026-03-30 21:49:43 -07:00
parent d821dd7ce2
commit 07804b5f8f
7 changed files with 507 additions and 7 deletions
+108
View File
@@ -0,0 +1,108 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { canDeleteTournament } from "@/lib/permissions";
/**
* DELETE /api/admin/tournaments/[id]
*
* Delete a tournament and optionally its associated matches
* Options:
* - deleteMatches: Delete all matches associated with the tournament
* - orphanMatches: Keep matches but remove tournament association (eventId becomes null)
*/
export async function DELETE(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const tournamentId = parseInt(params.id);
const body = await request.json();
const { deleteMatches = false } = body;
// Validate tournament ID
if (isNaN(tournamentId)) {
return NextResponse.json(
{ error: "Invalid tournament ID" },
{ status: 400 }
);
}
// Check if user has permission to delete this tournament
const permission = await canDeleteTournament(tournamentId);
if (!permission.allowed) {
return NextResponse.json(
{ error: permission.reason || "Not authorized to delete this tournament" },
{ status: 403 }
);
}
// Verify tournament exists
const tournament = await prisma.event.findUnique({
where: { id: tournamentId },
include: { matches: true },
});
if (!tournament) {
return NextResponse.json(
{ error: "Tournament not found" },
{ status: 404 }
);
}
const matchCount = tournament.matches.length;
// Handle matches based on user choice
if (deleteMatches) {
// Delete all matches associated with the tournament
await prisma.match.deleteMany({
where: { eventId: tournamentId },
});
} else {
// Orphan matches - remove tournament association
await prisma.match.updateMany({
where: { eventId: tournamentId },
data: { eventId: null },
});
}
// Delete event participants
await prisma.eventParticipant.deleteMany({
where: { eventId: tournamentId },
});
// Delete teams
await prisma.team.deleteMany({
where: { eventId: tournamentId },
});
// Delete tournament rounds
await prisma.tournamentRound.deleteMany({
where: { eventId: tournamentId },
});
// Delete bracket matchups
await prisma.bracketMatchup.deleteMany({
where: { eventId: tournamentId },
});
// Delete the tournament itself
await prisma.event.delete({
where: { id: tournamentId },
});
return NextResponse.json({
success: true,
message: "Tournament deleted successfully",
deletedTournament: tournament.name,
deletedMatches: deleteMatches ? matchCount : 0,
orphanedMatches: deleteMatches ? 0 : matchCount,
});
} catch (error: unknown) {
console.error("Error deleting tournament:", error);
const message = error instanceof Error ? error.message : "Failed to delete tournament";
return NextResponse.json(
{ error: message },
{ status: 500 }
);
}
}