diff --git a/src/app/api/mobile/document/more/route.ts b/src/app/api/mobile/document/more/route.ts new file mode 100644 index 0000000..4cab41f --- /dev/null +++ b/src/app/api/mobile/document/more/route.ts @@ -0,0 +1,299 @@ +import { DIR, funCopyFile, prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; +import _ from "lodash"; +import moment from "moment"; +import "moment/locale/id"; +import { NextResponse } from "next/server"; + + +// MOVE ITEM +export async function POST(request: Request) { + try { + const { path, dataItem, user } = (await request.json()); + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + 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: 200 }); + } + } + + 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 + } + }) + } + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User memindahkan file atau folder', table: 'divisionDocumentFolderFile', data: '', user: userMobile.id }) + + + return NextResponse.json({ success: true, message: "Berhasil memindahkan item" }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal memindahkan item, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; + + +// COPY ITEM +export async function PUT(request: Request) { + try { + const { idDivision, path, dataItem, user } = (await request.json()); + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + 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: 200 }); + } + } + + for (let i = 0; i < dataItem.length; i++) { + let name = dataItem[i].name; + const category = dataItem[i].category; + const extension = dataItem[i].extension; + const idStorage = dataItem[i].idStorage; + + const copyOnStorage = await funCopyFile({ fileId: idStorage, dirId: DIR.document }) + if (copyOnStorage.success) { + 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, + idStorage: copyOnStorage.data.id, + createdBy: user.id + }, + select: { + id: true + } + }) + + // create log user + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menyalin file', table: 'divisionDocumentFolderFile', data: create.id, user: userMobile.id }) + } + } + + return NextResponse.json({ success: true, message: "Berhasil salin item" }, { status: 200 }); + + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal salin item, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; + + +// SHARE ITEM +export async function DELETE(request: Request) { + try { + const { dataDivision, dataItem, user } = (await request.json()); + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + + for (let i = 0; i < dataItem.length; i++) { + const del = await prisma.divisionDocumentShare.deleteMany({ + where: { + idDocument: dataItem[i].id + } + }) + + const omitData = dataDivision.map((v: any) => ({ + ..._.omit(v, ["name", "id"]), + idDivision: v.id, + idDocument: dataItem[i].id + })) + + const insert = await prisma.divisionDocumentShare.createMany({ + data: omitData + }) + + // create log user + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membagikan item', table: 'divisionDocumentShare', data: dataItem[i].id, user: userMobile.id }) + } + + return NextResponse.json({ success: true, message: "Berhasil membagikan item" }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal membagikan item, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; + +// GET INFO ITEM +export async function GET(request: Request) { + try { + const { searchParams } = new URL(request.url); + const idItem = searchParams.get("item"); + const category = searchParams.get("cat"); + const user = searchParams.get("user"); + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + + const cekItem = await prisma.divisionDocumentFolderFile.count({ + where: { + id: String(idItem), + } + }) + + if (cekItem == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan dokumen, data tidak ditemukan" }, { status: 200 }); + } + + let fixData + if (category == 'share') { + const share = await prisma.divisionDocumentShare.findMany({ + where: { + idDocument: String(idItem), + isActive: true + }, + select: { + idDivision: true, + Division: { + select: { + name: true + } + } + } + }) + + fixData = share.map((v: any) => ({ + ..._.omit(v, ["idDivision", "Division"]), + id: v.idDivision, + name: v.Division.name + })) + + } else { + const data = await prisma.divisionDocumentFolderFile.findUnique({ + where: { + id: String(idItem), + }, + select: { + category: true, + name: true, + extension: true, + createdAt: true, + path: true, + Division: { + select: { + name: true + } + }, + User: { + select: { + name: true + } + } + } + }) + + const dataPath = await prisma.divisionDocumentFolderFile.findUnique({ + where: { + id: data?.path + } + }) + + + fixData = { + category: data?.category, + name: data?.name, + extension: data?.extension, + createdAt: moment(data?.createdAt).format('DD MMMM YYYY'), + path: (dataPath?.name !== undefined && dataPath?.name !== null && dataPath?.name !== '') ? dataPath.name : "home", + division: data?.Division?.name, + createdBy: data?.User?.name + } + } + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan item", data: fixData }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan item, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/document/route.ts b/src/app/api/mobile/document/route.ts new file mode 100644 index 0000000..d405f1b --- /dev/null +++ b/src/app/api/mobile/document/route.ts @@ -0,0 +1,408 @@ +import { prisma } from "@/module/_global"; +import { funGetUserByCookies, funGetUserById } from "@/module/auth"; +import { createLogUser, createLogUserMobile } from "@/module/user"; +import _ from "lodash"; +import moment from "moment"; +import { NextResponse } from "next/server"; + + +// GET ALL DOCUMENT +export async function GET(request: Request) { + try { + const { searchParams } = new URL(request.url); + const idDivision = searchParams.get("division"); + const path = searchParams.get("path"); + const category = searchParams.get("category"); + const user = searchParams.get("user"); + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + const cekDivision = await prisma.division.count({ + where: { + id: String(idDivision), + // isActive: true + } + }) + + if (cekDivision == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 200 }); + } + + let statusAkses = false + let aksesPath = String(path) + if (path != "home" && path != "null" && path != "undefined" && path != "") { + const cekPath = await prisma.divisionDocumentFolderFile.count({ + where: { + isActive: true, + id: String(path), + idDivision: String(idDivision) + } + }) + + const cekSharePath = await prisma.divisionDocumentShare.count({ + where: { + isActive: true, + idDivision: String(idDivision), + idDocument: String(path), + DivisionDocumentFolderFile: { + isActive: true + } + } + }) + + if (cekPath == 0 && cekSharePath == 0) { + do { + const dataPath = await prisma.divisionDocumentFolderFile.findUnique({ + where: { + id: String(aksesPath) + } + }) + + if (dataPath) { + const cekShare = await prisma.divisionDocumentShare.count({ + where: { + isActive: true, + idDivision: String(idDivision), + idDocument: String(aksesPath) + } + }) + if (cekShare == 0) { + statusAkses = false + aksesPath = dataPath.path + } else { + statusAkses = true + } + + } else { + aksesPath = "home" + } + } while (aksesPath != "home" && statusAkses == false); + + if (statusAkses == false) { + return NextResponse.json({ success: false, message: "Data tidak ditemukan / tidak memilik hak akses" }, { status: 200 }); + } + } + } + + + let kondisi: any = { + isActive: true, + idDivision: String(idDivision), + path: (path == "undefined" || path == "null" || path == "" || path == null) ? "home" : path + } + + let formatDataShare: any[] = []; + + if (category == "folder") { + kondisi = { + isActive: true, + idDivision: String(idDivision), + path: (path == "undefined" || path == "null" || path == "" || path == null) ? "home" : path, + category: "FOLDER" + } + } else { + if (path == "home" || path == "null" || path == "undefined") { + const dataShare = await prisma.divisionDocumentShare.findMany({ + where: { + isActive: true, + idDivision: String(idDivision), + DivisionDocumentFolderFile: { + isActive: true + } + }, + select: { + DivisionDocumentFolderFile: { + select: { + idStorage: true, + id: true, + category: true, + name: true, + extension: true, + path: true, + User: { + select: { + name: true + } + }, + createdAt: true, + updatedAt: true + } + } + }, + orderBy: { + DivisionDocumentFolderFile: { + createdAt: 'desc' + } + } + }) + + formatDataShare = dataShare.map((v: any) => ({ + ..._.omit(v, ["DivisionDocumentFolderFile"]), + idStorage: v.DivisionDocumentFolderFile.idStorage, + id: v.DivisionDocumentFolderFile.id, + category: v.DivisionDocumentFolderFile.category, + name: v.DivisionDocumentFolderFile.name, + extension: v.DivisionDocumentFolderFile.extension, + path: v.DivisionDocumentFolderFile.path, + createdBy: v.DivisionDocumentFolderFile.User.name, + createdAt: v.DivisionDocumentFolderFile.createdAt, + updatedAt: v.DivisionDocumentFolderFile.updatedAt, + share: true + })) + + } else { + kondisi = { + isActive: true, + path: (path == "undefined" || path == "null" || path == null) ? "home" : path + } + } + } + + + const data = await prisma.divisionDocumentFolderFile.findMany({ + where: kondisi, + select: { + id: true, + category: true, + name: true, + extension: true, + idStorage: true, + path: true, + User: { + select: { + name: true + } + }, + createdAt: true, + updatedAt: true + }, + orderBy: { + createdAt: 'desc' + } + }) + + const allData = data.map((v: any) => ({ + ..._.omit(v, ["User", "createdAt", "updatedAt"]), + createdBy: v.User.name, + createdAt: v.createdAt, + updatedAt: v.updatedAt, + share: false + })) + + if (formatDataShare.length > 0) { + allData.push(...formatDataShare) + } + + const formatData = _.orderBy(allData, ['category', 'createdAt'], ['desc', 'desc']); + + const fixData = formatData.map((v: any) => ({ + ..._.omit(v, ["createdAt", "updatedAt"]), + createdAt: moment(v.createdAt).format("DD-MM-YYYY HH:mm"), + updatedAt: moment(v.updatedAt).format("DD-MM-YYYY HH:mm"), + })) + + 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 && (dataPath.idDivision == idDivision || path == pathNow || pathNow == aksesPath)) { + 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: fixData, jalur }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan item, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + + +// CREATE FOLDER +export async function POST(request: Request) { + try { + const { name, path, idDivision, user } = (await request.json()); + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + const cekDivision = await prisma.division.count({ + where: { + id: String(idDivision), + isActive: true + } + }) + + if (cekDivision == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 200 }); + } + + 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: 200 }); + } + } + + const nameFile = await prisma.divisionDocumentFolderFile.count({ + where: { + name, + idDivision, + path, + extension: "folder", + category: "FOLDER", + isActive: true + } + }) + + + if (nameFile > 0) { + return NextResponse.json({ success: false, message: "Gagal membuat folder baru, folder sudah ada" }, { status: 200 }); + } + + const data = await prisma.divisionDocumentFolderFile.create({ + data: { + name, + path, + idDivision, + category: "FOLDER", + extension: "folder", + createdBy: user.id, + }, + select: { + id: true + } + }); + + // create log user + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat folder baru', table: 'divisionDocumentFolderFile', data: data.id, user: userMobile.id }) + + return NextResponse.json({ success: true, message: "Berhasil membuat folder baru" }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal membuat folder, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; + + +// RENAME ITEM +export async function PUT(request: Request) { + try { + const { name, id, path, idDivision, extension, user } = (await request.json()); + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + const cekFile = await prisma.divisionDocumentFolderFile.count({ + where: { + id: id, + isActive: true + } + }) + + if (cekFile == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan item, data tidak ditemukan" }, { status: 200 }); + } + + const nameFile = await prisma.divisionDocumentFolderFile.count({ + where: { + name, + idDivision, + path, + extension, + isActive: true, + NOT: { + id: id + }, + } + }) + + + if (nameFile > 0) { + return NextResponse.json({ success: false, message: "Gagal mengubah nama item, item sudah ada" }, { status: 200 }); + } + + const update = await prisma.divisionDocumentFolderFile.update({ + where: { + id: id + }, + data: { + name, + } + }) + + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengubah nama file atau folder', table: 'divisionDocumentFolderFile', data: id, user: userMobile.id }) + + + return NextResponse.json({ success: true, message: "Berhasil mengubah nama item" }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengubah nama item (error: 500), coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); + } +}; + + +// DELETE ITEM +export async function DELETE(request: Request) { + try { + const data = await request.json() + + for (let i = 0; i < data.length; i++) { + const id = data[i].id; + const cekFile = await prisma.divisionDocumentFolderFile.update({ + where: { + id: id + }, + data: { + isActive: false + } + }) + } + + // create log user + // const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus file atau folder', table: 'divisionDocumentFolderFile', data: '', user: userMobile.id }) + + + return NextResponse.json({ success: true, message: "Berhasil menghapus item" }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menghapus item, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; \ No newline at end of file diff --git a/src/app/api/mobile/document/upload/route.ts b/src/app/api/mobile/document/upload/route.ts new file mode 100644 index 0000000..fbb3963 --- /dev/null +++ b/src/app/api/mobile/document/upload/route.ts @@ -0,0 +1,97 @@ +import { DIR, funUploadFile, prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; +import _ from "lodash"; +import { NextResponse } from "next/server"; + + +// UPLOAD FILE +export async function POST(request: Request) { + try { + const body = await request.formData() + const dataBody = body.get("data") + const file = body.get("file") as File + const fileName = file.name + + const { idPath, idDivision, user } = JSON.parse(dataBody as string) + + const userMobile = await funGetUserById({ id: String(user) }) + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + + const cekDivision = await prisma.division.count({ + where: { + id: String(idDivision), + isActive: true + } + }) + + if (cekDivision == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 200 }); + } + + if (idPath != "home") { + const cekPath = await prisma.divisionDocumentFolderFile.count({ + where: { + isActive: true, + id: idPath + } + }) + + if (cekPath == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan path, data tidak ditemukan" }, { status: 200 }); + } + } + + const nameFile = await prisma.divisionDocumentFolderFile.findMany({ + where: { + idDivision, + path: idPath, + category: "FILE", + isActive: true + } + }) + + const dataOmit = nameFile.map((v: any) => ({ + ..._.omit(v, [""]), + file: v.name + '.' + v.extension, + })) + + const cek = dataOmit.some((i: any) => i.file == fileName) + + if (cek) { + return NextResponse.json({ success: false, message: "Terdapat file dengan nama yang sama" }, { status: 200 }); + } + + + const fExt = file.name.split(".").pop() + const fName = file.name.replace("." + fExt, "") + const upload = await funUploadFile({ file: file, dirId: DIR.document }) + if (upload.success) { + const dataInsert = await prisma.divisionDocumentFolderFile.create({ + data: { + name: fName, + path: idPath, + idDivision, + category: "FILE", + extension: String(fExt), + createdBy: userMobile.id, + idStorage: upload.data.id + }, + select: { + id: true + } + }); + + // create log user + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User mengupload file baru', table: 'divisionDocumentFolderFile', data: dataInsert.id, user: userMobile.id }) + return NextResponse.json({ success: true, message: "Berhasil upload file" }, { status: 200 }); + } else { + return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti" }, { status: 200 }); + } + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; \ No newline at end of file