W n8n attention vm
This commit is contained in:
parent
ea96b1d416
commit
503afb99f9
@ -187,12 +187,14 @@ 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 {
|
||||||
|
console.log('=== Mission Creation Started ===');
|
||||||
const { authorized, userId } = await checkAuth(request);
|
const { authorized, userId } = await checkAuth(request);
|
||||||
if (!authorized || !userId) {
|
if (!authorized || !userId) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
|
console.log('Received request body:', JSON.stringify(body, null, 2));
|
||||||
|
|
||||||
// Simple validation
|
// Simple validation
|
||||||
if (!body.name || !body.oddScope) {
|
if (!body.name || !body.oddScope) {
|
||||||
@ -204,9 +206,11 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
// Check if this is a request from n8n
|
// Check if this is a request from n8n
|
||||||
const isN8nRequest = request.headers.get('x-api-key') === process.env.N8N_API_KEY;
|
const isN8nRequest = request.headers.get('x-api-key') === process.env.N8N_API_KEY;
|
||||||
|
console.log('Is N8N request:', isN8nRequest);
|
||||||
|
|
||||||
if (!isN8nRequest) {
|
if (!isN8nRequest) {
|
||||||
try {
|
try {
|
||||||
|
console.log('=== Starting N8N Workflow ===');
|
||||||
const n8nService = new N8nService();
|
const n8nService = new N8nService();
|
||||||
|
|
||||||
// Prepare data for n8n
|
// Prepare data for n8n
|
||||||
@ -218,11 +222,14 @@ export async function POST(request: Request) {
|
|||||||
MISSION_API_URL: process.env.NEXT_PUBLIC_API_URL
|
MISSION_API_URL: process.env.NEXT_PUBLIC_API_URL
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
console.log('Sending to N8N:', JSON.stringify(n8nData, null, 2));
|
||||||
|
|
||||||
// Trigger n8n workflow first
|
// Trigger n8n workflow first
|
||||||
const workflowResult = await n8nService.triggerMissionCreation(n8nData);
|
const workflowResult = await n8nService.triggerMissionCreation(n8nData);
|
||||||
|
console.log('N8N Workflow Result:', JSON.stringify(workflowResult, null, 2));
|
||||||
|
|
||||||
if (!workflowResult.success) {
|
if (!workflowResult.success) {
|
||||||
|
console.error('N8N workflow failed:', workflowResult.error);
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
error: 'Failed to create mission resources',
|
error: 'Failed to create mission resources',
|
||||||
details: workflowResult.error
|
details: workflowResult.error
|
||||||
@ -231,24 +238,20 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
// Only create mission in database after n8n succeeds
|
// Only create mission in database after n8n succeeds
|
||||||
try {
|
try {
|
||||||
// Log the entire workflow result
|
|
||||||
console.log('N8N Workflow Result:', JSON.stringify(workflowResult, null, 2));
|
|
||||||
|
|
||||||
// Extract logo path from workflow result
|
// Extract logo path from workflow result
|
||||||
let logoPath = null;
|
let logoPath = null;
|
||||||
if (workflowResult.data && Array.isArray(workflowResult.data)) {
|
console.log('Full workflow result:', JSON.stringify(workflowResult, null, 2));
|
||||||
const firstResult = workflowResult.data[0];
|
|
||||||
if (firstResult && firstResult.logoPath) {
|
if (workflowResult.results && Array.isArray(workflowResult.results)) {
|
||||||
logoPath = firstResult.logoPath;
|
const firstResult = workflowResult.results[0];
|
||||||
|
console.log('First result from workflow:', JSON.stringify(firstResult, null, 2));
|
||||||
|
|
||||||
|
if (firstResult && firstResult.json && firstResult.json.logoPath) {
|
||||||
|
logoPath = firstResult.json.logoPath;
|
||||||
|
console.log('Extracted logo path:', logoPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Creating mission with data:', {
|
|
||||||
name: body.name,
|
|
||||||
logoPath,
|
|
||||||
workflowResult: workflowResult
|
|
||||||
});
|
|
||||||
|
|
||||||
const missionData = {
|
const missionData = {
|
||||||
name: body.name,
|
name: body.name,
|
||||||
oddScope: body.oddScope,
|
oddScope: body.oddScope,
|
||||||
@ -264,17 +267,13 @@ export async function POST(request: Request) {
|
|||||||
logo: logoPath ? `missions/${logoPath}` : null
|
logo: logoPath ? `missions/${logoPath}` : null
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('Mission data to be saved:', missionData);
|
console.log('Creating mission with data:', JSON.stringify(missionData, null, 2));
|
||||||
|
|
||||||
const mission = await prisma.mission.create({
|
const mission = await prisma.mission.create({
|
||||||
data: missionData
|
data: missionData
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Mission created with logo:', {
|
console.log('Mission created successfully:', JSON.stringify(mission, null, 2));
|
||||||
missionId: mission.id,
|
|
||||||
logo: mission.logo,
|
|
||||||
fullMission: mission
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
@ -296,6 +295,8 @@ export async function POST(request: Request) {
|
|||||||
}, { status: 500 });
|
}, { status: 500 });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
console.log('=== Handling N8N Callback ===');
|
||||||
|
console.log('N8N callback body:', JSON.stringify(body, null, 2));
|
||||||
// Handle n8n callback - update mission with integration IDs
|
// Handle n8n callback - update mission with integration IDs
|
||||||
try {
|
try {
|
||||||
const mission = await prisma.mission.update({
|
const mission = await prisma.mission.update({
|
||||||
@ -305,10 +306,13 @@ export async function POST(request: Request) {
|
|||||||
outlineCollectionId: body.outlineCollectionId || null,
|
outlineCollectionId: body.outlineCollectionId || null,
|
||||||
rocketChatChannelId: body.rocketChatChannelId || null,
|
rocketChatChannelId: body.rocketChatChannelId || null,
|
||||||
giteaRepositoryUrl: body.giteaRepositoryUrl || null,
|
giteaRepositoryUrl: body.giteaRepositoryUrl || null,
|
||||||
penpotProjectId: body.penpotProjectId || null
|
penpotProjectId: body.penpotProjectId || null,
|
||||||
|
logo: body.logoPath ? `missions/${body.logoPath}` : null // Add logo path update here
|
||||||
} as Prisma.MissionUpdateInput
|
} as Prisma.MissionUpdateInput
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('Mission updated with integrations:', JSON.stringify(mission, null, 2));
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
mission,
|
mission,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user