From cdd4dc127f7840fd2d0637645cbd905455ff7936 Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 24 May 2025 08:42:07 +0200 Subject: [PATCH] W n8n route --- My_workflow_49.json | 2 +- app/api/missions/route.ts | 9 +++++++++ lib/services/n8n-service.ts | 10 +++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/My_workflow_49.json b/My_workflow_49.json index dfb148f9..8294cfdf 100644 --- a/My_workflow_49.json +++ b/My_workflow_49.json @@ -481,7 +481,7 @@ }, { "parameters": { - "functionCode": "// Combine results from all integrations with better error handling\ntry {\n // Defensively get results from each service node\n let gitRepoResult = {};\n let leantimeResult = {};\n let docCollectionResult = {};\n let rocketChatResult = {};\n let uploadResults = {};\n let keycloakToken = {};\n \n try { \n gitRepoResult = $node['Create Git Repository']?.json || {};\n console.log('Git repo node executed successfully'); \n } catch (e) { \n console.log('Git repo node not executed yet, continuing anyway'); \n }\n \n try { \n leantimeResult = $node['Create Leantime Project']?.json || {};\n console.log('Leantime node executed successfully');\n } catch (e) { \n console.log('Leantime node not executed yet, continuing anyway'); \n }\n \n try { \n docCollectionResult = $node['Create Documentation Collection']?.json || {};\n console.log('Documentation node executed successfully');\n } catch (e) { \n console.log('Documentation node not executed yet, continuing anyway'); \n }\n \n try { \n rocketChatResult = $node['Create RocketChat Channel']?.json || {};\n console.log('RocketChat node executed successfully');\n } catch (e) { \n console.log('RocketChat node not executed yet, continuing anyway'); \n }\n \n try { \n uploadResults = $node['Process Upload Results']?.json || {};\n console.log('Upload Results available');\n } catch (e) { \n console.log('Upload Results not available, continuing anyway'); \n }\n \n try { \n keycloakToken = $node['Process Token']?.json || {};\n console.log('Keycloak token available');\n } catch (e) { \n console.log('Keycloak token not available, continuing anyway'); \n }\n \n // Track which resources were actually created vs already existed\n const resourceStatus = {\n gitRepo: false,\n leantimeProject: false,\n docCollection: false,\n rocketChatChannel: false\n };\n \n // Process Git repository result\n if (gitRepoResult.error?.includes('already exists')) {\n console.log('Git repository already exists');\n gitRepoResult = { exists: true };\n } else if (gitRepoResult.body?.html_url) {\n resourceStatus.gitRepo = true;\n }\n \n // Process Leantime project result\n if (leantimeResult.error?.includes('already exists')) {\n console.log('Leantime project already exists');\n leantimeResult = { exists: true };\n } else if (leantimeResult.body?.result?.id) {\n resourceStatus.leantimeProject = true;\n }\n \n // Process Documentation collection result\n if (docCollectionResult.error?.includes('already exists')) {\n console.log('Documentation collection already exists');\n docCollectionResult = { exists: true };\n } else if (docCollectionResult.body?.id) {\n resourceStatus.docCollection = true;\n }\n \n // Process RocketChat channel result\n if (rocketChatResult.error?.includes('error-duplicate-channel-name')) {\n console.log('RocketChat channel already exists');\n rocketChatResult = { exists: true };\n } else if (rocketChatResult.body?.channel?._id) {\n resourceStatus.rocketChatChannel = true;\n }\n \n // Gather information about what executed\n const executedNodes = [];\n if (Object.keys(gitRepoResult).length > 0) executedNodes.push('Git');\n if (Object.keys(leantimeResult).length > 0) executedNodes.push('Leantime');\n if (Object.keys(docCollectionResult).length > 0) executedNodes.push('Documentation');\n if (Object.keys(rocketChatResult).length > 0) executedNodes.push('RocketChat');\n \n console.log(`Executed nodes (${executedNodes.length}): ${executedNodes.join(', ')}`);\n \n // Handle empty results with empty objects to prevent errors\n const results = {\n gitRepo: gitRepoResult.error ? { error: gitRepoResult.error.message || 'Git repository creation failed' } : (gitRepoResult.body || gitRepoResult || {}),\n leantimeProject: leantimeResult.error ? { error: leantimeResult.error.message || 'Leantime project creation failed' } : (leantimeResult.body || leantimeResult || {}),\n docCollection: docCollectionResult.error ? { error: docCollectionResult.error.message || 'Documentation collection creation failed' } : (docCollectionResult.body || docCollectionResult || {}),\n rocketChatChannel: rocketChatResult.error ? { error: rocketChatResult.error.message || 'RocketChat channel creation failed' } : (rocketChatResult.body || rocketChatResult || {}),\n uploadResults: uploadResults || {},\n keycloakToken: keycloakToken || {},\n executedNodes: executedNodes,\n resourceStatus: resourceStatus\n };\n \n // Log key details for debugging\n console.log('Git repo HTML URL:', results.gitRepo?.html_url || 'not available');\n console.log('Leantime project ID:', results.leantimeProject?.result?.id || 'not available');\n console.log('Documentation ID:', results.docCollection?.id || 'not available');\n console.log('RocketChat channel ID:', results.rocketChatChannel?.channel?._id || 'not available');\n \n return results;\n} catch (error) {\n console.error('Error in Combine Results:', error);\n // Return minimal object to allow workflow to continue\n return {\n error: `Error combining results: ${error.message}`,\n gitRepo: {},\n leantimeProject: {},\n docCollection: {},\n rocketChatChannel: {}\n };\n}" + "functionCode": "// Combine results from all integrations with better error handling\ntry {\n // Defensively get results from each service node\n let gitRepoResult = {};\n let leantimeResult = {};\n let docCollectionResult = {};\n let rocketChatResult = {};\n let uploadResults = {};\n let keycloakToken = {};\n \n try { \n gitRepoResult = $node['Create Git Repository']?.json || {};\n console.log('Git repo node executed successfully'); \n } catch (e) { \n console.log('Git repo node not executed yet, continuing anyway'); \n }\n \n try { \n leantimeResult = $node['Create Leantime Project']?.json || {};\n console.log('Leantime node executed successfully');\n } catch (e) { \n console.log('Leantime node not executed yet, continuing anyway'); \n }\n \n try { \n docCollectionResult = $node['Create Documentation Collection']?.json || {};\n console.log('Documentation node executed successfully');\n } catch (e) { \n console.log('Documentation node not executed yet, continuing anyway'); \n }\n \n try { \n rocketChatResult = $node['Create RocketChat Channel']?.json || {};\n console.log('RocketChat node executed successfully');\n } catch (e) { \n console.log('RocketChat node not executed yet, continuing anyway'); \n }\n \n try { \n uploadResults = $node['Process Upload Results']?.json || {};\n console.log('Upload Results available');\n } catch (e) { \n console.log('Upload Results not available, continuing anyway'); \n }\n \n try { \n keycloakToken = $node['Process Token']?.json || {};\n console.log('Keycloak token available');\n } catch (e) { \n console.log('Keycloak token not available, continuing anyway'); \n }\n \n // Track which resources were actually created vs already existed\n const resourceStatus = {\n gitRepo: false,\n leantimeProject: false,\n docCollection: false,\n rocketChatChannel: false\n };\n \n // Process Git repository result\n if (gitRepoResult.error?.includes('already exists')) {\n console.log('Git repository already exists');\n gitRepoResult = { exists: true };\n } else if (gitRepoResult.body?.body?.html_url) {\n resourceStatus.gitRepo = true;\n console.log('Git repository created successfully with URL:', gitRepoResult.body.body.html_url);\n }\n \n // Process Leantime project result - Updated to check for array result\n if (leantimeResult.error?.includes('already exists')) {\n console.log('Leantime project already exists');\n leantimeResult = { exists: true };\n } else if (leantimeResult.body?.result && Array.isArray(leantimeResult.body.result) && leantimeResult.body.result.length > 0) {\n resourceStatus.leantimeProject = true;\n console.log('Leantime project created successfully with ID:', leantimeResult.body.result[0]);\n }\n \n // Process Documentation collection result - Updated to check for data wrapper\n if (docCollectionResult.error?.includes('already exists')) {\n console.log('Documentation collection already exists');\n docCollectionResult = { exists: true };\n } else if (docCollectionResult.body?.data?.id) {\n resourceStatus.docCollection = true;\n console.log('Documentation collection created successfully with ID:', docCollectionResult.body.data.id);\n }\n \n // Process RocketChat channel result\n if (rocketChatResult.error?.includes('error-duplicate-channel-name')) {\n console.log('RocketChat channel already exists');\n rocketChatResult = { exists: true };\n } else if (rocketChatResult.body?.channel?._id) {\n resourceStatus.rocketChatChannel = true;\n }\n \n // Gather information about what executed\n const executedNodes = [];\n if (Object.keys(gitRepoResult).length > 0) executedNodes.push('Git');\n if (Object.keys(leantimeResult).length > 0) executedNodes.push('Leantime');\n if (Object.keys(docCollectionResult).length > 0) executedNodes.push('Documentation');\n if (Object.keys(rocketChatResult).length > 0) executedNodes.push('RocketChat');\n \n console.log(`Executed nodes (${executedNodes.length}): ${executedNodes.join(', ')}`);\n \n // Handle empty results with empty objects to prevent errors\n const results = {\n gitRepo: gitRepoResult.error ? { error: gitRepoResult.error.message || 'Git repository creation failed' } : (gitRepoResult.body?.body || gitRepoResult.body || gitRepoResult || {}),\n leantimeProject: leantimeResult.error ? { error: leantimeResult.error.message || 'Leantime project creation failed' } : (leantimeResult.body || leantimeResult || {}),\n docCollection: docCollectionResult.error ? { error: docCollectionResult.error.message || 'Documentation collection creation failed' } : (docCollectionResult.body || docCollectionResult || {}),\n rocketChatChannel: rocketChatResult.error ? { error: rocketChatResult.error.message || 'RocketChat channel creation failed' } : (rocketChatResult.body || rocketChatResult || {}),\n uploadResults: uploadResults || {},\n keycloakToken: keycloakToken || {},\n executedNodes: executedNodes,\n resourceStatus: resourceStatus\n };\n \n // Log key details for debugging\n console.log('Git repo HTML URL:', results.gitRepo?.html_url || 'not available');\n console.log('Leantime project ID:', results.leantimeProject?.result?.[0] || 'not available');\n console.log('Documentation ID:', results.docCollection?.data?.id || 'not available');\n console.log('RocketChat channel ID:', results.rocketChatChannel?.channel?._id || 'not available');\n \n return results;\n} catch (error) {\n console.error('Error in Combine Results:', error);\n // Return minimal object to allow workflow to continue\n return {\n error: `Error combining results: ${error.message}`,\n gitRepo: {},\n leantimeProject: {},\n docCollection: {},\n rocketChatChannel: {}\n };\n}" }, "name": "Combine Results", "type": "n8n-nodes-base.function", diff --git a/app/api/missions/route.ts b/app/api/missions/route.ts index 2ddc8557..dba6e36f 100644 --- a/app/api/missions/route.ts +++ b/app/api/missions/route.ts @@ -155,6 +155,15 @@ export async function POST(request: Request) { } const body = await request.json() as MissionCreateInput; + + // Add detailed logging + console.log('Mission API - Request body:', { + name: body.name, + hasServices: Array.isArray(body.services), + services: body.services, + hasGite: body.services?.includes('Gite'), + isN8nRequest: request.headers.get('x-api-key') === process.env.N8N_API_KEY + }); // Validate required fields const requiredFields = ['name', 'oddScope']; diff --git a/lib/services/n8n-service.ts b/lib/services/n8n-service.ts index 74f88112..d2944fbb 100644 --- a/lib/services/n8n-service.ts +++ b/lib/services/n8n-service.ts @@ -18,6 +18,14 @@ export class N8nService { async triggerMissionCreation(data: any): Promise { try { + console.log('N8nService - Input data:', { + hasServices: Array.isArray(data.services), + services: data.services, + hasGite: data.services?.includes('Gite'), + missionProcessed: data.missionProcessed, + config: data.config + }); + // Clean and validate the data const cleanData = { name: data.name, @@ -88,7 +96,7 @@ export class N8nService { } const responseText = await response.text(); - console.log('Raw response from n8n:', responseText); + console.log('N8nService - Raw response:', responseText); // Try to parse the response as JSON try {