W n8n
This commit is contained in:
parent
25157439c3
commit
3cb22a643f
@ -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,69 +234,89 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create mission directly (n8n request)
|
// Create mission directly (n8n request)
|
||||||
const mission = await prisma.mission.create({
|
try {
|
||||||
data: {
|
const mission = await prisma.mission.create({
|
||||||
name: body.name,
|
data: {
|
||||||
oddScope: body.oddScope || ['default'],
|
name: body.name,
|
||||||
niveau: body.niveau,
|
oddScope: body.oddScope || ['default'],
|
||||||
intention: body.intention,
|
niveau: body.niveau,
|
||||||
missionType: body.missionType,
|
intention: body.intention,
|
||||||
donneurDOrdre: body.donneurDOrdre,
|
missionType: body.missionType,
|
||||||
projection: body.projection,
|
donneurDOrdre: body.donneurDOrdre,
|
||||||
services: Array.isArray(body.services) ? body.services.filter(Boolean) : [],
|
projection: body.projection,
|
||||||
profils: Array.isArray(body.profils) ? body.profils.filter(Boolean) : [],
|
services: Array.isArray(body.services) ? body.services.filter(Boolean) : [],
|
||||||
participation: body.participation || 'default',
|
profils: Array.isArray(body.profils) ? body.profils.filter(Boolean) : [],
|
||||||
creatorId: userId,
|
participation: body.participation || 'default',
|
||||||
logo: body.logo || null,
|
creatorId: userId,
|
||||||
leantimeProjectId: body.leantimeProjectId || null,
|
logo: body.logo || null,
|
||||||
outlineCollectionId: body.documentationCollectionId || null,
|
leantimeProjectId: body.leantimeProjectId || null,
|
||||||
rocketChatChannelId: body.rocketchatChannelId || null,
|
outlineCollectionId: body.documentationCollectionId || null,
|
||||||
giteaRepositoryUrl: body.gitRepoUrl || null,
|
rocketChatChannelId: body.rocketchatChannelId || null,
|
||||||
penpotProjectId: body.penpotProjectId || null
|
giteaRepositoryUrl: body.gitRepoUrl || null,
|
||||||
} as Prisma.MissionUncheckedCreateInput
|
penpotProjectId: body.penpotProjectId || null
|
||||||
});
|
} as Prisma.MissionUncheckedCreateInput
|
||||||
|
});
|
||||||
|
|
||||||
// Add guardians and volunteers
|
// Add guardians and volunteers
|
||||||
if (body.guardians || body.volunteers) {
|
if (body.guardians || body.volunteers) {
|
||||||
const missionUsers: MissionUserInput[] = [];
|
const missionUsers: MissionUserInput[] = [];
|
||||||
|
|
||||||
// Add guardians
|
// Add guardians
|
||||||
if (body.guardians) {
|
if (body.guardians) {
|
||||||
Object.entries(body.guardians).forEach(([role, userId]) => {
|
Object.entries(body.guardians).forEach(([role, userId]) => {
|
||||||
if (userId) {
|
if (userId) {
|
||||||
missionUsers.push({
|
missionUsers.push({
|
||||||
role,
|
role,
|
||||||
userId: userId as string,
|
userId: userId as string,
|
||||||
missionId: mission.id
|
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
|
return NextResponse.json({
|
||||||
if (body.volunteers && Array.isArray(body.volunteers)) {
|
message: 'Mission created successfully',
|
||||||
body.volunteers.forEach(userId => {
|
mission
|
||||||
if (userId) {
|
});
|
||||||
missionUsers.push({
|
} catch (error) {
|
||||||
role: 'volontaire',
|
console.error('Error creating mission:', error);
|
||||||
userId,
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
missionId: mission.id
|
if (error.code === 'P2003') {
|
||||||
});
|
return NextResponse.json({
|
||||||
}
|
error: 'Invalid reference',
|
||||||
});
|
details: 'One or more referenced users do not exist',
|
||||||
}
|
code: 'INVALID_REFERENCE'
|
||||||
|
}, { status: 400 });
|
||||||
if (missionUsers.length > 0) {
|
}
|
||||||
await prisma.missionUser.createMany({
|
|
||||||
data: missionUsers
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
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) {
|
} catch (error) {
|
||||||
console.error('Error creating mission:', error);
|
console.error('Error creating mission:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user