diff --git a/app/api/curators/route.ts b/app/api/curators/route.ts index aff08ec..4450651 100644 --- a/app/api/curators/route.ts +++ b/app/api/curators/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from 'next/server'; -import { PrismaClient } from '@prisma/client'; +import { PrismaClient, Prisma } from '@prisma/client'; import bcrypt from 'bcryptjs'; import { requireAdminAuth } from '@/lib/auth'; @@ -69,6 +69,15 @@ export async function POST(request: NextRequest) { }); } catch (error) { console.error('Error creating curator:', error); + + // Handle unique username constraint violation explicitly + if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') { + return NextResponse.json( + { error: 'A curator with this username already exists.' }, + { status: 409 } + ); + } + return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); } } @@ -153,6 +162,15 @@ export async function PUT(request: NextRequest) { }); } catch (error) { console.error('Error updating curator:', error); + + // Handle unique username constraint violation explicitly for updates + if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') { + return NextResponse.json( + { error: 'A curator with this username already exists.' }, + { status: 409 } + ); + } + return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); } }