From 880d22665e2258706513e1a2d3f7daa24e9950c6 Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 24 May 2025 20:39:52 +0200 Subject: [PATCH] W n8n attention vm --- app/api/missions/route.ts | 94 ++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 55 deletions(-) diff --git a/app/api/missions/route.ts b/app/api/missions/route.ts index 09197345..75d54da4 100644 --- a/app/api/missions/route.ts +++ b/app/api/missions/route.ts @@ -209,28 +209,7 @@ export async function POST(request: Request) { }, { status: 400 }); } - // Step 1: Upload logo to Minio if present - let logoPath = null; - if (body.logo?.data) { - try { - // Convert base64 to File object - const base64Data = body.logo.data.split(',')[1]; - const buffer = Buffer.from(base64Data, 'base64'); - const file = new File([buffer], body.logo.name || 'logo.png', { type: body.logo.type || 'image/png' }); - - // Upload logo - const { filePath } = await uploadMissionLogo(userId, 'temp', file); - logoPath = filePath; - uploadedFiles.push({ type: 'logo', path: filePath }); - - console.log('Logo uploaded successfully:', { logoPath }); - } catch (uploadError) { - console.error('Error uploading logo:', uploadError); - throw new Error('Failed to upload logo'); - } - } - - // Step 2: Create mission in database with logo path + // Step 1: Create mission in database first const missionData = { name: body.name, oddScope: body.oddScope, @@ -243,7 +222,7 @@ export async function POST(request: Request) { profils: body.profils, participation: body.participation, creatorId: userId, - logo: logoPath, + logo: null, // Will update after upload }; console.log('Creating mission with data:', JSON.stringify(missionData, null, 2)); @@ -254,7 +233,34 @@ export async function POST(request: Request) { console.log('Mission created successfully:', JSON.stringify(mission, null, 2)); - // Handle attachments if present + // Step 2: Upload logo to Minio if present + let logoPath = null; + if (body.logo?.data) { + try { + // Convert base64 to File object + const base64Data = body.logo.data.split(',')[1]; + const buffer = Buffer.from(base64Data, 'base64'); + const file = new File([buffer], body.logo.name || 'logo.png', { type: body.logo.type || 'image/png' }); + + // Upload logo using the correct function + const { filePath } = await uploadMissionLogo(userId, mission.id, file); + logoPath = filePath; + uploadedFiles.push({ type: 'logo', path: filePath }); + + // Update mission with logo path + await prisma.mission.update({ + where: { id: mission.id }, + data: { logo: filePath } + }); + + console.log('Logo uploaded successfully:', { logoPath }); + } catch (uploadError) { + console.error('Error uploading logo:', uploadError); + throw new Error('Failed to upload logo'); + } + } + + // Step 3: Handle attachments if present if (body.attachments && body.attachments.length > 0) { try { const attachmentPromises = body.attachments.map(async (attachment: any) => { @@ -262,18 +268,18 @@ export async function POST(request: Request) { const buffer = Buffer.from(base64Data, 'base64'); const file = new File([buffer], attachment.name || 'attachment', { type: attachment.type || 'application/octet-stream' }); - // Upload attachment - const { filePath } = await uploadMissionLogo(userId, 'temp', file); + // Upload attachment using the correct function + const { filePath, filename, fileType, fileSize } = await uploadMissionAttachment(userId, mission.id, file); uploadedFiles.push({ type: 'attachment', path: filePath }); - // Create attachment record in database + // Create attachment record in database with final path return prisma.attachment.create({ data: { missionId: mission.id, - filename: attachment.name || 'attachment', - filePath: filePath, - fileType: attachment.type || 'application/octet-stream', - fileSize: buffer.length, + filename, + filePath, + fileType, + fileSize, uploaderId: userId } }); @@ -287,29 +293,7 @@ export async function POST(request: Request) { } } - // Move uploaded files to final location - try { - const movePromises = uploadedFiles.map(async (file) => { - const finalPath = file.path.replace('temp', mission.id); - await s3Client.send(new CopyObjectCommand({ - Bucket: 'missions', - CopySource: `missions/${file.path}`, - Key: finalPath.replace('missions/', '') - })); - await s3Client.send(new DeleteObjectCommand({ - Bucket: 'missions', - Key: file.path.replace('missions/', '') - })); - }); - - await Promise.all(movePromises); - console.log('All files moved to final location successfully'); - } catch (moveError) { - console.error('Error moving files to final location:', moveError); - throw new Error('Failed to move files to final location'); - } - - // Only trigger n8n workflow after all Minio operations are complete + // Step 4: Trigger n8n workflow after all file operations are complete try { console.log('=== Starting N8N Workflow ==='); const n8nService = new N8nService(); @@ -318,7 +302,7 @@ export async function POST(request: Request) { const n8nData = { ...body, creatorId: userId, - logoPath: logoPath ? logoPath.replace('temp', mission.id) : null, + logoPath: logoPath, config: { N8N_API_KEY: process.env.N8N_API_KEY, MISSION_API_URL: process.env.NEXT_PUBLIC_API_URL