upd: api ai user
Deskripsi: - list user - detail user No Issues
This commit is contained in:
75
src/app/api/ai/user/[id]/route.ts
Normal file
75
src/app/api/ai/user/[id]/route.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import _ from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
// GET ONE MEMBER / USER
|
||||
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params;
|
||||
|
||||
const users = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
nik: true,
|
||||
name: true,
|
||||
phone: true,
|
||||
email: true,
|
||||
gender: true,
|
||||
img: true,
|
||||
idGroup: true,
|
||||
isActive: true,
|
||||
idPosition: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
UserRole: {
|
||||
select: {
|
||||
name: true,
|
||||
id: true
|
||||
}
|
||||
},
|
||||
Position: {
|
||||
select: {
|
||||
name: true,
|
||||
id: true
|
||||
},
|
||||
},
|
||||
Group: {
|
||||
select: {
|
||||
name: true,
|
||||
id: true
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const { ...userData } = users;
|
||||
const group = users?.Group.name
|
||||
const position = users?.Position?.name
|
||||
const idUserRole = users?.UserRole.id
|
||||
const phone = '+62' + users?.phone
|
||||
const role = users?.UserRole.name
|
||||
const gender = users?.gender == "F" ? "Perempuan" : "Laki-Laki"
|
||||
|
||||
const result = { ...userData, gender, group, position, idUserRole, phone, role };
|
||||
|
||||
const omitData = _.omit(result, ["Group", "Position", "UserRole"]);
|
||||
|
||||
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan anggota",
|
||||
data: omitData,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan anggota, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
88
src/app/api/ai/user/route.ts
Normal file
88
src/app/api/ai/user/route.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import _ from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
// GET ALL MEMBER / USER
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const search = searchParams.get('search')
|
||||
const idVillage = searchParams.get("desa");
|
||||
const idGroup = searchParams.get("group");
|
||||
const active = searchParams.get("active");
|
||||
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,
|
||||
idVillage: String(idVillage),
|
||||
name: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive",
|
||||
},
|
||||
NOT: {
|
||||
idUserRole: 'developer'
|
||||
}
|
||||
}
|
||||
|
||||
if (idGroup != "null" && idGroup != undefined && idGroup != "" && idGroup != null) {
|
||||
kondisi = {
|
||||
...kondisi,
|
||||
idGroup: String(idGroup)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const users = await prisma.user.findMany({
|
||||
skip: dataSkip,
|
||||
take: getFix,
|
||||
where: kondisi,
|
||||
select: {
|
||||
id: true,
|
||||
idUserRole: true,
|
||||
isActive: true,
|
||||
nik: true,
|
||||
name: true,
|
||||
phone: true,
|
||||
Position: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
Group: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc'
|
||||
}
|
||||
});
|
||||
|
||||
const allData = users.map((v: any) => ({
|
||||
..._.omit(v, ["phone", "gender", "Group", "Position"]),
|
||||
gender: v.gender == "F" ? "Perempuan" : "Laki-Laki",
|
||||
phone: "+" + v.phone,
|
||||
group: v.Group.name,
|
||||
position: v?.Position?.name
|
||||
}))
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil member", data: allData }, { status: 200 });
|
||||
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan anggota, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user