This commit is contained in:
alma 2025-05-23 10:29:02 +02:00
parent 25157439c3
commit 3cb22a643f

View File

@ -140,6 +140,19 @@ export async function POST(request: Request) {
}, { status: 400 }); }, { 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 // Check if mission with same name exists
const existingMission = await prisma.mission.findFirst({ const existingMission = await prisma.mission.findFirst({
where: { name: body.name } where: { name: body.name }
@ -208,10 +221,11 @@ export async function POST(request: Request) {
return NextResponse.json(workflowResult); return NextResponse.json(workflowResult);
} catch (error) { } catch (error) {
console.error('Error triggering n8n workflow:', error);
return NextResponse.json( return NextResponse.json(
{ {
error: 'Failed to create mission resources', 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' code: 'WORKFLOW_ERROR'
}, },
{ status: 500 } { status: 500 }
@ -220,6 +234,7 @@ export async function POST(request: Request) {
} }
// Create mission directly (n8n request) // Create mission directly (n8n request)
try {
const mission = await prisma.mission.create({ const mission = await prisma.mission.create({
data: { data: {
name: body.name, name: body.name,
@ -283,6 +298,25 @@ export async function POST(request: Request) {
message: 'Mission created successfully', message: 'Mission created successfully',
mission 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 }
);
}
} catch (error) { } catch (error) {
console.error('Error creating mission:', error); console.error('Error creating mission:', error);
return NextResponse.json( return NextResponse.json(