import { PrismaClient } from '@prisma/client'; // Initialize Prisma client const prisma = new PrismaClient(); export default defineEventHandler(async (event) => { try { // Get the process ID from the route parameter const processId = getRouterParam(event, 'id'); if (!processId) { return { success: false, error: 'Process ID is required' }; } // Check if the ID is a UUID or numeric ID const isUUID = processId.length === 36 && processId.includes('-'); // First, check if the process exists and isn't already deleted const existingProcess = await prisma.process.findFirst({ where: { ...(isUUID ? { processUUID: processId } : { processID: parseInt(processId) }), processStatus: { not: 'deleted' } // Exclude already deleted processes }, select: { processID: true, processName: true, processStatus: true } }); if (!existingProcess) { return { success: false, error: 'Process not found or already deleted' }; } // Optional: Prevent deletion of published processes if (existingProcess.processStatus === 'published') { return { success: false, error: 'Cannot delete published processes. Please unpublish the process first.' }; } // Soft delete: Update status to 'deleted' instead of actual deletion // This preserves all associated data and relationships await prisma.process.update({ where: isUUID ? { processUUID: processId } : { processID: parseInt(processId) }, data: { processStatus: 'deleted', // Optionally add deletion metadata processDeletedDate: new Date(), processModifiedDate: new Date() } }); return { success: true, message: `Process "${existingProcess.processName}" has been moved to trash`, note: 'Process data has been preserved and can be restored if needed' }; } catch (error) { console.error('Error deleting process:', error); // Handle specific Prisma errors if (error.code === 'P2025') { return { success: false, error: 'Process not found' }; } return { success: false, error: 'Failed to delete process', details: process.env.NODE_ENV === 'development' ? error.message : undefined }; } });