diff --git a/src/controllers/03-employee-controller.ts b/src/controllers/03-employee-controller.ts index 67b2f3b..57d102a 100644 --- a/src/controllers/03-employee-controller.ts +++ b/src/controllers/03-employee-controller.ts @@ -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(query, [ + { + employeePassport: { + some: { number: { contains: query } }, + }, + }, + { firstName: { contains: query } }, + { firstNameEN: { contains: query } }, + { lastName: { contains: query } }, + { lastNameEN: { contains: query } }, + ...whereAddressQuery(query), + ]) ?? []), + ...(queryOrNot(!!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) { diff --git a/src/utils/relation.ts b/src/utils/relation.ts index fde2c6d..20290d2 100644 --- a/src/utils/relation.ts +++ b/src/utils/relation.ts @@ -28,6 +28,6 @@ export function whereAddressQuery(query: string) { ]; } -export function queryOrNot(query: string, where: T): T | undefined { +export function queryOrNot(query: string | boolean, where: T): T | undefined { return !!query ? where : undefined; }