feat: add player name editing functionality in admin UI and fix site_admin access to admin dashboard

This commit is contained in:
2026-03-30 21:58:30 -07:00
parent 9201b2c437
commit db782ff504
4 changed files with 264 additions and 29 deletions
+90
View File
@@ -0,0 +1,90 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { hasRole } from "@/lib/permissions";
/**
* PATCH /api/admin/players/[id]
*
* Update a player's name
* Requires: club_admin or site_admin role
*/
export async function PATCH(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const playerId = parseInt(params.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 edit players
const permission = await hasRole('club_admin');
if (!permission.allowed) {
return NextResponse.json(
{ error: permission.reason || "Not authorized to edit players" },
{ status: 403 }
);
}
// Verify player exists
const existingPlayer = await prisma.player.findUnique({
where: { id: playerId },
include: { user: true },
});
if (!existingPlayer) {
return NextResponse.json(
{ error: "Player not found" },
{ status: 404 }
);
}
const body = await request.json();
const { name } = body;
if (!name || typeof name !== 'string' || name.trim().length === 0) {
return NextResponse.json(
{ error: "Player name is required" },
{ status: 400 }
);
}
const trimmedName = name.trim();
const normalizedName = trimmedName.toLowerCase();
// Update the player
const updatedPlayer = await prisma.player.update({
where: { id: playerId },
data: {
name: trimmedName,
normalizedName,
},
});
// If the player has an associated user, update the user's name too
if (existingPlayer.user) {
await prisma.user.update({
where: { id: existingPlayer.user.id },
data: { name: trimmedName },
});
}
return NextResponse.json({
success: true,
player: updatedPlayer,
});
} catch (error: unknown) {
console.error("Error updating player:", error);
const message = error instanceof Error ? error.message : "Failed to update player";
return NextResponse.json(
{ error: message },
{ status: 500 }
);
}
}
+32
View File
@@ -0,0 +1,32 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
/**
* GET /api/players
*
* Get all players with their user associations
* This is a public endpoint (no authentication required)
*/
export async function GET() {
try {
const players = await prisma.player.findMany({
include: {
user: {
select: {
email: true,
},
},
},
orderBy: { name: "asc" },
});
return NextResponse.json(players);
} catch (error: unknown) {
console.error("Error fetching players:", error);
const message = error instanceof Error ? error.message : "Failed to fetch players";
return NextResponse.json(
{ error: message },
{ status: 500 }
);
}
}