From 927f972a916e3f327c3dc9b3049a62a86aca390b Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Thu, 14 Nov 2024 17:43:13 +0700 Subject: [PATCH] fix: empty query result in empty array --- .../00-employment-office-controller.ts | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/controllers/00-employment-office-controller.ts b/src/controllers/00-employment-office-controller.ts index 22e55db..4aef059 100644 --- a/src/controllers/00-employment-office-controller.ts +++ b/src/controllers/00-employment-office-controller.ts @@ -10,25 +10,30 @@ export class EmploymentOfficeController extends Controller { async getEmploymentOfficeList(@Query() districtId?: string, @Query() query: string = "") { return await prisma.employmentOffice.findMany({ where: { - OR: [ - ...(districtId + OR: + !!districtId || !!query ? [ - { - province: { - district: { some: { id: districtId } }, - }, - district: { none: {} }, - }, - { - district: { - some: { districtId }, - }, - }, + ...(districtId + ? [ + { + province: { + district: { some: { id: districtId } }, + }, + district: { none: {} }, + }, + { + district: { + some: { districtId }, + }, + }, + ] + : []), + ...(queryOrNot(query, [ + { name: { contains: query } }, + { nameEN: { contains: query } }, + ]) ?? []), ] - : []), - ...(queryOrNot(query, [{ name: { contains: query } }, { nameEN: { contains: query } }]) ?? - []), - ], + : undefined, }, orderBy: [{ provinceId: "asc" }, { id: "asc" }], });