corrad-bp/server/api/process/[id]/unpublish.post.js
Md Afiq Iskandar 0c93e93460 Enhance Form Management and Process Handling
- Updated the form management interface to improve the search and filter functionality, including new filters for category and group, enhancing user experience and organization.
- Redesigned the forms display to utilize a grid layout, providing a more visually appealing and user-friendly interface for form management.
- Introduced loading states and improved error handling for process publishing and unpublishing, ensuring better feedback for users during these actions.
- Added functionality to prevent deletion of published processes, guiding users to unpublish first, thereby improving data integrity and user guidance.
- Enhanced the process store with a new unpublish method, allowing for better management of process states and updates to the UI accordingly.
2025-08-05 17:28:11 +08:00

84 lines
2.0 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, get the current process to validate it can be unpublished
const currentProcess = await prisma.process.findFirst({
where: isUUID
? { processUUID: processId }
: { processID: parseInt(processId) }
});
if (!currentProcess) {
return {
success: false,
error: 'Process not found'
};
}
// Check if the process is currently published
if (currentProcess.processStatus !== 'published') {
return {
success: false,
error: 'Process is not currently published'
};
}
// Update the process status to draft
const unpublishedProcess = await prisma.process.update({
where: isUUID
? { processUUID: processId }
: { processID: parseInt(processId) },
data: {
processStatus: 'draft'
},
include: {
creator: {
select: {
userID: true,
userFullName: true,
userUsername: true
}
}
}
});
return {
success: true,
message: 'Process unpublished successfully',
process: unpublishedProcess
};
} catch (error) {
console.error('Error unpublishing process:', error);
// Handle specific Prisma errors
if (error.code === 'P2025') {
return {
success: false,
error: 'Process not found'
};
}
return {
success: false,
error: 'Failed to unpublish process',
details: process.env.NODE_ENV === 'development' ? error.message : undefined
};
}
});