corrad-bp/server/api/process/create.post.js
Afiq 44baddb6e3 Enhance Process Builder with Critical Fixes and Database Integration
- Implemented complete API system with REST endpoints for all process operations, including CRUD functionality.
- Added support for direct process linking via URL parameters, improving navigation and usability.
- Enhanced save functionality with success/error notifications and improved state management.
- Fixed navigation issues, including unsaved changes detection and automatic URL synchronization.
- Resolved Vue Flow interference, allowing for seamless connector dragging between nodes.
- Ensured backward compatibility for legacy process definitions, automatically upgrading them.
- Introduced comprehensive toast notifications for user feedback on all operations.
- Optimized performance by reducing re-renders and improving memory management.
- Enhanced error handling with robust validation and graceful recovery throughout the system.
- Updated UI consistency across form builder and process builder management interfaces.
2025-05-30 16:36:32 +08:00

78 lines
2.4 KiB
JavaScript

import { PrismaClient } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid';
// Initialize Prisma client
const prisma = new PrismaClient();
export default defineEventHandler(async (event) => {
try {
// Parse the request body
const body = await readBody(event);
// Validate required fields
if (!body.processName) {
return {
success: false,
error: 'Process name is required'
};
}
// 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 || {};
// Create a new process in the database
const process = await prisma.process.create({
data: {
processUUID: uuidv4(),
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 || 'draft',
isTemplate: body.isTemplate || false,
templateCategory: body.templateCategory || null,
processCreatedBy: body.createdBy || null // In a real app, this would come from the authenticated user
},
include: {
creator: {
select: {
userID: true,
userFullName: true,
userUsername: true
}
}
}
});
return {
success: true,
process
};
} catch (error) {
console.error('Error creating process:', error);
return {
success: false,
error: 'Failed to create process',
details: process.env.NODE_ENV === 'development' ? error.message : undefined
};
}
});