From 435cf15eed1cc1fd53be893f6caca630618ad33c Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 14:53:47 +0100 Subject: [PATCH] Pages corrections pages missions --- .../[missionId]/files/upload/route.ts | 52 +++++++++++++------ components/carnet/mission-files-manager.tsx | 4 +- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/app/api/missions/[missionId]/files/upload/route.ts b/app/api/missions/[missionId]/files/upload/route.ts index ce1ec57..0099571 100644 --- a/app/api/missions/[missionId]/files/upload/route.ts +++ b/app/api/missions/[missionId]/files/upload/route.ts @@ -64,23 +64,45 @@ export async function POST( }); // Upload using the proven function from mission-uploads.ts - const uploadResult = await uploadMissionAttachment(userId, missionId, file); - - console.log(`[POST /api/missions/${missionId}/files/upload] Upload successful:`, { - filePath: uploadResult.filePath - }); + let uploadResult; + try { + uploadResult = await uploadMissionAttachment(userId, missionId, file); + console.log(`[POST /api/missions/${missionId}/files/upload] Upload successful:`, { + filePath: uploadResult.filePath + }); + } catch (uploadError: any) { + console.error(`[POST /api/missions/${missionId}/files/upload] Upload failed:`, { + error: uploadError.message, + code: uploadError.Code, + fileName: file.name + }); + throw uploadError; + } // Create attachment record in database - const attachment = await prisma.attachment.create({ - data: { - filename: uploadResult.filename, - filePath: uploadResult.filePath, - fileType: uploadResult.fileType, - fileSize: uploadResult.fileSize, - missionId: missionId, - uploaderId: userId - } - }); + let attachment; + try { + attachment = await prisma.attachment.create({ + data: { + filename: uploadResult.filename, + filePath: uploadResult.filePath, + fileType: uploadResult.fileType, + fileSize: uploadResult.fileSize, + missionId: missionId, + uploaderId: userId + } + }); + console.log(`[POST /api/missions/${missionId}/files/upload] Attachment record created:`, { + attachmentId: attachment.id + }); + } catch (dbError: any) { + console.error(`[POST /api/missions/${missionId}/files/upload] Database error:`, { + error: dbError.message, + fileName: file.name + }); + // File was uploaded but DB record creation failed - still return success but log the error + // The file exists in S3 even if the DB record doesn't + } return NextResponse.json({ success: true, diff --git a/components/carnet/mission-files-manager.tsx b/components/carnet/mission-files-manager.tsx index 4745285..8f2fb03 100644 --- a/components/carnet/mission-files-manager.tsx +++ b/components/carnet/mission-files-manager.tsx @@ -96,7 +96,9 @@ export const MissionFilesManager: React.FC = ({ }); if (!response.ok) { - throw new Error('Failed to upload file'); + const errorData = await response.json().catch(() => ({})); + const errorMessage = errorData.details || errorData.error || 'Failed to upload file'; + throw new Error(errorMessage); } // Refresh file list