From d1a591a63a5a21f0bc4f608def82aa20ddc1a928 Mon Sep 17 00:00:00 2001 From: amaliadwiy Date: Tue, 7 Oct 2025 12:01:19 +0800 Subject: [PATCH] upd: api mobile Deskripsi: - check nama divisi No Issues --- src/app/api/mobile/division/route.ts | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/app/api/mobile/division/route.ts b/src/app/api/mobile/division/route.ts index 48dfef7..9b6b538 100644 --- a/src/app/api/mobile/division/route.ts +++ b/src/app/api/mobile/division/route.ts @@ -342,4 +342,45 @@ export async function POST(request: Request) { console.error(error); return NextResponse.json({ success: false, message: "Gagal menambahkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); } +}; + + +// CEK DATA DIVISI (NAME DI DESA DAN GROUP YG SAMA) +export async function PUT(request: Request) { + try { + + const sent = (await request.json()) + const user = sent.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 }); + } + + let fixGroup + if (sent.data.idGroup == "null" || sent.data.idGroup == undefined || sent.data.idGroup == "") { + fixGroup = userMobile.idGroup + } else { + fixGroup = sent.data.idGroup + } + + const checkData = await prisma.division.count({ + where: { + name: { + equals: sent.data.name, + mode: "insensitive" + }, + idGroup: fixGroup, + idVillage: String(userMobile.idVillage) + } + }) + + if (checkData > 0) { + return NextResponse.json({ success: true, message: "Divisi dengan nama ini sudah ada", available: false }, { status: 200 }); + } + + return NextResponse.json({ success: true, message: "Berhasil cek data divisi", available: true }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menambahkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } }; \ No newline at end of file