From bd112deec54036c7f08eee113a63a844bb1fc3b6 Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 21 Jan 2026 14:03:15 +0100 Subject: [PATCH] missions finition --- app/api/missions/[missionId]/close/route.ts | 28 +++++++++++++++++++-- app/api/missions/[missionId]/route.ts | 25 +++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/api/missions/[missionId]/close/route.ts b/app/api/missions/[missionId]/close/route.ts index 4df5c57..15cb1ee 100644 --- a/app/api/missions/[missionId]/close/route.ts +++ b/app/api/missions/[missionId]/close/route.ts @@ -35,7 +35,8 @@ export async function POST( include: { user: true } - } + }, + calendars: true } }); @@ -89,9 +90,32 @@ export async function POST( logger.debug('Calling N8N NeahMissionClose webhook', { missionId: mission.id, missionName: mission.name, - hasRepoName: !!repoName + hasRepoName: !!repoName, + calendarsCount: mission.calendars.length }); + // Delete all calendars associated with this mission before closing + try { + if (mission.calendars.length > 0) { + const calendarIds = mission.calendars.map(cal => cal.id); + await prisma.calendar.deleteMany({ + where: { + id: { in: calendarIds } + } + }); + logger.debug('Mission calendars deleted before closing', { + missionId, + calendarsDeleted: mission.calendars.length + }); + } + } catch (calendarDeleteError) { + logger.error('Error deleting mission calendars before closing (continuing)', { + error: calendarDeleteError instanceof Error ? calendarDeleteError.message : String(calendarDeleteError), + missionId + }); + // Continue with closing even if calendar deletion fails + } + // Call N8N webhook const webhookUrl = process.env.N8N_CLOSE_MISSION_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/NeahMissionClose'; const apiKey = process.env.N8N_API_KEY || ''; diff --git a/app/api/missions/[missionId]/route.ts b/app/api/missions/[missionId]/route.ts index e4533a1..a2e5fd4 100644 --- a/app/api/missions/[missionId]/route.ts +++ b/app/api/missions/[missionId]/route.ts @@ -141,9 +141,32 @@ export async function DELETE( missionId, missionName: mission.name, hasLogo: !!mission.logo, - attachmentsCount: mission.attachments.length + attachmentsCount: mission.attachments.length, + calendarsCount: mission.calendars.length }); + // Step 0: Delete all calendars associated with this mission + try { + if (mission.calendars.length > 0) { + const calendarIds = mission.calendars.map(cal => cal.id); + await prisma.calendar.deleteMany({ + where: { + id: { in: calendarIds } + } + }); + logger.debug('Mission calendars deleted', { + missionId, + calendarsDeleted: mission.calendars.length + }); + } + } catch (calendarDeleteError) { + logger.error('Error deleting mission calendars (continuing)', { + error: calendarDeleteError instanceof Error ? calendarDeleteError.message : String(calendarDeleteError), + missionId + }); + // Continue with deletion even if calendar deletion fails + } + // Step 1: Delete all files from S3/MinIO try { // Delete logo if exists