upd: api ai
Deskripsi: - list divisi - detail divisi No Issues
This commit is contained in:
63
src/app/api/ai/division/[id]/route.ts
Normal file
63
src/app/api/ai/division/[id]/route.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import { prisma } from "@/module/_global";
|
||||||
|
import _ from "lodash";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
|
||||||
|
// GET ONE DATA DIVISI :: UNTUK TAMPIL DATA DI HALAMAN EDIT DAN INFO
|
||||||
|
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||||
|
try {
|
||||||
|
const { id } = context.params;
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const idVillage = searchParams.get("desa");
|
||||||
|
|
||||||
|
const data = await prisma.division.findUnique({
|
||||||
|
where: {
|
||||||
|
id: String(id),
|
||||||
|
idVillage: String(idVillage)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!data) {
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const member = await prisma.divisionMember.findMany({
|
||||||
|
where: {
|
||||||
|
idDivision: String(id),
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
isAdmin: true,
|
||||||
|
idUser: true,
|
||||||
|
User: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
img: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
isAdmin: 'desc',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const fixMember = member.map((v: any) => ({
|
||||||
|
..._.omit(v, ["User"]),
|
||||||
|
name: v.User.name,
|
||||||
|
img: v.User.img
|
||||||
|
}))
|
||||||
|
|
||||||
|
const dataFix = {
|
||||||
|
...data,
|
||||||
|
member: fixMember
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: dataFix, }, { status: 200 });
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
84
src/app/api/ai/division/route.ts
Normal file
84
src/app/api/ai/division/route.ts
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import { prisma } from "@/module/_global";
|
||||||
|
import _ from "lodash";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
|
||||||
|
// GET ALL DATA DIVISI == LIST DATA DIVISI
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
try {
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const idVillage = searchParams.get("desa");
|
||||||
|
const idGroup = searchParams.get("group");
|
||||||
|
const name = searchParams.get('search');
|
||||||
|
const page = searchParams.get('page');
|
||||||
|
const active = searchParams.get("active");
|
||||||
|
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,
|
||||||
|
idVillage: String(idVillage),
|
||||||
|
name: {
|
||||||
|
contains: (name == undefined || name == "null") ? "" : name,
|
||||||
|
mode: "insensitive"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (idGroup != "null" && idGroup != undefined && idGroup != "") {
|
||||||
|
kondisi = {
|
||||||
|
...kondisi,
|
||||||
|
idGroup: String(idGroup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const data = await prisma.division.findMany({
|
||||||
|
skip: dataSkip,
|
||||||
|
take: getFix,
|
||||||
|
where: kondisi,
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
desc: true,
|
||||||
|
idGroup: true,
|
||||||
|
Group: {
|
||||||
|
select: {
|
||||||
|
name: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
DivisionMember: {
|
||||||
|
where: {
|
||||||
|
isActive: true
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
idUser: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'desc'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const allData = data.map((v: any) => ({
|
||||||
|
..._.omit(v, ["DivisionMember", "Group"]),
|
||||||
|
group: v.Group.name,
|
||||||
|
jumlahMember: v.DivisionMember.length,
|
||||||
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: allData }, { status: 200 });
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user