missions api2 gitea

This commit is contained in:
alma 2025-05-06 20:21:26 +02:00
parent f2a08fbadb
commit 60b2555e3e
2 changed files with 44 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import { authOptions } from "@/app/api/auth/options";
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
import { deleteMissionLogo } from '@/lib/mission-uploads'; import { deleteMissionLogo } from '@/lib/mission-uploads';
import { getPublicUrl, S3_CONFIG } from '@/lib/s3'; import { getPublicUrl, S3_CONFIG } from '@/lib/s3';
import { IntegrationService } from '@/lib/services/integration-service';
// Helper function to check authentication // Helper function to check authentication
async function checkAuth(request: Request) { async function checkAuth(request: Request) {
@ -34,7 +35,7 @@ export async function GET(request: Request, props: { params: Promise<{ missionId
} }
// Get mission with detailed info // Get mission with detailed info
const mission = await prisma.mission.findFirst({ const mission = await (prisma as any).mission.findFirst({
where: { where: {
id: missionId, id: missionId,
OR: [ 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 // 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: { where: {
id: missionId, id: missionId,
OR: [ OR: [
@ -147,7 +148,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId
} = body; } = body;
// Update the mission data // Update the mission data
const updatedMission = await prisma.mission.update({ const updatedMission = await (prisma as any).mission.update({
where: { id: missionId }, where: { id: missionId },
data: { data: {
name, name,
@ -167,7 +168,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId
// Update guardians if provided // Update guardians if provided
if (guardians) { if (guardians) {
// Get current guardians // Get current guardians
const currentGuardians = await prisma.missionUser.findMany({ const currentGuardians = await (prisma as any).missionUser.findMany({
where: { where: {
missionId, missionId,
role: { in: ['gardien-temps', 'gardien-parole', 'gardien-memoire'] } 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 // Delete all guardians
if (currentGuardians.length > 0) { if (currentGuardians.length > 0) {
await prisma.missionUser.deleteMany({ await (prisma as any).missionUser.deleteMany({
where: { where: {
missionId, missionId,
role: { in: ['gardien-temps', 'gardien-parole', 'gardien-memoire'] } 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) { if (guardianEntries.length > 0) {
await prisma.missionUser.createMany({ await (prisma as any).missionUser.createMany({
data: guardianEntries data: guardianEntries
}); });
} }
@ -204,7 +205,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId
// Update volunteers if provided // Update volunteers if provided
if (volunteers) { if (volunteers) {
// Get current volunteers // Get current volunteers
const currentVolunteers = await prisma.missionUser.findMany({ const currentVolunteers = await (prisma as any).missionUser.findMany({
where: { where: {
missionId, missionId,
role: 'volontaire' role: 'volontaire'
@ -213,7 +214,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId
// Delete all volunteers // Delete all volunteers
if (currentVolunteers.length > 0) { if (currentVolunteers.length > 0) {
await prisma.missionUser.deleteMany({ await (prisma as any).missionUser.deleteMany({
where: { where: {
missionId, missionId,
role: 'volontaire' role: 'volontaire'
@ -229,7 +230,7 @@ export async function PUT(request: Request, props: { params: Promise<{ missionId
missionId missionId
})); }));
await prisma.missionUser.createMany({ await (prisma as any).missionUser.createMany({
data: volunteerEntries 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 // Check if mission exists and user is the creator
const mission = await prisma.mission.findFirst({ const mission = await (prisma as any).mission.findFirst({
where: { where: {
id: missionId, id: missionId,
creatorId: userId // Only creator can delete a mission 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) // Delete mission (cascade will handle missionUsers and attachments)
await prisma.mission.delete({ await (prisma as any).mission.delete({
where: { id: missionId } where: { id: missionId }
}); });

View File

@ -235,7 +235,7 @@ export class IntegrationService {
* @param rocketChatChannelId The Rocket.Chat channel ID to delete * @param rocketChatChannelId The Rocket.Chat channel ID to delete
* @param giteaRepositoryUrl The Gitea repository URL to extract owner/repo from for deletion * @param giteaRepositoryUrl The Gitea repository URL to extract owner/repo from for deletion
*/ */
private async rollbackIntegrations( public async rollbackIntegrations(
leantimeProjectId?: number, leantimeProjectId?: number,
outlineCollectionId?: string, outlineCollectionId?: string,
rocketChatChannelId?: string, rocketChatChannelId?: string,
@ -346,4 +346,4 @@ export class IntegrationService {
if (giteaRepositoryUrl) console.log(`- Gitea repository: ${giteaRepositoryUrl}`); if (giteaRepositoryUrl) console.log(`- Gitea repository: ${giteaRepositoryUrl}`);
} }
} }
} }