feat: add post endpoint for complex criteria

refactor: also accept boolean for queryOrNot fn
This commit is contained in:
Methapon Metanipat 2024-11-11 13:08:49 +07:00
parent 773c7f97d8
commit 00ce2abc7e
2 changed files with 92 additions and 1 deletions

View file

@ -267,6 +267,97 @@ export class EmployeeController extends Controller {
};
}
@Post("list")
@Security("keycloak")
async listByCriteria(
@Request() req: RequestWithUser,
@Query() zipCode?: string,
@Query() gender?: string,
@Query() status?: Status,
@Query() visa?: boolean,
@Query() passport?: boolean,
@Query() customerId?: string,
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
@Body()
body?: {
query?: string;
passport?: string[];
},
) {
const where = {
OR:
!!query || !!body
? [
...(queryOrNot<Prisma.EmployeeWhereInput[]>(query, [
{
employeePassport: {
some: { number: { contains: query } },
},
},
{ firstName: { contains: query } },
{ firstNameEN: { contains: query } },
{ lastName: { contains: query } },
{ lastNameEN: { contains: query } },
...whereAddressQuery(query),
]) ?? []),
...(queryOrNot<Prisma.EmployeeWhereInput[]>(!!body, [
{
employeePassport: {
some: { number: { contains: query } },
},
},
]) ?? []),
]
: undefined,
AND: {
...filterStatus(status),
customerBranch: {
customerId,
customer: isSystem(req.user)
? undefined
: {
registeredBranch: {
OR: permissionCond(req.user),
},
},
},
subDistrict: zipCode ? { zipCode } : undefined,
gender,
},
} satisfies Prisma.EmployeeWhereInput;
const [result, total] = await prisma.$transaction([
prisma.employee.findMany({
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
include: {
employeePassport: passport ? { orderBy: { expireDate: "desc" } } : undefined,
employeeVisa: visa ? { orderBy: { expireDate: "desc" } } : undefined,
province: true,
district: true,
subDistrict: true,
customerBranch: {
include: { customer: true },
},
createdBy: true,
updatedBy: true,
},
where,
take: pageSize,
skip: (page - 1) * pageSize,
}),
prisma.employee.count({ where }),
]);
return {
result,
page,
pageSize,
total,
};
}
@Get("{employeeId}")
@Security("keycloak")
async getById(@Path() employeeId: string) {

View file

@ -28,6 +28,6 @@ export function whereAddressQuery(query: string) {
];
}
export function queryOrNot<T>(query: string, where: T): T | undefined {
export function queryOrNot<T>(query: string | boolean, where: T): T | undefined {
return !!query ? where : undefined;
}