From fbfdc21b9dfa1646ef2e239885746ba12613a810 Mon Sep 17 00:00:00 2001 From: amal Date: Fri, 19 Sep 2025 11:42:45 +0800 Subject: [PATCH] upd: api ai positision Deskripsi: - list position No Issues --- src/app/api/ai/position/route.ts | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/app/api/ai/position/route.ts diff --git a/src/app/api/ai/position/route.ts b/src/app/api/ai/position/route.ts new file mode 100644 index 0000000..3aaca1f --- /dev/null +++ b/src/app/api/ai/position/route.ts @@ -0,0 +1,79 @@ +import { prisma } from "@/module/_global"; +import _ from "lodash"; +import { NextResponse } from "next/server"; + + +// GET ALL POSITION +export async function GET(request: Request) { + try { + const { searchParams } = new URL(request.url); + const idVillage = searchParams.get("desa"); + const idGroup = searchParams.get("group"); + const active = searchParams.get('active'); + const search = searchParams.get('search') + const page = searchParams.get('page') + const get = searchParams.get('get') + + let getFix = 10; + if (get == null || get == undefined || get == "" || _.isNaN(Number(get))) { + getFix = 10; + } else { + getFix = Number(get); + } + + const dataSkip = page == null || page == undefined ? 0 : Number(page) * getFix - getFix; + + let kondisi: any = { + isActive: active == 'false' ? false : true, + Group: { + idVillage: String(idVillage) + }, + name: { + contains: (search == undefined || search == null) ? "" : search, + mode: "insensitive" + } + } + + if (idGroup != "null" && idGroup != undefined && idGroup != "") { + kondisi = { + ...kondisi, + idGroup: String(idGroup) + } + } + + + + const positions = await prisma.position.findMany({ + skip: dataSkip, + take: getFix, + where: kondisi, + select: { + id: true, + name: true, + idGroup: true, + isActive: true, + createdAt: true, + updatedAt: true, + Group: { + select: { + name: true + } + } + }, + orderBy: { + name: 'asc' + } + }); + + const allData = positions.map((v: any) => ({ + ..._.omit(v, ["Group"]), + group: v.Group.name + })) + + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan jabatan", data: allData }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file