n8n int cleaning

This commit is contained in:
alma 2025-05-12 13:42:04 +02:00
parent 698a6e52ee
commit 9bd796a585
2 changed files with 62 additions and 131 deletions

View File

@ -1,75 +1,42 @@
import axios from 'axios';
import { env } from '@/lib/env';
export class N8nService {
private webhookUrl: string;
private rollbackWebhookUrl: string;
constructor() {
// Use consistent webhook URLs without -test suffix
this.webhookUrl = process.env.N8N_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/mission-created';
this.rollbackWebhookUrl = process.env.N8N_ROLLBACK_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/mission-rollback';
console.log('N8nService initialized with webhook URLs:', {
create: this.webhookUrl,
rollback: this.rollbackWebhookUrl
});
}
/**
* Trigger the mission creation workflow in n8n
* @param missionData The mission data to process
* @returns The workflow execution result
*/
async createMission(missionData: any): Promise<any> {
async triggerMissionCreation(data: any): Promise<any> {
try {
console.log('Triggering n8n workflow for mission creation:', {
name: missionData.name,
missionType: missionData.missionType,
webhookUrl: this.webhookUrl,
fullData: JSON.stringify(missionData, null, 2)
});
console.log('Triggering n8n workflow with data:', JSON.stringify(data, null, 2));
console.log('Using webhook URL:', this.webhookUrl);
const response = await axios.post(this.webhookUrl, missionData, {
const response = await fetch(this.webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
timeout: 30000 // 30 second timeout
body: JSON.stringify(data),
});
console.log('n8n workflow response:', {
status: response.status,
statusText: response.statusText,
headers: response.headers,
data: response.data,
contentType: response.headers['content-type']
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Handle string response
if (typeof response.data === 'string') {
console.warn('Received string response from n8n instead of JSON:', {
response: response.data,
contentType: response.headers['content-type'],
webhookUrl: this.webhookUrl
});
// Try to parse the string as JSON if it looks like JSON
if (response.data.trim().startsWith('{') || response.data.trim().startsWith('[')) {
try {
const parsedData = JSON.parse(response.data);
console.log('Successfully parsed string response as JSON:', parsedData);
return {
success: true,
results: parsedData
};
} catch (parseError) {
console.error('Failed to parse string response as JSON:', parseError);
}
}
const result = await response.json();
console.log('Received response from n8n:', JSON.stringify(result, null, 2));
// If we can't parse it as JSON, return default structure
// Handle different response formats
if (typeof result === 'string') {
console.warn('Received string response from n8n:', result);
return {
success: true,
success: false,
error: 'Invalid response format from n8n',
results: {
message: response.data,
leantimeProjectId: null,
outlineCollectionId: null,
rocketChatChannelId: null,
@ -78,102 +45,64 @@ export class N8nService {
};
}
if (response.data.errors && response.data.errors.length > 0) {
console.warn('Workflow completed with partial success:', response.data.errors);
return response.data;
}
// Extract results from the response
const integrationResults = result.results || result;
console.log('Integration results:', JSON.stringify(integrationResults, null, 2));
if (!response.data.success) {
console.error('Workflow execution failed:', {
message: response.data.message,
data: response.data
});
throw new Error(`Workflow execution failed: ${response.data.message || 'Unknown error'}`);
}
// Ensure the response has the expected structure
const result = response.data;
if (!result.results) {
console.warn('Response missing results object, adding default structure');
result.results = {
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 {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
results: {
leantimeProjectId: null,
outlineCollectionId: null,
rocketChatChannelId: null,
giteaRepositoryUrl: null
};
}
return result;
} catch (error) {
console.error('Error triggering n8n workflow:', {
error: error instanceof Error ? error.message : String(error),
webhookUrl: this.webhookUrl,
errorDetails: error instanceof Error ? error.stack : undefined
});
throw new Error(`Failed to trigger mission creation workflow: ${error instanceof Error ? error.message : String(error)}`);
}
};
}
}
/**
* Trigger the mission rollback workflow in n8n
* @param mission The mission to rollback
* @returns The workflow execution result
*/
async rollbackMission(mission: any): Promise<any> {
async triggerMissionRollback(data: any): Promise<any> {
try {
console.log('Triggering n8n workflow for mission rollback:', {
id: mission.id,
name: mission.name,
webhookUrl: this.rollbackWebhookUrl,
fullData: JSON.stringify(mission, null, 2)
});
console.log('Triggering n8n rollback workflow with data:', JSON.stringify(data, null, 2));
console.log('Using rollback webhook URL:', this.rollbackWebhookUrl);
const response = await axios.post(this.rollbackWebhookUrl, mission, {
const response = await fetch(this.rollbackWebhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
timeout: 30000 // 30 second timeout
body: JSON.stringify(data),
});
console.log('n8n rollback workflow response:', {
status: response.status,
statusText: response.statusText,
headers: response.headers,
data: response.data
});
// Handle string response
if (typeof response.data === 'string') {
console.log('Received string response from n8n rollback, treating as success');
return {
success: true,
results: {
message: response.data
}
};
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
if (response.data.errors && response.data.errors.length > 0) {
console.warn('Rollback workflow completed with partial success:', response.data.errors);
return response.data;
}
const result = await response.json();
console.log('Received response from n8n rollback:', JSON.stringify(result, null, 2));
if (!response.data.success) {
console.error('Rollback workflow execution failed:', {
message: response.data.message,
data: response.data
});
throw new Error(`Rollback workflow execution failed: ${response.data.message || 'Unknown error'}`);
}
return response.data;
return {
success: true,
results: result
};
} catch (error) {
console.error('Error triggering n8n rollback workflow:', {
error: error instanceof Error ? error.message : String(error),
webhookUrl: this.rollbackWebhookUrl,
errorDetails: error instanceof Error ? error.stack : undefined
});
throw new Error(`Failed to trigger mission rollback workflow: ${error instanceof Error ? error.message : String(error)}`);
console.error('Error triggering n8n rollback workflow:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
}

File diff suppressed because one or more lines are too long