diff --git a/lib/services/n8n-service.ts b/lib/services/n8n-service.ts index 81e5b788..dfcc342a 100644 --- a/lib/services/n8n-service.ts +++ b/lib/services/n8n-service.ts @@ -30,22 +30,35 @@ export class N8nService { const result = await response.json(); console.log('Received response from n8n:', JSON.stringify(result, null, 2)); - // Handle different response formats - if (typeof result === 'string') { - console.warn('Received string response from n8n:', result); - return { - success: false, - error: 'Invalid response format from n8n', - results: { - leantimeProjectId: null, - outlineCollectionId: null, - rocketChatChannelId: null, - giteaRepositoryUrl: null - } - }; + // Handle lastNodeJson format + if (result.lastNodeJson) { + try { + const parsedResults = JSON.parse(result.lastNodeJson); + return { + success: true, + results: { + leantimeProjectId: parsedResults.leantimeProjectId?.toString() || null, + outlineCollectionId: parsedResults.outlineCollectionId?.toString() || null, + rocketChatChannelId: parsedResults.rocketChatChannelId?.toString() || null, + giteaRepositoryUrl: parsedResults.giteaRepositoryUrl || null + } + }; + } catch (parseError) { + console.error('Error parsing lastNodeJson:', parseError); + return { + success: false, + error: 'Failed to parse n8n response', + results: { + leantimeProjectId: null, + outlineCollectionId: null, + rocketChatChannelId: null, + giteaRepositoryUrl: null + } + }; + } } - // Extract results from the response + // Handle regular JSON response const integrationResults = result.results || result; console.log('Integration results:', JSON.stringify(integrationResults, null, 2));