NeahNew/lib/services/n8n-service.ts
2025-05-11 21:50:33 +02:00

45 lines
1.4 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-test/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
});
const response = await axios.post(this.webhookUrl, missionData, {
headers: {
'Content-Type': 'application/json'
}
});
console.log('n8n workflow response:', 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) {
throw new Error(`Workflow execution failed: ${response.data.message || 'Unknown error'}`);
}
return response.data;
} catch (error) {
console.error('Error triggering n8n workflow:', error);
throw new Error(`Failed to trigger mission creation workflow: ${error instanceof Error ? error.message : String(error)}`);
}
}
}