From 60b2555e3e4c8df97f959b14761fd7e085507e70 Mon Sep 17 00:00:00 2001 From: alma Date: Tue, 6 May 2025 20:21:26 +0200 Subject: [PATCH] missions api2 gitea --- app/api/missions/[missionId]/route.ts | 53 +++++++++++++++++++++------ lib/services/integration-service.ts | 4 +- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/app/api/missions/[missionId]/route.ts b/app/api/missions/[missionId]/route.ts index 1e72168d..14d8eacd 100644 --- a/app/api/missions/[missionId]/route.ts +++ b/app/api/missions/[missionId]/route.ts @@ -4,6 +4,7 @@ import { authOptions } from "@/app/api/auth/options"; import { prisma } from '@/lib/prisma'; import { deleteMissionLogo } from '@/lib/mission-uploads'; import { getPublicUrl, S3_CONFIG } from '@/lib/s3'; +import { IntegrationService } from '@/lib/services/integration-service'; // Helper function to check authentication async function checkAuth(request: Request) { @@ -34,7 +35,7 @@ export async function GET(request: Request, props: { params: Promise<{ missionId } // Get mission with detailed info - const mission = await prisma.mission.findFirst({ + const mission = await (prisma as any).mission.findFirst({ where: { id: missionId, OR: [ @@ -114,7 +115,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId } // Check if mission exists and user has access to modify it - const existingMission = await prisma.mission.findFirst({ + const existingMission = await (prisma as any).mission.findFirst({ where: { id: missionId, OR: [ @@ -147,7 +148,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId } = body; // Update the mission data - const updatedMission = await prisma.mission.update({ + const updatedMission = await (prisma as any).mission.update({ where: { id: missionId }, data: { name, @@ -167,7 +168,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId // Update guardians if provided if (guardians) { // Get current guardians - const currentGuardians = await prisma.missionUser.findMany({ + const currentGuardians = await (prisma as any).missionUser.findMany({ where: { missionId, role: { in: ['gardien-temps', 'gardien-parole', 'gardien-memoire'] } @@ -176,7 +177,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId // Delete all guardians if (currentGuardians.length > 0) { - await prisma.missionUser.deleteMany({ + await (prisma as any).missionUser.deleteMany({ where: { missionId, role: { in: ['gardien-temps', 'gardien-parole', 'gardien-memoire'] } @@ -195,7 +196,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId })); if (guardianEntries.length > 0) { - await prisma.missionUser.createMany({ + await (prisma as any).missionUser.createMany({ data: guardianEntries }); } @@ -204,7 +205,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId // Update volunteers if provided if (volunteers) { // Get current volunteers - const currentVolunteers = await prisma.missionUser.findMany({ + const currentVolunteers = await (prisma as any).missionUser.findMany({ where: { missionId, role: 'volontaire' @@ -213,7 +214,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId // Delete all volunteers if (currentVolunteers.length > 0) { - await prisma.missionUser.deleteMany({ + await (prisma as any).missionUser.deleteMany({ where: { missionId, role: 'volontaire' @@ -229,7 +230,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId missionId })); - await prisma.missionUser.createMany({ + await (prisma as any).missionUser.createMany({ data: volunteerEntries }); } @@ -267,7 +268,7 @@ export async function DELETE(request: Request, props: { params: Promise<{ missio } // Check if mission exists and user is the creator - const mission = await prisma.mission.findFirst({ + const mission = await (prisma as any).mission.findFirst({ where: { id: missionId, creatorId: userId // Only creator can delete a mission @@ -291,8 +292,38 @@ export async function DELETE(request: Request, props: { params: Promise<{ missio } } + // Clean up external integrations before deleting the mission + try { + // Extract integration IDs from the mission + const leantimeProjectId = mission.leantimeProjectId ? parseInt(mission.leantimeProjectId, 10) : undefined; + const outlineCollectionId = mission.outlineCollectionId; + const rocketChatChannelId = mission.rocketChatChannelId; + const giteaRepositoryUrl = mission.giteaRepositoryUrl; + + // Only attempt rollback if there are any integrations to clean up + if (leantimeProjectId || outlineCollectionId || rocketChatChannelId || giteaRepositoryUrl) { + console.log(`Rolling back integrations for mission ${missionId}...`); + const integrationService = new IntegrationService(); + + // Use the public rollbackIntegrations method + await integrationService.rollbackIntegrations( + leantimeProjectId, + outlineCollectionId, + rocketChatChannelId, + giteaRepositoryUrl + ); + console.log(`Successfully rolled back integrations for mission ${missionId}`); + } else { + console.log(`No integrations to clean up for mission ${missionId}`); + } + } catch (integrationError) { + console.error('Error cleaning up integrations:', integrationError); + // Continue with mission deletion even if integration cleanup fails + // The admin will need to clean up manually in this case + } + // Delete mission (cascade will handle missionUsers and attachments) - await prisma.mission.delete({ + await (prisma as any).mission.delete({ where: { id: missionId } }); diff --git a/lib/services/integration-service.ts b/lib/services/integration-service.ts index 9972fc1d..6c4c2f15 100644 --- a/lib/services/integration-service.ts +++ b/lib/services/integration-service.ts @@ -235,7 +235,7 @@ export class IntegrationService { * @param rocketChatChannelId The Rocket.Chat channel ID to delete * @param giteaRepositoryUrl The Gitea repository URL to extract owner/repo from for deletion */ - private async rollbackIntegrations( + public async rollbackIntegrations( leantimeProjectId?: number, outlineCollectionId?: string, rocketChatChannelId?: string, @@ -346,4 +346,4 @@ export class IntegrationService { if (giteaRepositoryUrl) console.log(`- Gitea repository: ${giteaRepositoryUrl}`); } } -} \ No newline at end of file +} \ No newline at end of file