corrad-bp/server/api/process/[id].delete.js
Afiq 8805484de2 Enhance Process Flow Components with Improved Node and Edge Management
- Introduced intelligent synchronization for nodes and edges in ProcessFlowCanvas, preventing unnecessary re-renders and improving performance.
- Added state management flags to prevent recursive updates during node and edge changes.
- Implemented explicit sync method for manual canvas updates, enhancing control over the rendering process.
- Updated ProcessSettingsModal to change the label for JSON export to "Source" for better clarity.
- Enhanced VariableManager with improved styling and default value handling, including validation for JSON objects.
- Updated navigation to remove unused icons and improve overall UI consistency.
- Added support for restoring deleted processes in the API, allowing for better data management and recovery options.
- Enhanced process management with new filters and improved loading states in the manage process view.
2025-05-30 18:36:50 +08:00

87 lines
2.4 KiB
JavaScript

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
};
}
});