corrad-bp/server/api/process/index.get.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

113 lines
2.7 KiB
JavaScript

import { PrismaClient } from '@prisma/client';
// Initialize Prisma client
const prisma = new PrismaClient();
export default defineEventHandler(async (event) => {
try {
// Get query parameters
const query = getQuery(event);
const {
page = 1,
limit = 20,
status,
category,
search,
isTemplate,
sortBy = 'processCreatedDate',
sortOrder = 'desc'
} = query;
// Build where clause
const where = {};
if (status) {
where.processStatus = status;
}
if (category) {
where.processCategory = category;
}
if (isTemplate !== undefined) {
where.isTemplate = isTemplate === 'true';
}
if (search) {
where.OR = [
{ processName: { contains: search, mode: 'insensitive' } },
{ processDescription: { contains: search, mode: 'insensitive' } }
];
}
// Calculate pagination
const skip = (parseInt(page) - 1) * parseInt(limit);
const take = parseInt(limit);
// Build orderBy clause
const orderBy = {};
orderBy[sortBy] = sortOrder;
// Get processes with pagination
const [processes, totalCount] = await Promise.all([
prisma.process.findMany({
where,
orderBy,
skip,
take,
select: {
processID: true,
processUUID: true,
processName: true,
processDescription: true,
processCategory: true,
processPriority: true,
processOwner: true,
processVersion: true,
processStatus: true,
isTemplate: true,
templateCategory: true,
processCreatedDate: true,
processModifiedDate: true,
// Don't include the full definition data to keep response size small
creator: {
select: {
userID: true,
userFullName: true,
userUsername: true
}
}
}
}),
prisma.process.count({ where })
]);
// Calculate pagination info
const totalPages = Math.ceil(totalCount / take);
const hasNextPage = parseInt(page) < totalPages;
const hasPrevPage = parseInt(page) > 1;
return {
success: true,
data: {
processes,
pagination: {
currentPage: parseInt(page),
totalPages,
totalCount,
limit: take,
hasNextPage,
hasPrevPage
}
}
};
} catch (error) {
console.error('Error fetching processes:', error);
return {
success: false,
error: 'Failed to fetch processes',
details: process.env.NODE_ENV === 'development' ? error.message : undefined
};
}
});