corrad-bp/server/api/process/[id].put.js
Afiq 00d8341f3a Enhance Process Flow and Store Management with Node Handling Improvements
- Added functionality to handle node additions from drag & drop in ProcessFlowCanvas, ensuring immediate visual feedback and persistence in application state.
- Implemented logic in the process store to prevent duplicate nodes when adding new nodes from the canvas.
- Enhanced server-side logging to track process updates, including node and edge counts for better debugging.
- Improved node extraction logic in the process builder store to create placeholder nodes from edge references when no nodes are present.
- Added debug logging for saving process data to facilitate better tracking of changes in the application state.
2025-06-11 12:23:52 +08:00

156 lines
5.1 KiB
JavaScript

import { PrismaClient } from '@prisma/client';
import { defineEventHandler, getRouterParam, readBody } from 'h3';
// 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'
};
}
// Parse the request body
const body = await readBody(event);
// Validate required fields
if (!body.processName) {
return {
success: false,
error: 'Process name is required'
};
}
// Check if the ID is a UUID or numeric ID
const isUUID = processId.length === 36 && processId.includes('-');
// Find the existing process first to save its current state to history
const existingProcess = await prisma.process.findFirst({
where: isUUID
? { processUUID: processId }
: { processID: parseInt(processId) },
include: {
creator: {
select: {
userID: true,
userFullName: true,
userUsername: true
}
}
}
});
if (!existingProcess) {
return {
success: false,
error: 'Process not found'
};
}
// Get the next version number for history
const lastHistory = await prisma.processHistory.findFirst({
where: { processID: existingProcess.processID },
orderBy: { versionNumber: 'desc' },
select: { versionNumber: true }
});
const nextVersionNumber = (lastHistory?.versionNumber || 0) + 1;
// Save current state to history before updating
await prisma.processHistory.create({
data: {
processID: existingProcess.processID,
processUUID: existingProcess.processUUID,
processName: existingProcess.processName,
processDescription: existingProcess.processDescription,
processDefinition: existingProcess.processDefinition,
processVersion: existingProcess.processVersion,
processStatus: existingProcess.processStatus,
processCategory: existingProcess.processCategory,
processOwner: existingProcess.processOwner,
processPermissions: existingProcess.processPermissions,
processPriority: existingProcess.processPriority,
processSettings: existingProcess.processSettings,
processVariables: existingProcess.processVariables,
templateCategory: existingProcess.templateCategory,
versionNumber: nextVersionNumber,
changeDescription: body.changeDescription || null,
savedBy: body.savedBy || existingProcess.processCreatedBy
}
});
// Debug logging to see what we received
console.log('🔥 Server received process update:', {
processId,
nodeCount: (body.nodes || []).length,
edgeCount: (body.edges || []).length,
nodes: (body.nodes || []).map(n => ({ id: n.id, type: n.type, label: n.label })),
edges: (body.edges || []).map(e => ({ id: e.id, source: e.source, target: e.target }))
});
// Prepare process definition
const processDefinition = {
nodes: body.nodes || [],
edges: body.edges || [],
viewport: body.viewport || { x: 0, y: 0, zoom: 1 }
};
// Prepare process variables (if any)
const processVariables = body.variables || {};
// Prepare process settings (if any)
const processSettings = body.settings || {};
// Prepare process permissions (if any)
const processPermissions = body.permissions || {};
// Update the process
const updatedProcess = await prisma.process.update({
where: isUUID
? { processUUID: processId }
: { processID: parseInt(processId) },
data: {
processName: body.processName,
processDescription: body.processDescription || null,
processCategory: body.processCategory || null,
processPriority: body.processPriority || 'normal',
processOwner: body.processOwner || null,
processDefinition: processDefinition,
processVariables: Object.keys(processVariables).length > 0 ? processVariables : null,
processSettings: Object.keys(processSettings).length > 0 ? processSettings : null,
processPermissions: Object.keys(processPermissions).length > 0 ? processPermissions : null,
processStatus: body.processStatus || existingProcess.processStatus,
processVersion: existingProcess.processVersion + 1
},
include: {
creator: {
select: {
userID: true,
userFullName: true,
userUsername: true
}
}
}
});
return {
success: true,
process: updatedProcess,
message: 'Process updated successfully and previous version saved to history'
};
} catch (error) {
console.error('Error updating process:', error);
return {
success: false,
error: 'Failed to update process',
details: process.env.NODE_ENV === 'development' ? error.message : undefined
};
}
});