diff --git a/src/app/api/matches/upload/route.ts b/src/app/api/matches/upload/route.ts index a44cf75..fb066bd 100644 --- a/src/app/api/matches/upload/route.ts +++ b/src/app/api/matches/upload/route.ts @@ -194,17 +194,20 @@ export async function POST(request: Request) { } async function findOrCreatePlayer(name: string) { - // Try to find existing player with exact name match + // Normalize the name for deduplication: trim whitespace and convert to lowercase + const normalizedName = name.trim().toLowerCase(); + + // Try to find existing player with matching normalized name let player = await prisma.player.findFirst({ - where: { name }, + where: { normalizedName }, }); if (!player) { - // Create a new player with a unique name - const uniqueName = `${name}-${Date.now()}-${Math.random().toString(36).substring(2, 8)}`; + // Create a new player with the original name and normalized name player = await prisma.player.create({ data: { - name: uniqueName, + name: name.trim(), + normalizedName, rating: 1000, currentElo: 1000, },