W n8n
This commit is contained in:
parent
144178f760
commit
6877062826
@ -184,126 +184,111 @@ export async function POST(request: Request) {
|
||||
return NextResponse.json({ error: 'A mission with this name already exists' }, { status: 400 });
|
||||
}
|
||||
|
||||
console.log('Creating mission in database...');
|
||||
// Create the mission
|
||||
const mission = await prisma.mission.create({
|
||||
data: {
|
||||
name,
|
||||
oddScope: oddScope || 'default',
|
||||
niveau,
|
||||
intention,
|
||||
missionType,
|
||||
donneurDOrdre,
|
||||
projection,
|
||||
services: Array.isArray(services) ? services.filter(Boolean) : [],
|
||||
profils: Array.isArray(profils) ? profils.filter(Boolean) : [],
|
||||
participation: participation || 'default',
|
||||
creatorId: userId
|
||||
}
|
||||
// Trigger n8n workflow first
|
||||
const n8nService = new N8nService();
|
||||
const n8nData = {
|
||||
...body,
|
||||
creatorId: userId
|
||||
};
|
||||
console.log('Sending data to n8n service:', {
|
||||
name: n8nData.name,
|
||||
creatorId: n8nData.creatorId,
|
||||
oddScope: n8nData.oddScope,
|
||||
niveau: n8nData.niveau,
|
||||
intention: n8nData.intention,
|
||||
missionType: n8nData.missionType,
|
||||
donneurDOrdre: n8nData.donneurDOrdre,
|
||||
projection: n8nData.projection,
|
||||
services: n8nData.services,
|
||||
participation: n8nData.participation,
|
||||
profils: n8nData.profils,
|
||||
fullData: JSON.stringify(n8nData, null, 2)
|
||||
});
|
||||
|
||||
console.log('Created mission:', JSON.stringify(mission, null, 2));
|
||||
|
||||
// Add guardians and volunteers
|
||||
if (guardians || volunteers) {
|
||||
console.log('Adding guardians and volunteers...');
|
||||
const missionUsers: MissionUserInput[] = [];
|
||||
|
||||
// Add guardians
|
||||
if (guardians) {
|
||||
Object.entries(guardians).forEach(([role, userId]) => {
|
||||
if (userId) {
|
||||
missionUsers.push({
|
||||
role,
|
||||
userId: userId as string,
|
||||
missionId: mission.id
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Add volunteers
|
||||
if (volunteers && Array.isArray(volunteers)) {
|
||||
volunteers.forEach(userId => {
|
||||
if (userId) {
|
||||
missionUsers.push({
|
||||
role: 'volontaire',
|
||||
userId,
|
||||
missionId: mission.id
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (missionUsers.length > 0) {
|
||||
console.log('Creating mission users:', JSON.stringify(missionUsers, null, 2));
|
||||
await prisma.missionUser.createMany({
|
||||
data: missionUsers
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger n8n workflow
|
||||
const n8nService = new N8nService();
|
||||
console.log('About to trigger n8n workflow with data:', JSON.stringify({
|
||||
missionId: mission.id,
|
||||
name: mission.name,
|
||||
creatorId: mission.creatorId,
|
||||
fullData: body
|
||||
}, null, 2));
|
||||
|
||||
try {
|
||||
const workflowResult = await n8nService.triggerMissionCreation({
|
||||
missionId: mission.id,
|
||||
name: mission.name,
|
||||
creatorId: mission.creatorId,
|
||||
oddScope: mission.oddScope,
|
||||
niveau: mission.niveau,
|
||||
intention: mission.intention,
|
||||
missionType: mission.missionType,
|
||||
donneurDOrdre: mission.donneurDOrdre,
|
||||
projection: mission.projection,
|
||||
services: mission.services,
|
||||
participation: mission.participation,
|
||||
profils: mission.profils,
|
||||
config: {
|
||||
N8N_API_KEY: process.env.N8N_API_KEY,
|
||||
MISSION_API_URL: process.env.NEXT_PUBLIC_API_URL || 'https://api.slm-lab.net/api'
|
||||
}
|
||||
});
|
||||
|
||||
const workflowResult = await n8nService.triggerMissionCreation(n8nData);
|
||||
console.log('Received workflow result:', JSON.stringify(workflowResult, null, 2));
|
||||
|
||||
if (!workflowResult.success) {
|
||||
console.error('N8n workflow failed:', workflowResult.error);
|
||||
// Continue with mission creation even if n8n workflow fails
|
||||
return NextResponse.json(mission);
|
||||
return NextResponse.json({ error: 'Failed to create mission resources' }, { status: 500 });
|
||||
}
|
||||
|
||||
// Process workflow results
|
||||
const results = workflowResult.results || {};
|
||||
console.log('Processing workflow results:', JSON.stringify(results, null, 2));
|
||||
|
||||
// Update mission with integration data
|
||||
const integrationData = {
|
||||
leantimeProjectId: results.leantimeProjectId?.toString(),
|
||||
outlineCollectionId: results.outlineCollectionId?.toString(),
|
||||
rocketChatChannelId: results.rocketChatChannelId?.toString(),
|
||||
giteaRepositoryUrl: results.giteaRepositoryUrl?.toString()
|
||||
} as Prisma.MissionUpdateInput;
|
||||
|
||||
console.log('Updating mission with integration data:', JSON.stringify(integrationData, null, 2));
|
||||
|
||||
const updatedMission = await prisma.mission.update({
|
||||
where: { id: mission.id },
|
||||
data: integrationData
|
||||
// Now create the mission with the logo URL from n8n
|
||||
console.log('Creating mission in database...');
|
||||
const mission = await prisma.mission.create({
|
||||
data: {
|
||||
name,
|
||||
oddScope: oddScope || 'default',
|
||||
niveau,
|
||||
intention,
|
||||
missionType,
|
||||
donneurDOrdre,
|
||||
projection,
|
||||
services: Array.isArray(services) ? services.filter(Boolean) : [],
|
||||
profils: Array.isArray(profils) ? profils.filter(Boolean) : [],
|
||||
participation: participation || 'default',
|
||||
creatorId: userId,
|
||||
logo: results.logoUrl, // Use the logo URL from n8n
|
||||
leantimeProjectId: results.leantimeProjectId?.toString(),
|
||||
outlineCollectionId: results.outlineCollectionId?.toString(),
|
||||
rocketChatChannelId: results.rocketChatChannelId?.toString(),
|
||||
giteaRepositoryUrl: results.giteaRepositoryUrl?.toString()
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json(updatedMission);
|
||||
console.log('Created mission:', JSON.stringify(mission, null, 2));
|
||||
|
||||
// Add guardians and volunteers
|
||||
if (guardians || volunteers) {
|
||||
console.log('Adding guardians and volunteers...');
|
||||
const missionUsers: MissionUserInput[] = [];
|
||||
|
||||
// Add guardians
|
||||
if (guardians) {
|
||||
Object.entries(guardians).forEach(([role, userId]) => {
|
||||
if (userId) {
|
||||
missionUsers.push({
|
||||
role,
|
||||
userId: userId as string,
|
||||
missionId: mission.id
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Add volunteers
|
||||
if (volunteers && Array.isArray(volunteers)) {
|
||||
volunteers.forEach(userId => {
|
||||
if (userId) {
|
||||
missionUsers.push({
|
||||
role: 'volontaire',
|
||||
userId,
|
||||
missionId: mission.id
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (missionUsers.length > 0) {
|
||||
console.log('Creating mission users:', JSON.stringify(missionUsers, null, 2));
|
||||
await prisma.missionUser.createMany({
|
||||
data: missionUsers
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(mission);
|
||||
} catch (error) {
|
||||
console.error('Error in n8n workflow:', error);
|
||||
// Return the mission even if n8n workflow fails
|
||||
return NextResponse.json(mission);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create mission resources', details: error instanceof Error ? error.message : String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating mission:', error);
|
||||
|
||||
@ -18,6 +18,22 @@ export class N8nService {
|
||||
|
||||
async triggerMissionCreation(data: any): Promise<any> {
|
||||
try {
|
||||
// Log the incoming data
|
||||
console.log('Incoming data to n8n service:', {
|
||||
hasMissionId: !!data.missionId,
|
||||
hasName: !!data.name,
|
||||
hasCreatorId: !!data.creatorId,
|
||||
oddScope: data.oddScope,
|
||||
niveau: data.niveau,
|
||||
intention: data.intention,
|
||||
missionType: data.missionType,
|
||||
donneurDOrdre: data.donneurDOrdre,
|
||||
projection: data.projection,
|
||||
services: data.services,
|
||||
participation: data.participation,
|
||||
profils: data.profils
|
||||
});
|
||||
|
||||
// Add API key and default values to the data
|
||||
const dataWithDefaults = {
|
||||
...data,
|
||||
@ -38,10 +54,10 @@ export class N8nService {
|
||||
hasConfig: !!dataWithDefaults.config,
|
||||
configKeys: dataWithDefaults.config ? Object.keys(dataWithDefaults.config) : [],
|
||||
apiKeyPresent: !!dataWithDefaults.config?.N8N_API_KEY,
|
||||
apiUrlPresent: !!dataWithDefaults.config?.MISSION_API_URL
|
||||
apiUrlPresent: !!dataWithDefaults.config?.MISSION_API_URL,
|
||||
fullData: JSON.stringify(dataWithDefaults, null, 2)
|
||||
});
|
||||
|
||||
console.log('Triggering n8n workflow with data:', JSON.stringify(dataWithDefaults, null, 2));
|
||||
console.log('Using webhook URL:', this.webhookUrl);
|
||||
console.log('API key present:', !!this.apiKey);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user