157 lines
5.1 KiB
TypeScript
157 lines
5.1 KiB
TypeScript
import axios from 'axios';
|
|
|
|
export class N8nService {
|
|
private webhookUrl: string;
|
|
private rollbackWebhookUrl: string;
|
|
|
|
constructor() {
|
|
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> {
|
|
try {
|
|
console.log('Triggering n8n workflow for mission creation:', {
|
|
name: missionData.name,
|
|
missionType: missionData.missionType,
|
|
webhookUrl: this.webhookUrl,
|
|
fullData: JSON.stringify(missionData, null, 2)
|
|
});
|
|
|
|
const response = await axios.post(this.webhookUrl, missionData, {
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
timeout: 30000 // 30 second timeout
|
|
});
|
|
|
|
console.log('n8n 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, treating as success');
|
|
return {
|
|
success: true,
|
|
results: {
|
|
message: response.data,
|
|
// Add default empty integration results
|
|
leantimeProjectId: null,
|
|
outlineCollectionId: null,
|
|
rocketChatChannelId: null,
|
|
giteaRepositoryUrl: null
|
|
}
|
|
};
|
|
}
|
|
|
|
if (response.data.errors && response.data.errors.length > 0) {
|
|
console.warn('Workflow completed with partial success:', response.data.errors);
|
|
return response.data;
|
|
}
|
|
|
|
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) {
|
|
result.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> {
|
|
try {
|
|
console.log('Triggering n8n workflow for mission rollback:', {
|
|
id: mission.id,
|
|
name: mission.name,
|
|
webhookUrl: this.rollbackWebhookUrl,
|
|
fullData: JSON.stringify(mission, null, 2)
|
|
});
|
|
|
|
const response = await axios.post(this.rollbackWebhookUrl, mission, {
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
timeout: 30000 // 30 second timeout
|
|
});
|
|
|
|
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.data.errors && response.data.errors.length > 0) {
|
|
console.warn('Rollback workflow completed with partial success:', response.data.errors);
|
|
return response.data;
|
|
}
|
|
|
|
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;
|
|
} 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)}`);
|
|
}
|
|
}
|
|
} |