feat: add count gender by query

This commit is contained in:
Methapon2001 2024-07-02 09:06:05 +07:00
parent bd00bcbbe1
commit d23e404196

View file

@ -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<Record<string, number>>((a, c) => {