NeahNew/lib/services/n8n-service.ts
2025-05-18 09:03:46 +02:00

145 lines
4.9 KiB
TypeScript

import { env } from '@/lib/env';
export class N8nService {
private webhookUrl: string;
private rollbackWebhookUrl: string;
constructor() {
// Use consistent webhook URLs without -test suffix
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';
}
async triggerMissionCreation(data: any): Promise<any> {
try {
console.log('Triggering n8n workflow with data:', JSON.stringify(data, null, 2));
console.log('Using webhook URL:', this.webhookUrl);
const response = await fetch(this.webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
console.log('Webhook response status:', response.status);
console.log('Webhook response headers:', Object.fromEntries(response.headers.entries()));
if (!response.ok) {
const errorText = await response.text();
console.error('Webhook error response:', errorText);
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);
}
// Handle allEntries format
const responseText = await response.text();
console.log('Raw response from n8n:', responseText);
try {
// Try to parse as JSON first
const result = JSON.parse(responseText);
console.log('Parsed JSON response:', JSON.stringify(result, null, 2));
// Handle lastNodeJson format
if (result.lastNodeJson) {
try {
const parsedResults = JSON.parse(result.lastNodeJson);
return {
success: true,
results: {
leantimeProjectId: parsedResults.leantimeProjectId?.toString() || null,
outlineCollectionId: parsedResults.outlineCollectionId?.toString() || null,
rocketChatChannelId: parsedResults.rocketChatChannelId?.toString() || null,
giteaRepositoryUrl: parsedResults.giteaRepositoryUrl || null
}
};
} catch (parseError) {
console.error('Error parsing lastNodeJson:', parseError);
return {
success: false,
error: 'Failed to parse n8n response',
results: {
leantimeProjectId: null,
outlineCollectionId: null,
rocketChatChannelId: null,
giteaRepositoryUrl: null
}
};
}
}
// Handle regular JSON response
const integrationResults = result.results || result;
console.log('Integration results:', JSON.stringify(integrationResults, null, 2));
return {
success: true,
results: {
leantimeProjectId: integrationResults.leantimeProjectId?.toString() || null,
outlineCollectionId: integrationResults.outlineCollectionId?.toString() || null,
rocketChatChannelId: integrationResults.rocketChatChannelId?.toString() || null,
giteaRepositoryUrl: integrationResults.giteaRepositoryUrl || null
}
};
} catch (parseError) {
console.error('Error parsing response:', parseError);
// If parsing fails, return a default success response
return {
success: true,
results: {
leantimeProjectId: null,
outlineCollectionId: null,
rocketChatChannelId: null,
giteaRepositoryUrl: null
}
};
}
} catch (error) {
console.error('Error triggering n8n workflow:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
results: {
leantimeProjectId: null,
outlineCollectionId: null,
rocketChatChannelId: null,
giteaRepositoryUrl: null
}
};
}
}
async triggerMissionRollback(data: any): Promise<any> {
try {
console.log('Triggering n8n rollback workflow with data:', JSON.stringify(data, null, 2));
console.log('Using rollback webhook URL:', this.rollbackWebhookUrl);
const response = await fetch(this.rollbackWebhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
console.log('Received response from n8n rollback:', JSON.stringify(result, null, 2));
return {
success: true,
results: result
};
} catch (error) {
console.error('Error triggering n8n rollback workflow:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
}