W n8n
This commit is contained in:
parent
144178f760
commit
6877062826
@ -184,8 +184,42 @@ export async function POST(request: Request) {
|
|||||||
return NextResponse.json({ error: 'A mission with this name already exists' }, { status: 400 });
|
return NextResponse.json({ error: 'A mission with this name already exists' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
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);
|
||||||
|
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));
|
||||||
|
|
||||||
|
// Now create the mission with the logo URL from n8n
|
||||||
console.log('Creating mission in database...');
|
console.log('Creating mission in database...');
|
||||||
// Create the mission
|
|
||||||
const mission = await prisma.mission.create({
|
const mission = await prisma.mission.create({
|
||||||
data: {
|
data: {
|
||||||
name,
|
name,
|
||||||
@ -198,7 +232,12 @@ export async function POST(request: Request) {
|
|||||||
services: Array.isArray(services) ? services.filter(Boolean) : [],
|
services: Array.isArray(services) ? services.filter(Boolean) : [],
|
||||||
profils: Array.isArray(profils) ? profils.filter(Boolean) : [],
|
profils: Array.isArray(profils) ? profils.filter(Boolean) : [],
|
||||||
participation: participation || 'default',
|
participation: participation || 'default',
|
||||||
creatorId: userId
|
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()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -243,67 +282,13 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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(mission);
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json(updatedMission);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in n8n workflow:', error);
|
console.error('Error in n8n workflow:', error);
|
||||||
// Return the mission even if n8n workflow fails
|
return NextResponse.json(
|
||||||
return NextResponse.json(mission);
|
{ error: 'Failed to create mission resources', 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);
|
||||||
|
|||||||
@ -18,6 +18,22 @@ export class N8nService {
|
|||||||
|
|
||||||
async triggerMissionCreation(data: any): Promise<any> {
|
async triggerMissionCreation(data: any): Promise<any> {
|
||||||
try {
|
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
|
// Add API key and default values to the data
|
||||||
const dataWithDefaults = {
|
const dataWithDefaults = {
|
||||||
...data,
|
...data,
|
||||||
@ -38,10 +54,10 @@ export class N8nService {
|
|||||||
hasConfig: !!dataWithDefaults.config,
|
hasConfig: !!dataWithDefaults.config,
|
||||||
configKeys: dataWithDefaults.config ? Object.keys(dataWithDefaults.config) : [],
|
configKeys: dataWithDefaults.config ? Object.keys(dataWithDefaults.config) : [],
|
||||||
apiKeyPresent: !!dataWithDefaults.config?.N8N_API_KEY,
|
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('Using webhook URL:', this.webhookUrl);
|
||||||
console.log('API key present:', !!this.apiKey);
|
console.log('API key present:', !!this.apiKey);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user