From 97f1313a181f959e4f3294d00aa810919475f363 Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 24 May 2025 20:38:23 +0200 Subject: [PATCH] W n8n attention vm --- app/api/missions/route.ts | 69 +++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/app/api/missions/route.ts b/app/api/missions/route.ts index 8ee19131..09197345 100644 --- a/app/api/missions/route.ts +++ b/app/api/missions/route.ts @@ -254,26 +254,67 @@ export async function POST(request: Request) { console.log('Mission created successfully:', JSON.stringify(mission, null, 2)); - // Move uploaded files to final location - if (logoPath) { - const finalLogoPath = logoPath.replace('temp', mission.id); - await s3Client.send(new CopyObjectCommand({ - Bucket: 'missions', - CopySource: `missions/${logoPath}`, - Key: finalLogoPath.replace('missions/', '') - })); - await s3Client.send(new DeleteObjectCommand({ - Bucket: 'missions', - Key: logoPath.replace('missions/', '') - })); + // Handle attachments if present + if (body.attachments && body.attachments.length > 0) { + try { + const attachmentPromises = body.attachments.map(async (attachment: any) => { + const base64Data = attachment.data.split(',')[1]; + 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); + uploadedFiles.push({ type: 'attachment', path: filePath }); + + // Create attachment record in database + return prisma.attachment.create({ + data: { + missionId: mission.id, + filename: attachment.name || 'attachment', + filePath: filePath, + fileType: attachment.type || 'application/octet-stream', + fileSize: buffer.length, + uploaderId: userId + } + }); + }); + + await Promise.all(attachmentPromises); + console.log('Attachments uploaded successfully'); + } catch (attachmentError) { + console.error('Error uploading attachments:', attachmentError); + throw new Error('Failed to upload attachments'); + } } - // Step 3: Trigger n8n workflow only after mission is created and logo is uploaded + // 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 try { console.log('=== Starting N8N Workflow ==='); const n8nService = new N8nService(); - // Prepare data for n8n + // Prepare data for n8n with final file paths const n8nData = { ...body, creatorId: userId,