From d23e40419612c36e3e5882e93bd3dbfee08721f2 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Jul 2024 09:06:05 +0700 Subject: [PATCH] feat: add count gender by query --- src/controllers/employee-controller.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/controllers/employee-controller.ts b/src/controllers/employee-controller.ts index e46973b..eb3f62b 100644 --- a/src/controllers/employee-controller.ts +++ b/src/controllers/employee-controller.ts @@ -210,12 +210,31 @@ export class EmployeeController extends Controller { } @Get("stats/gender") - async getEmployeeStatsGender(@Query() customerBranchId?: string) { + async getEmployeeStatsGender( + @Query() customerBranchId?: string, + @Query() status?: Status, + @Query() query: string = "", + ) { + const filterStatus = (val?: Status) => { + if (!val) return {}; + + return val !== Status.CREATED && val !== Status.ACTIVE + ? { status: val } + : { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] }; + }; + return await prisma.employee .groupBy({ _count: true, by: ["gender"], - where: { customerBranchId }, + where: { + OR: [ + { firstName: { contains: query }, customerBranchId, ...filterStatus(status) }, + { firstNameEN: { contains: query }, customerBranchId, ...filterStatus(status) }, + { lastName: { contains: query }, customerBranchId, ...filterStatus(status) }, + { lastNameEN: { contains: query }, customerBranchId, ...filterStatus(status) }, + ], + }, }) .then((res) => res.reduce>((a, c) => {