From 503afb99f99a5a114ab7be6951d0bf6e8f7e5d8d Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 24 May 2025 18:15:14 +0200 Subject: [PATCH] W n8n attention vm --- app/api/missions/route.ts | 44 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/app/api/missions/route.ts b/app/api/missions/route.ts index e32340a3..17980a51 100644 --- a/app/api/missions/route.ts +++ b/app/api/missions/route.ts @@ -187,12 +187,14 @@ export async function GET(request: Request) { // POST endpoint to create a new mission export async function POST(request: Request) { try { + console.log('=== Mission Creation Started ==='); const { authorized, userId } = await checkAuth(request); if (!authorized || !userId) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const body = await request.json(); + console.log('Received request body:', JSON.stringify(body, null, 2)); // Simple validation if (!body.name || !body.oddScope) { @@ -204,9 +206,11 @@ export async function POST(request: Request) { // Check if this is a request from n8n const isN8nRequest = request.headers.get('x-api-key') === process.env.N8N_API_KEY; + console.log('Is N8N request:', isN8nRequest); if (!isN8nRequest) { try { + console.log('=== Starting N8N Workflow ==='); const n8nService = new N8nService(); // Prepare data for n8n @@ -218,11 +222,14 @@ export async function POST(request: Request) { MISSION_API_URL: process.env.NEXT_PUBLIC_API_URL } }; + console.log('Sending to N8N:', JSON.stringify(n8nData, null, 2)); // Trigger n8n workflow first const workflowResult = await n8nService.triggerMissionCreation(n8nData); + console.log('N8N 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', details: workflowResult.error @@ -231,24 +238,20 @@ export async function POST(request: Request) { // Only create mission in database after n8n succeeds try { - // Log the entire workflow result - console.log('N8N Workflow Result:', JSON.stringify(workflowResult, null, 2)); - // Extract logo path from workflow result let logoPath = null; - if (workflowResult.data && Array.isArray(workflowResult.data)) { - const firstResult = workflowResult.data[0]; - if (firstResult && firstResult.logoPath) { - logoPath = firstResult.logoPath; + console.log('Full workflow result:', JSON.stringify(workflowResult, null, 2)); + + if (workflowResult.results && Array.isArray(workflowResult.results)) { + const firstResult = workflowResult.results[0]; + console.log('First result from workflow:', JSON.stringify(firstResult, null, 2)); + + if (firstResult && firstResult.json && firstResult.json.logoPath) { + logoPath = firstResult.json.logoPath; + console.log('Extracted logo path:', logoPath); } } - console.log('Creating mission with data:', { - name: body.name, - logoPath, - workflowResult: workflowResult - }); - const missionData = { name: body.name, oddScope: body.oddScope, @@ -264,17 +267,13 @@ export async function POST(request: Request) { logo: logoPath ? `missions/${logoPath}` : null }; - console.log('Mission data to be saved:', missionData); + console.log('Creating mission with data:', JSON.stringify(missionData, null, 2)); const mission = await prisma.mission.create({ data: missionData }); - console.log('Mission created with logo:', { - missionId: mission.id, - logo: mission.logo, - fullMission: mission - }); + console.log('Mission created successfully:', JSON.stringify(mission, null, 2)); return NextResponse.json({ success: true, @@ -296,6 +295,8 @@ export async function POST(request: Request) { }, { status: 500 }); } } else { + console.log('=== Handling N8N Callback ==='); + console.log('N8N callback body:', JSON.stringify(body, null, 2)); // Handle n8n callback - update mission with integration IDs try { const mission = await prisma.mission.update({ @@ -305,10 +306,13 @@ export async function POST(request: Request) { outlineCollectionId: body.outlineCollectionId || null, rocketChatChannelId: body.rocketChatChannelId || null, giteaRepositoryUrl: body.giteaRepositoryUrl || null, - penpotProjectId: body.penpotProjectId || null + penpotProjectId: body.penpotProjectId || null, + logo: body.logoPath ? `missions/${body.logoPath}` : null // Add logo path update here } as Prisma.MissionUpdateInput }); + console.log('Mission updated with integrations:', JSON.stringify(mission, null, 2)); + return NextResponse.json({ success: true, mission,