n8n int cleaning
This commit is contained in:
parent
698a6e52ee
commit
9bd796a585
@ -1,75 +1,42 @@
|
|||||||
import axios from 'axios';
|
import { env } from '@/lib/env';
|
||||||
|
|
||||||
export class N8nService {
|
export class N8nService {
|
||||||
private webhookUrl: string;
|
private webhookUrl: string;
|
||||||
private rollbackWebhookUrl: string;
|
private rollbackWebhookUrl: string;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
// Use consistent webhook URLs without -test suffix
|
||||||
this.webhookUrl = process.env.N8N_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/mission-created';
|
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';
|
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
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async triggerMissionCreation(data: any): Promise<any> {
|
||||||
* 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> {
|
|
||||||
try {
|
try {
|
||||||
console.log('Triggering n8n workflow for mission creation:', {
|
console.log('Triggering n8n workflow with data:', JSON.stringify(data, null, 2));
|
||||||
name: missionData.name,
|
console.log('Using webhook URL:', this.webhookUrl);
|
||||||
missionType: missionData.missionType,
|
|
||||||
webhookUrl: this.webhookUrl,
|
|
||||||
fullData: JSON.stringify(missionData, null, 2)
|
|
||||||
});
|
|
||||||
|
|
||||||
const response = await axios.post(this.webhookUrl, missionData, {
|
const response = await fetch(this.webhookUrl, {
|
||||||
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept': 'application/json'
|
|
||||||
},
|
},
|
||||||
timeout: 30000 // 30 second timeout
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('n8n workflow response:', {
|
if (!response.ok) {
|
||||||
status: response.status,
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
statusText: response.statusText,
|
}
|
||||||
headers: response.headers,
|
|
||||||
data: response.data,
|
|
||||||
contentType: response.headers['content-type']
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle string response
|
const result = await response.json();
|
||||||
if (typeof response.data === 'string') {
|
console.log('Received response from n8n:', JSON.stringify(result, null, 2));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
return {
|
||||||
success: true,
|
success: false,
|
||||||
|
error: 'Invalid response format from n8n',
|
||||||
results: {
|
results: {
|
||||||
message: response.data,
|
|
||||||
leantimeProjectId: null,
|
leantimeProjectId: null,
|
||||||
outlineCollectionId: null,
|
outlineCollectionId: null,
|
||||||
rocketChatChannelId: null,
|
rocketChatChannelId: null,
|
||||||
@ -78,102 +45,64 @@ export class N8nService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.data.errors && response.data.errors.length > 0) {
|
// Extract results from the response
|
||||||
console.warn('Workflow completed with partial success:', response.data.errors);
|
const integrationResults = result.results || result;
|
||||||
return response.data;
|
console.log('Integration results:', JSON.stringify(integrationResults, null, 2));
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.data.success) {
|
return {
|
||||||
console.error('Workflow execution failed:', {
|
success: true,
|
||||||
message: response.data.message,
|
results: {
|
||||||
data: response.data
|
leantimeProjectId: integrationResults.leantimeProjectId?.toString() || null,
|
||||||
});
|
outlineCollectionId: integrationResults.outlineCollectionId?.toString() || null,
|
||||||
throw new Error(`Workflow execution failed: ${response.data.message || 'Unknown error'}`);
|
rocketChatChannelId: integrationResults.rocketChatChannelId?.toString() || null,
|
||||||
}
|
giteaRepositoryUrl: integrationResults.giteaRepositoryUrl || null
|
||||||
|
}
|
||||||
// Ensure the response has the expected structure
|
};
|
||||||
const result = response.data;
|
} catch (error) {
|
||||||
if (!result.results) {
|
console.error('Error triggering n8n workflow:', error);
|
||||||
console.warn('Response missing results object, adding default structure');
|
return {
|
||||||
result.results = {
|
success: false,
|
||||||
|
error: error instanceof Error ? error.message : 'Unknown error',
|
||||||
|
results: {
|
||||||
leantimeProjectId: null,
|
leantimeProjectId: null,
|
||||||
outlineCollectionId: null,
|
outlineCollectionId: null,
|
||||||
rocketChatChannelId: null,
|
rocketChatChannelId: null,
|
||||||
giteaRepositoryUrl: 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)}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async triggerMissionRollback(data: any): Promise<any> {
|
||||||
* Trigger the mission rollback workflow in n8n
|
|
||||||
* @param mission The mission to rollback
|
|
||||||
* @returns The workflow execution result
|
|
||||||
*/
|
|
||||||
async rollbackMission(mission: any): Promise<any> {
|
|
||||||
try {
|
try {
|
||||||
console.log('Triggering n8n workflow for mission rollback:', {
|
console.log('Triggering n8n rollback workflow with data:', JSON.stringify(data, null, 2));
|
||||||
id: mission.id,
|
console.log('Using rollback webhook URL:', this.rollbackWebhookUrl);
|
||||||
name: mission.name,
|
|
||||||
webhookUrl: this.rollbackWebhookUrl,
|
|
||||||
fullData: JSON.stringify(mission, null, 2)
|
|
||||||
});
|
|
||||||
|
|
||||||
const response = await axios.post(this.rollbackWebhookUrl, mission, {
|
const response = await fetch(this.rollbackWebhookUrl, {
|
||||||
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
timeout: 30000 // 30 second timeout
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('n8n rollback workflow response:', {
|
if (!response.ok) {
|
||||||
status: response.status,
|
throw new Error(`HTTP error! 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.data.errors && response.data.errors.length > 0) {
|
const result = await response.json();
|
||||||
console.warn('Rollback workflow completed with partial success:', response.data.errors);
|
console.log('Received response from n8n rollback:', JSON.stringify(result, null, 2));
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.data.success) {
|
return {
|
||||||
console.error('Rollback workflow execution failed:', {
|
success: true,
|
||||||
message: response.data.message,
|
results: result
|
||||||
data: response.data
|
};
|
||||||
});
|
|
||||||
throw new Error(`Rollback workflow execution failed: ${response.data.message || 'Unknown error'}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.data;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error triggering n8n rollback workflow:', {
|
console.error('Error triggering n8n rollback workflow:', error);
|
||||||
error: error instanceof Error ? error.message : String(error),
|
return {
|
||||||
webhookUrl: this.rollbackWebhookUrl,
|
success: false,
|
||||||
errorDetails: error instanceof Error ? error.stack : undefined
|
error: error instanceof Error ? error.message : 'Unknown error'
|
||||||
});
|
};
|
||||||
throw new Error(`Failed to trigger mission rollback workflow: ${error instanceof Error ? error.message : String(error)}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user