68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
import axios from 'axios';
|
|
|
|
export class N8nService {
|
|
private webhookUrl: string;
|
|
|
|
constructor() {
|
|
this.webhookUrl = process.env.N8N_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/mission-created';
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
});
|
|
|
|
const response = await axios.post(this.webhookUrl, missionData, {
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
console.log('n8n workflow response:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
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
|
|
}
|
|
};
|
|
}
|
|
|
|
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'}`);
|
|
}
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error triggering n8n workflow:', {
|
|
error: error instanceof Error ? error.message : String(error),
|
|
webhookUrl: this.webhookUrl
|
|
});
|
|
throw new Error(`Failed to trigger mission creation workflow: ${error instanceof Error ? error.message : String(error)}`);
|
|
}
|
|
}
|
|
} |