EDMS/server/api/dms/folder.patch.js
shb f709707463 Added patch functionality for renaming and moving folders
Added extra functions:
- Move folder into different directories.
- Rename folders functionality
2025-06-12 13:17:33 +08:00

91 lines
2.4 KiB
JavaScript

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export default defineEventHandler( async (event) => {
console.log("This is a test for a patch request.");
const body = await readBody(event);
/*
This is a sample of the expected body data structure:
{
function: "", ["rename", "move"] // Required to determine patch functionality.
cabinet_id: "", // REQUIRED to identify the folder to be patched.
new_name: "", // REQUIRED to rename the folder.
new_parent_id: "", // REQUIRED to move the folder to a new parent.
}
*/
// Function to check for body data.
if (!body.cabinet_id) {
return {
status: 400,
message: "Folder or cabinet_id is required",
}
} else {
const folder = await prisma.cabinets.findUnique({
where: {
cb_id: body.cabinet_id,
}
});
if (!folder) {
return {
status: 404,
message: "The requested folder was not found",
}
}
}
switch (body.function) {
case "rename":
if (!body.new_name) {
return {
status: 400,
message: "new_name was not provided. The folder was not renamed.",
}
}
const renameFolder = await prisma.cabinets.update({
where: {
cb_id: body.cabinet_id,
},
data: {
cb_name: body.new_name,
}
})
return {
status: 200,
message: "Folder renamed successfully",
folder: renameFolder,
}
case "move":
if (!body.new_parent_id || body.new_parent_id === null){
return
}
const moveFolder = await prisma.cabinets.update({
where: {
cb_id: body.cabinet_id,
},
data: {
cb_parent_id: body.new_parent_id,
}
})
return {
status: 200,
message: "Folder moved successfully",
folder: moveFolder,
}
default:
return {
status: 400,
message: "Invalid function",
}
}
})