feat: add delete player functionality to admin panel

This commit is contained in:
2026-03-31 16:16:10 -07:00
parent 81f023317e
commit 222fb0bdc3
2 changed files with 130 additions and 9 deletions
+83
View File
@@ -2,6 +2,89 @@ import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { hasRole } from "@/lib/permissions";
/**
* DELETE /api/admin/players/[id]
*
* Delete a player
* Requires: club_admin or site_admin role
* Note: Player can only be deleted if they have no matches
*/
export async function DELETE(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const playerId = parseInt(id);
// Validate player ID
if (isNaN(playerId)) {
return NextResponse.json(
{ error: "Invalid player ID" },
{ status: 400 }
);
}
// Check permissions - only club_admin and site_admin can delete players
const permission = await hasRole('club_admin');
if (!permission.allowed) {
return NextResponse.json(
{ error: permission.reason || "Not authorized to delete players" },
{ status: 403 }
);
}
// Verify player exists
const existingPlayer = await prisma.player.findUnique({
where: { id: playerId },
include: {
matchesAsP1: true,
matchesAsP2: true,
matchesAsP3: true,
matchesAsP4: true,
},
});
if (!existingPlayer) {
return NextResponse.json(
{ error: "Player not found" },
{ status: 404 }
);
}
// Check if player has any matches
const hasMatches =
existingPlayer.matchesAsP1.length > 0 ||
existingPlayer.matchesAsP2.length > 0 ||
existingPlayer.matchesAsP3.length > 0 ||
existingPlayer.matchesAsP4.length > 0;
if (hasMatches) {
return NextResponse.json(
{ error: "Cannot delete player with match history" },
{ status: 400 }
);
}
// Delete the player (this will also delete associated ratings via cascade)
await prisma.player.delete({
where: { id: playerId },
});
return NextResponse.json({
success: true,
message: "Player deleted successfully",
});
} catch (error: unknown) {
console.error("Error deleting player:", error);
const message = error instanceof Error ? error.message : "Failed to delete player";
return NextResponse.json(
{ error: message },
{ status: 500 }
);
}
}
/**
* PATCH /api/admin/players/[id]
*