228 lines
6.6 KiB
JavaScript
228 lines
6.6 KiB
JavaScript
import { PrismaClient } from '@prisma/client';
|
|
|
|
// Initialize Prisma client
|
|
const prisma = new PrismaClient();
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
// Get the current authenticated user from the request context
|
|
const currentUser = event.context.user;
|
|
|
|
if (!currentUser || !currentUser.userID) {
|
|
return {
|
|
success: false,
|
|
error: 'Authentication required'
|
|
};
|
|
}
|
|
|
|
// 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 = {};
|
|
|
|
// Exclude deleted processes by default unless explicitly requested
|
|
if (query.includeDeleted !== 'true') {
|
|
where.processStatus = { not: 'deleted' };
|
|
}
|
|
|
|
if (status && status !== 'deleted') {
|
|
// If status filter is provided and it's not 'deleted', filter by that status
|
|
// and still exclude deleted processes
|
|
where.processStatus = status;
|
|
} else if (status === 'deleted') {
|
|
// If specifically requesting deleted processes, only show those
|
|
where.processStatus = 'deleted';
|
|
}
|
|
|
|
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 all processes first
|
|
const allProcesses = await 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,
|
|
processDefinition: true,
|
|
creator: {
|
|
select: {
|
|
userID: true,
|
|
userFullName: true,
|
|
userUsername: true
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
// Get current user's roles
|
|
const userRoles = await prisma.userrole.findMany({
|
|
where: {
|
|
userRoleUserID: parseInt(currentUser.userID)
|
|
},
|
|
select: {
|
|
role: {
|
|
select: {
|
|
roleID: true,
|
|
roleName: true
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
const userRoleIds = userRoles.map(ur => ur.role.roleID);
|
|
const userRoleNames = userRoles.map(ur => ur.role.roleName);
|
|
|
|
// Filter processes based on user assignments in the first form
|
|
const filteredProcesses = [];
|
|
|
|
for (const process of allProcesses) {
|
|
const processDefinition = process.processDefinition || {};
|
|
const nodes = processDefinition.nodes || [];
|
|
const edges = processDefinition.edges || [];
|
|
|
|
// Find all form nodes
|
|
const formNodes = nodes.filter(node => node.type === 'form');
|
|
|
|
if (formNodes.length === 0) {
|
|
// If no form nodes, skip this process
|
|
continue;
|
|
}
|
|
|
|
// Get the first form node (assuming it's the starting form)
|
|
const firstFormNode = formNodes[0];
|
|
const firstFormData = firstFormNode.data || {};
|
|
|
|
// Check if the user is assigned to this form
|
|
let isAssigned = false;
|
|
|
|
// Check assignment type
|
|
const assignmentType = firstFormData.assignmentType || 'public';
|
|
|
|
if (assignmentType === 'public') {
|
|
// Public assignment - anyone can access
|
|
isAssigned = true;
|
|
} else if (assignmentType === 'users') {
|
|
// Check if current user is in assigned users
|
|
const assignedUsers = firstFormData.assignedUsers || [];
|
|
const currentUserIdStr = String(currentUser.userID);
|
|
|
|
|
|
|
|
isAssigned = assignedUsers.some(user =>
|
|
String(user.value) === currentUserIdStr ||
|
|
user.username === currentUser.email
|
|
);
|
|
} else if (assignmentType === 'roles') {
|
|
// Check if current user's roles are in assigned roles
|
|
const assignedRoles = firstFormData.assignedRoles || [];
|
|
|
|
|
|
|
|
isAssigned = assignedRoles.some(role =>
|
|
userRoleIds.includes(parseInt(role.value)) ||
|
|
userRoleNames.includes(role.label)
|
|
);
|
|
} else if (assignmentType === 'variable') {
|
|
// For variable-based assignment, we'll include it for now
|
|
// In a real implementation, you might want to evaluate the variable
|
|
isAssigned = true;
|
|
}
|
|
|
|
if (isAssigned) {
|
|
filteredProcesses.push(process);
|
|
}
|
|
}
|
|
|
|
// Count total processes for pagination (we need to get all processes to filter)
|
|
const totalProcesses = await prisma.process.count({ where });
|
|
|
|
// Calculate pagination info
|
|
const totalPages = Math.ceil(totalProcesses / take);
|
|
const hasNextPage = parseInt(page) < totalPages;
|
|
const hasPrevPage = parseInt(page) > 1;
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
processes: filteredProcesses.map(process => ({
|
|
processID: process.processID,
|
|
processUUID: process.processUUID,
|
|
processName: process.processName,
|
|
processDescription: process.processDescription,
|
|
processCategory: process.processCategory,
|
|
processPriority: process.processPriority,
|
|
processOwner: process.processOwner,
|
|
processVersion: process.processVersion,
|
|
processStatus: process.processStatus,
|
|
isTemplate: process.isTemplate,
|
|
templateCategory: process.templateCategory,
|
|
processCreatedDate: process.processCreatedDate,
|
|
processModifiedDate: process.processModifiedDate,
|
|
creator: process.creator
|
|
})),
|
|
pagination: {
|
|
currentPage: parseInt(page),
|
|
totalPages,
|
|
totalCount: totalProcesses,
|
|
filteredCount: filteredProcesses.length,
|
|
limit: take,
|
|
hasNextPage,
|
|
hasPrevPage
|
|
}
|
|
}
|
|
};
|
|
} catch (error) {
|
|
console.error('Error fetching assigned processes:', error);
|
|
|
|
return {
|
|
success: false,
|
|
error: 'Failed to fetch assigned processes',
|
|
details: process.env.NODE_ENV === 'development' ? error.message : undefined
|
|
};
|
|
}
|
|
});
|