W n8n
This commit is contained in:
parent
bc5f30fd06
commit
f4a730422c
@ -14,38 +14,7 @@ interface MissionUserInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to check authentication
|
// Helper function to check authentication
|
||||||
async function checkAuth(request: Request, body?: any) {
|
async function checkAuth(request: Request) {
|
||||||
// Check for service account API key first
|
|
||||||
const apiKey = request.headers.get('x-api-key');
|
|
||||||
console.log('API key from header:', apiKey);
|
|
||||||
console.log('API key from env:', process.env.N8N_API_KEY);
|
|
||||||
console.log('Keys match:', apiKey === process.env.N8N_API_KEY);
|
|
||||||
if (apiKey === process.env.N8N_API_KEY) {
|
|
||||||
// For service account, use the creatorId from the request body
|
|
||||||
if (body?.creatorId) {
|
|
||||||
console.log('Using creatorId from request body:', body.creatorId);
|
|
||||||
// Verify the user exists
|
|
||||||
const user = await prisma.user.findUnique({
|
|
||||||
where: { id: body.creatorId }
|
|
||||||
});
|
|
||||||
if (!user) {
|
|
||||||
console.error('Creator user not found:', body.creatorId);
|
|
||||||
return { authorized: false, userId: null };
|
|
||||||
}
|
|
||||||
return { authorized: true, userId: body.creatorId };
|
|
||||||
}
|
|
||||||
// Fallback to system user if no creatorId provided
|
|
||||||
console.log('No creatorId provided, using system user');
|
|
||||||
// Use the first user in the database as system user
|
|
||||||
const systemUser = await prisma.user.findFirst();
|
|
||||||
if (!systemUser) {
|
|
||||||
console.error('No users found in database for system user fallback');
|
|
||||||
return { authorized: false, userId: null };
|
|
||||||
}
|
|
||||||
return { authorized: true, userId: systemUser.id };
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall back to NextAuth session for regular users
|
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
console.error('Unauthorized access attempt:', {
|
console.error('Unauthorized access attempt:', {
|
||||||
@ -150,8 +119,8 @@ export async function GET(request: Request) {
|
|||||||
// POST endpoint to create a new mission
|
// POST endpoint to create a new mission
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const session = await getServerSession(authOptions);
|
const { authorized, userId } = await checkAuth(request);
|
||||||
if (!session?.user) {
|
if (!authorized || !userId) {
|
||||||
console.error('Unauthorized access attempt - no session or user');
|
console.error('Unauthorized access attempt - no session or user');
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
@ -209,7 +178,7 @@ export async function POST(request: Request) {
|
|||||||
services: Array.isArray(services) ? services.filter(Boolean) : [],
|
services: Array.isArray(services) ? services.filter(Boolean) : [],
|
||||||
profils: Array.isArray(profils) ? profils.filter(Boolean) : [],
|
profils: Array.isArray(profils) ? profils.filter(Boolean) : [],
|
||||||
participation,
|
participation,
|
||||||
creatorId: session.user.id
|
creatorId: userId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -10,12 +10,17 @@ export class N8nService {
|
|||||||
this.webhookUrl = process.env.N8N_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/mission-created';
|
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';
|
this.rollbackWebhookUrl = process.env.N8N_ROLLBACK_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/mission-rollback';
|
||||||
this.apiKey = process.env.N8N_API_KEY || '';
|
this.apiKey = process.env.N8N_API_KEY || '';
|
||||||
|
|
||||||
|
if (!this.apiKey) {
|
||||||
|
console.error('N8N_API_KEY is not set in environment variables');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async triggerMissionCreation(data: any): Promise<any> {
|
async triggerMissionCreation(data: any): Promise<any> {
|
||||||
try {
|
try {
|
||||||
console.log('Triggering n8n workflow with data:', JSON.stringify(data, null, 2));
|
console.log('Triggering n8n workflow with data:', JSON.stringify(data, null, 2));
|
||||||
console.log('Using webhook URL:', this.webhookUrl);
|
console.log('Using webhook URL:', this.webhookUrl);
|
||||||
|
console.log('API key present:', !!this.apiKey);
|
||||||
|
|
||||||
// Log the full request details
|
// Log the full request details
|
||||||
const requestDetails = {
|
const requestDetails = {
|
||||||
@ -83,6 +88,7 @@ export class N8nService {
|
|||||||
try {
|
try {
|
||||||
console.log('Triggering n8n rollback workflow with data:', JSON.stringify(data, null, 2));
|
console.log('Triggering n8n rollback workflow with data:', JSON.stringify(data, null, 2));
|
||||||
console.log('Using rollback webhook URL:', this.rollbackWebhookUrl);
|
console.log('Using rollback webhook URL:', this.rollbackWebhookUrl);
|
||||||
|
console.log('API key present:', !!this.apiKey);
|
||||||
|
|
||||||
const response = await fetch(this.rollbackWebhookUrl, {
|
const response = await fetch(this.rollbackWebhookUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -94,7 +100,9 @@ export class N8nService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
const errorText = await response.text();
|
||||||
|
console.error('Rollback webhook error response:', errorText);
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user