From 93d9cc8a78209dc395b55288c9427822d8664661 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 18 May 2025 09:03:46 +0200 Subject: [PATCH] n8n int cleaning --- lib/services/n8n-service.ts | 101 +++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 41 deletions(-) diff --git a/lib/services/n8n-service.ts b/lib/services/n8n-service.ts index a7594549..24a0dc48 100644 --- a/lib/services/n8n-service.ts +++ b/lib/services/n8n-service.ts @@ -32,50 +32,69 @@ export class N8nService { throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`); } - const result = await response.json(); - console.log('Received response from n8n:', JSON.stringify(result, null, 2)); + // Handle allEntries format + const responseText = await response.text(); + console.log('Raw response from n8n:', responseText); - // 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 - } - }; + try { + // Try to parse as JSON first + const result = JSON.parse(responseText); + console.log('Parsed JSON response:', JSON.stringify(result, null, 2)); + + // 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 + } + }; + } } + + // Handle regular JSON response + const integrationResults = result.results || result; + console.log('Integration results:', JSON.stringify(integrationResults, null, 2)); + + return { + success: true, + results: { + leantimeProjectId: integrationResults.leantimeProjectId?.toString() || null, + outlineCollectionId: integrationResults.outlineCollectionId?.toString() || null, + rocketChatChannelId: integrationResults.rocketChatChannelId?.toString() || null, + giteaRepositoryUrl: integrationResults.giteaRepositoryUrl || null + } + }; + } catch (parseError) { + console.error('Error parsing response:', parseError); + // If parsing fails, return a default success response + return { + success: true, + results: { + leantimeProjectId: null, + outlineCollectionId: null, + rocketChatChannelId: null, + giteaRepositoryUrl: null + } + }; } - - // Handle regular JSON response - const integrationResults = result.results || result; - console.log('Integration results:', JSON.stringify(integrationResults, null, 2)); - - return { - success: true, - results: { - leantimeProjectId: integrationResults.leantimeProjectId?.toString() || null, - outlineCollectionId: integrationResults.outlineCollectionId?.toString() || null, - rocketChatChannelId: integrationResults.rocketChatChannelId?.toString() || null, - giteaRepositoryUrl: integrationResults.giteaRepositoryUrl || null - } - }; } catch (error) { console.error('Error triggering n8n workflow:', error); return {