71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import { env } from '@/lib/env';
|
|
|
|
export class N8nService {
|
|
private readonly n8nUrl: string;
|
|
private readonly apiKey: string;
|
|
|
|
constructor() {
|
|
this.n8nUrl = process.env.N8N_URL || '';
|
|
this.apiKey = process.env.N8N_API_KEY || '';
|
|
|
|
if (!this.n8nUrl || !this.apiKey) {
|
|
throw new Error('N8N_URL and N8N_API_KEY must be set in environment variables');
|
|
}
|
|
}
|
|
|
|
async triggerMissionCreation(data: any): Promise<{ success: boolean; error?: string }> {
|
|
try {
|
|
const response = await fetch(`${this.n8nUrl}/webhook/mission-creation`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-API-Key': this.apiKey
|
|
},
|
|
body: JSON.stringify(data)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
return {
|
|
success: false,
|
|
error: errorData.message || 'Failed to trigger n8n workflow'
|
|
};
|
|
}
|
|
|
|
return { success: true };
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Failed to trigger n8n workflow'
|
|
};
|
|
}
|
|
}
|
|
|
|
async rollbackMissionCreation(data: any): Promise<{ success: boolean; error?: string }> {
|
|
try {
|
|
const response = await fetch(`${this.n8nUrl}/webhook/mission-rollback`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-API-Key': this.apiKey
|
|
},
|
|
body: JSON.stringify(data)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
return {
|
|
success: false,
|
|
error: errorData.message || 'Failed to trigger n8n rollback workflow'
|
|
};
|
|
}
|
|
|
|
return { success: true };
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Failed to trigger n8n rollback workflow'
|
|
};
|
|
}
|
|
}
|
|
} |