Files
sistem-desa-mandiri/src/module/position/api/get/getAllPosition.ts
lukman ad94d0aaa0 feat : update search
Deskripsi:
- search group
- search position
- search member / user / anggota

No Issue
2024-08-07 10:37:04 +08:00

55 lines
1.3 KiB
TypeScript

import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import _, { omit } from "lodash";
import { NextRequest } from "next/server";
export async function getAllPosition(req: NextRequest) {
try {
let grupFix
const searchParams = req.nextUrl.searchParams
const groupID = searchParams.get('groupId');
const active = searchParams.get('active');
const name = searchParams.get('name')
const user = await funGetUserByCookies()
console.log(groupID)
if (groupID == "null") {
grupFix = user.idGroup
} else {
grupFix = groupID
}
const positions = await prisma.position.findMany({
where: {
idGroup: String(grupFix),
isActive: (active == "true" ? true : false),
name: {
contains: String(name),
mode: "insensitive"
}
},
select: {
id: true,
name: true,
isActive: true,
Group: {
select: {
name: true
}
}
},
});
const allData = positions.map((v: any) => ({
..._.omit(v, ["Group"]),
group: v.Group.name
}))
return Response.json(allData);
} catch (error) {
console.error(error);
return Response.json({ success: false, message: "Internal Server Error" }, { status: 500 });
}
}