diff --git a/app/api/missions/route.ts b/app/api/missions/route.ts index 1be5c9b7..cfc22b55 100644 --- a/app/api/missions/route.ts +++ b/app/api/missions/route.ts @@ -144,15 +144,26 @@ export async function GET(request: Request) { // POST endpoint to create a new mission export async function POST(request: Request) { try { - // Parse the request body first const body = await request.json(); - - // Pass the body to checkAuth - const { authorized, userId } = await checkAuth(request, body); - if (!authorized || !userId) { + const auth = await checkAuth(request, body); + if (!auth.authorized || !auth.userId) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } + // Check for existing mission with the same name + const existingMission = await prisma.mission.findFirst({ + where: { + name: body.name + } + }); + + if (existingMission) { + return NextResponse.json({ + error: 'A mission with this name already exists', + existingMission + }, { status: 409 }); + } + // Validate required fields if (!body.name || !body.niveau || !body.intention || !body.missionType || !body.donneurDOrdre || !body.projection) { return NextResponse.json({ @@ -176,21 +187,19 @@ export async function POST(request: Request) { }, { status: 400 }); } - // Create the mission in the database first + // Create mission with default values const mission = await prisma.mission.create({ data: { name: body.name, - logo: body.logo, oddScope: body.oddScope || [], - niveau: body.niveau, - intention: body.intention, - missionType: body.missionType, - donneurDOrdre: body.donneurDOrdre, - projection: body.projection, + niveau: body.niveau || [], + intention: body.intention || [], + missionType: body.missionType || 'hybrid', + donneurDOrdre: body.donneurDOrdre || [], + projection: body.projection || [], services: body.services || [], - participation: body.participation, profils: body.profils || [], - creatorId: userId + creatorId: auth.userId } }); @@ -231,7 +240,7 @@ export async function POST(request: Request) { const workflowResult = await n8nService.createMission({ ...body, missionId: mission.id, - creatorId: userId + creatorId: auth.userId }); // Update mission with integration results, even if some failed