Merge pull request #139 from bipproduction/amalia/22-agustus-24

Amalia/22 agustus 24
This commit is contained in:
Amalia
2024-08-22 15:30:24 +08:00
committed by GitHub
8 changed files with 380 additions and 143 deletions

View File

@@ -1,6 +1,8 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { NextResponse } from "next/server";
// MOVE ITEM
export async function POST(request: Request) {
try {
@@ -9,9 +11,177 @@ export async function POST(request: Request) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const { path, dataItem } = (await request.json());
if (path != "home") {
const cekPath = await prisma.divisionDocumentFolderFile.count({
where: {
isActive: true,
id: path
}
})
if (cekPath == 0) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan path, data tidak ditemukan" }, { status: 404 });
}
}
for (let i = 0; i < dataItem.length; i++) {
let status = false
let numb = 1
let name = dataItem[i].name
do {
const cekName = await prisma.divisionDocumentFolderFile.count({
where: {
path: path,
isActive: true,
extension: dataItem[i].extension,
name
}
})
if (cekName > 0) {
name = dataItem[i].name + " (" + numb + ")"
numb++
status = false
} else {
status = true
}
} while (status == false);
const id = dataItem[i].id;
const update = await prisma.divisionDocumentFolderFile.update({
where: {
id
},
data: {
path,
name
}
})
}
return NextResponse.json({ success: true, message: "Berhasil memindahkan item" }, { status: 200 });
} catch (error) {
console.log(error);
return NextResponse.json({ success: false, message: "Gagal memindahkan item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
}
};
// COPY ITEM
export async function PUT(request: Request) {
try {
const user = await funGetUserByCookies()
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const { idDivision, path, dataItem } = (await request.json());
if (path != "home") {
const cekPath = await prisma.divisionDocumentFolderFile.count({
where: {
isActive: true,
id: path
}
})
if (cekPath == 0) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan path, data tidak ditemukan" }, { status: 404 });
}
}
for (let i = 0; i < dataItem.length; i++) {
let name = dataItem[i].name;
const category = dataItem[i].category;
const extension = dataItem[i].extension;
let status = false
let numb = 1
do {
const cekName = await prisma.divisionDocumentFolderFile.count({
where: {
path: path,
isActive: true,
extension,
name
}
})
if (cekName > 0) {
name = dataItem[i].name + " (" + numb + ")"
numb++
status = false
} else {
status = true
}
} while (status == false);
const create = await prisma.divisionDocumentFolderFile.create({
data: {
name,
path,
idDivision,
category,
extension,
createdBy: user.id
},
select: {
id: true
}
})
// let newPath = create.id
// let idPath = dataItem[i].id;
// let statusCek = false
// do {
// const cekFolder = await prisma.divisionDocumentFolderFile.findMany({
// where: {
// isActive: true,
// path: idPath
// }
// })
// if (cekFolder.length == 0) {
// statusCek = true
// } else {
// for (let index = 0; index < cekFolder.length; index++) {
// const addChildren = await prisma.divisionDocumentFolderFile.create({
// data: {
// name: cekFolder[index].name,
// path: newPath,
// idDivision,
// category: cekFolder[index].category,
// extension: cekFolder[index].extension,
// createdBy: user.id
// },
// select: {
// id: true
// }
// })
// newPath = create.id
// }
// }
// } while (statusCek == false);
}
return NextResponse.json({ success: true, message: "Berhasil salin item" }, { status: 200 });
} catch (error) {
console.log(error);
return NextResponse.json({ success: false, message: "Gagal salin item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
}
};

View File

@@ -17,6 +17,7 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const idDivision = searchParams.get("division");
const path = searchParams.get("path");
const category = searchParams.get("category");
const cekDivision = await prisma.division.count({
where: {
@@ -43,14 +44,24 @@ export async function GET(request: Request) {
}
let kondisi: any = {
isActive: true,
idDivision: String(idDivision),
path: (path == "undefined" || path == "null" || path == "" || path == null) ? "home" : path
}
if (category == "folder") {
kondisi = {
isActive: true,
idDivision: String(idDivision),
path: (path == "undefined" || path == "null" || path == "" || path == null) ? "home" : path,
category: "FOLDER"
}
}
const data = await prisma.divisionDocumentFolderFile.findMany({
where: {
isActive: true,
idDivision: String(idDivision),
path: (path == "undefined" || path == "null" || path == "" || path == null) ? "home" : path
},
where: kondisi,
select: {
id: true,
category: true,
@@ -78,7 +89,35 @@ export async function GET(request: Request) {
}))
return NextResponse.json({ success: true, message: "Berhasil mendapatkan item", data: allData, }, { status: 200 });
let pathNow = path
let jalur = []
if (path != "home" && path != "null" && path != "undefined" && path != "") {
do {
const dataPath = await prisma.divisionDocumentFolderFile.findUnique({
where: {
id: String(pathNow)
}
})
if (dataPath) {
const fix = {
id: String(pathNow),
name: dataPath.name,
}
jalur.push(fix)
pathNow = dataPath.path
} else {
pathNow = "home"
}
} while (pathNow != "home");
}
jalur.push({ id: 'home', name: 'home' })
jalur = [...jalur].reverse()
return NextResponse.json({ success: true, message: "Berhasil mendapatkan item", data: allData, jalur }, { status: 200 });
} catch (error) {
console.log(error);