diff --git a/src/app/api/mobile/discussion/[id]/route.ts b/src/app/api/mobile/discussion/[id]/route.ts index 07d9633..ecfe8e2 100644 --- a/src/app/api/mobile/discussion/[id]/route.ts +++ b/src/app/api/mobile/discussion/[id]/route.ts @@ -1,4 +1,4 @@ -import { countTime, prisma } from "@/module/_global"; +import { countTime, DIR, funUploadFile, prisma } from "@/module/_global"; import { funGetUserById } from "@/module/auth"; import { createLogUserMobile } from "@/module/user"; import _ from "lodash"; @@ -227,7 +227,13 @@ export async function PUT(request: Request, context: { params: { id: string } }) export async function POST(request: Request, context: { params: { id: string } }) { try { const { id } = context.params - const { title, desc, user } = (await request.json()) + const body = await request.formData() + const dataBody = body.get("data") + const cekFile = body.has("file0") + + // const { title, desc, user } = (await request.json()) + const { title, desc, user, oldFile } = JSON.parse(dataBody as string) + const userMobile = await funGetUserById({ id: String(user) }) @@ -254,6 +260,41 @@ export async function POST(request: Request, context: { params: { id: string } } } }); + if (oldFile.length > 0) { + for (let index = 0; index < oldFile.length; index++) { + const element = oldFile[index]; + if (element.delete) { + await prisma.divisionDiscussionFile.delete({ + where: { + id: element.id + } + }) + } + } + } + + if (cekFile) { + body.delete("data") + for (var pair of body.entries()) { + if (String(pair[0]).substring(0, 4) == "file") { + const file = body.get(pair[0]) as File + const fExt = file.name.split(".").pop() + const fName = decodeURIComponent(file.name.replace("." + fExt, "")) + const upload = await funUploadFile({ file: file, dirId: DIR.discussionDivision }) + if (upload.success) { + await prisma.divisionDiscussionFile.create({ + data: { + idStorage: upload.data.id, + idDiscussion: id, + name: fName, + extension: String(fExt) + } + }) + } + } + } + } + // create log user const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data diskusi', table: 'divisionDisscussion', data: id, user: userMobile.id }) return NextResponse.json({ success: true, message: "Berhasil mengedit diskusi" }, { status: 200 }); diff --git a/src/app/api/mobile/discussion/route.ts b/src/app/api/mobile/discussion/route.ts index f8d8d27..4f2d157 100644 --- a/src/app/api/mobile/discussion/route.ts +++ b/src/app/api/mobile/discussion/route.ts @@ -1,4 +1,3 @@ -import { DivisionDiscussionFile } from './../../../../../node_modules/.prisma/client/index.d'; import { DIR, funSendWebPush, funUploadFile, prisma } from "@/module/_global"; import { funGetUserById } from "@/module/auth"; import { createLogUserMobile } from "@/module/user";