diff --git a/app/api/missions/route.ts b/app/api/missions/route.ts index 8fdde8ca..cb9e211b 100644 --- a/app/api/missions/route.ts +++ b/app/api/missions/route.ts @@ -140,6 +140,19 @@ export async function POST(request: Request) { }, { status: 400 }); } + // Verify that the creator exists + const creator = await prisma.user.findUnique({ + where: { id: userId } + }); + + if (!creator) { + return NextResponse.json({ + error: 'Invalid creator ID', + details: 'The specified creator does not exist', + code: 'INVALID_CREATOR' + }, { status: 400 }); + } + // Check if mission with same name exists const existingMission = await prisma.mission.findFirst({ where: { name: body.name } @@ -208,10 +221,11 @@ export async function POST(request: Request) { return NextResponse.json(workflowResult); } catch (error) { + console.error('Error triggering n8n workflow:', error); return NextResponse.json( { error: 'Failed to create mission resources', - details: 'The mission creation process failed. Please try again later.', + details: error instanceof Error ? error.message : 'The mission creation process failed. Please try again later.', code: 'WORKFLOW_ERROR' }, { status: 500 } @@ -220,69 +234,89 @@ export async function POST(request: Request) { } // Create mission directly (n8n request) - const mission = await prisma.mission.create({ - data: { - name: body.name, - oddScope: body.oddScope || ['default'], - niveau: body.niveau, - intention: body.intention, - missionType: body.missionType, - donneurDOrdre: body.donneurDOrdre, - projection: body.projection, - services: Array.isArray(body.services) ? body.services.filter(Boolean) : [], - profils: Array.isArray(body.profils) ? body.profils.filter(Boolean) : [], - participation: body.participation || 'default', - creatorId: userId, - logo: body.logo || null, - leantimeProjectId: body.leantimeProjectId || null, - outlineCollectionId: body.documentationCollectionId || null, - rocketChatChannelId: body.rocketchatChannelId || null, - giteaRepositoryUrl: body.gitRepoUrl || null, - penpotProjectId: body.penpotProjectId || null - } as Prisma.MissionUncheckedCreateInput - }); + try { + const mission = await prisma.mission.create({ + data: { + name: body.name, + oddScope: body.oddScope || ['default'], + niveau: body.niveau, + intention: body.intention, + missionType: body.missionType, + donneurDOrdre: body.donneurDOrdre, + projection: body.projection, + services: Array.isArray(body.services) ? body.services.filter(Boolean) : [], + profils: Array.isArray(body.profils) ? body.profils.filter(Boolean) : [], + participation: body.participation || 'default', + creatorId: userId, + logo: body.logo || null, + leantimeProjectId: body.leantimeProjectId || null, + outlineCollectionId: body.documentationCollectionId || null, + rocketChatChannelId: body.rocketchatChannelId || null, + giteaRepositoryUrl: body.gitRepoUrl || null, + penpotProjectId: body.penpotProjectId || null + } as Prisma.MissionUncheckedCreateInput + }); - // Add guardians and volunteers - if (body.guardians || body.volunteers) { - const missionUsers: MissionUserInput[] = []; + // Add guardians and volunteers + if (body.guardians || body.volunteers) { + const missionUsers: MissionUserInput[] = []; - // Add guardians - if (body.guardians) { - Object.entries(body.guardians).forEach(([role, userId]) => { - if (userId) { - missionUsers.push({ - role, - userId: userId as string, - missionId: mission.id - }); - } - }); + // Add guardians + if (body.guardians) { + Object.entries(body.guardians).forEach(([role, userId]) => { + if (userId) { + missionUsers.push({ + role, + userId: userId as string, + missionId: mission.id + }); + } + }); + } + + // Add volunteers + if (body.volunteers && Array.isArray(body.volunteers)) { + body.volunteers.forEach(userId => { + if (userId) { + missionUsers.push({ + role: 'volontaire', + userId, + missionId: mission.id + }); + } + }); + } + + if (missionUsers.length > 0) { + await prisma.missionUser.createMany({ + data: missionUsers + }); + } } - // Add volunteers - if (body.volunteers && Array.isArray(body.volunteers)) { - body.volunteers.forEach(userId => { - if (userId) { - missionUsers.push({ - role: 'volontaire', - userId, - missionId: mission.id - }); - } - }); - } - - if (missionUsers.length > 0) { - await prisma.missionUser.createMany({ - data: missionUsers - }); + return NextResponse.json({ + message: 'Mission created successfully', + mission + }); + } catch (error) { + console.error('Error creating mission:', error); + if (error instanceof Prisma.PrismaClientKnownRequestError) { + if (error.code === 'P2003') { + return NextResponse.json({ + error: 'Invalid reference', + details: 'One or more referenced users do not exist', + code: 'INVALID_REFERENCE' + }, { status: 400 }); + } } + return NextResponse.json( + { + error: 'Failed to create mission', + details: error instanceof Error ? error.message : String(error) + }, + { status: 500 } + ); } - - return NextResponse.json({ - message: 'Mission created successfully', - mission - }); } catch (error) { console.error('Error creating mission:', error); return NextResponse.json(