Merge pull request 'upd : api mobile' (#4) from amalia/04-agustus-25 into join

Reviewed-on: bip/sistem-desa-mandiri#4
This commit is contained in:
2025-08-04 14:16:35 +08:00

View File

@@ -10,8 +10,6 @@ export async function POST(request: Request) {
try { try {
const body = await request.formData() const body = await request.formData()
const dataBody = body.get("data") 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 { idPath, idDivision, user } = JSON.parse(dataBody as string)
@@ -57,16 +55,19 @@ export async function POST(request: Request) {
..._.omit(v, [""]), ..._.omit(v, [""]),
file: v.name + '.' + v.extension, file: v.name + '.' + v.extension,
})) }))
let failed = 0
const cek = dataOmit.some((i: any) => i.file == fileName) body.delete("data")
for (var pair of body.entries()) {
if (cek) { if (String(pair[0]).substring(0, 4) == "file") {
return NextResponse.json({ success: false, message: "Terdapat file dengan nama yang sama" }, { status: 200 }); const file = body.get(pair[0]) as File
} const fileName = file.name
const fExt = file.name.split(".").pop() const fExt = file.name.split(".").pop()
const fName = file.name.replace("." + fExt, "") let fName = file.name.replace("." + fExt, "")
const cek = dataOmit.some((i: any) => i.file == fileName)
if (cek) {
const random = Math.floor(Math.random() * 1000)
fName = `${fName}_${random}`
}
const upload = await funUploadFile({ file: file, dirId: DIR.document }) const upload = await funUploadFile({ file: file, dirId: DIR.document })
if (upload.success) { if (upload.success) {
const dataInsert = await prisma.divisionDocumentFolderFile.create({ const dataInsert = await prisma.divisionDocumentFolderFile.create({
@@ -86,10 +87,16 @@ export async function POST(request: Request) {
// create log user // create log user
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User mengupload file baru', table: 'divisionDocumentFolderFile', data: dataInsert.id, user: userMobile.id }) 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 { } else {
return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti" }, { status: 200 }); failed++
} }
}
}
if (failed > 0) {
return NextResponse.json({ success: false, message: "Beberapa file gagal diupload", failed }, { status: 200 });
}
return NextResponse.json({ success: true, message: "Berhasil upload file" }, { status: 200 });
} catch (error) { } catch (error) {
console.error(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 }); return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });