diff --git a/src/controllers/01-branch-controller.ts b/src/controllers/01-branch-controller.ts index 4a6b5f7..c1f741c 100644 --- a/src/controllers/01-branch-controller.ts +++ b/src/controllers/01-branch-controller.ts @@ -34,7 +34,12 @@ import { createPermCondition, } from "../services/permission"; import { filterStatus } from "../services/prisma"; -import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation"; +import { + connectOrDisconnect, + connectOrNot, + queryOrNot, + whereAddressQuery, +} from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; if (!process.env.MINIO_BUCKET) { @@ -252,7 +257,7 @@ export class BranchController extends Controller { NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined }, OR: permissionCond(req.user, withHead), }, - OR: [ + OR: queryOrNot(query, [ { code: { contains: query, mode: "insensitive" } }, { nameEN: { contains: query } }, { name: { contains: query } }, @@ -273,7 +278,7 @@ export class BranchController extends Controller { }, }, }, - ], + ]), } satisfies Prisma.BranchWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/01-branch-user-controller.ts b/src/controllers/01-branch-user-controller.ts index 7e92692..2b0ec23 100644 --- a/src/controllers/01-branch-user-controller.ts +++ b/src/controllers/01-branch-user-controller.ts @@ -18,6 +18,7 @@ import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; import { branchRelationPermInclude, createPermCheck } from "../services/permission"; +import { queryOrNot } from "../utils/relation"; const MANAGE_ROLES = ["system", "head_of_admin", "admin", "branch_manager"]; @@ -102,7 +103,10 @@ export class UserBranchController extends Controller { branch: { subDistrict: { zipCode } }, userId, }, - OR: [{ branch: { name: { contains: query } } }, { branch: { nameEN: { contains: query } } }], + OR: queryOrNot(query, [ + { branch: { name: { contains: query } } }, + { branch: { nameEN: { contains: query } } }, + ]), } satisfies Prisma.BranchUserWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/02-user-controller.ts b/src/controllers/02-user-controller.ts index 9cdad25..f624f67 100644 --- a/src/controllers/02-user-controller.ts +++ b/src/controllers/02-user-controller.ts @@ -44,7 +44,12 @@ import { createPermCheck, createPermCondition, } from "../services/permission"; -import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation"; +import { + connectOrDisconnect, + connectOrNot, + queryOrNot, + whereAddressQuery, +} from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { retry } from "../utils/func"; @@ -259,7 +264,7 @@ export class UserController extends Controller { @Query() status?: Status, ) { const where = { - OR: [ + OR: queryOrNot(query, [ { code: { contains: query, mode: "insensitive" } }, { firstName: { contains: query } }, { firstNameEN: { contains: query } }, @@ -268,7 +273,7 @@ export class UserController extends Controller { { email: { contains: query } }, { telephoneNo: { contains: query } }, ...whereAddressQuery(query), - ], + ]), AND: { userRole: { not: "system" }, userType, diff --git a/src/controllers/03-customer-branch-controller.ts b/src/controllers/03-customer-branch-controller.ts index 1afa7ed..7200d19 100644 --- a/src/controllers/03-customer-branch-controller.ts +++ b/src/controllers/03-customer-branch-controller.ts @@ -24,7 +24,12 @@ import { createPermCondition, } from "../services/permission"; import { filterStatus } from "../services/prisma"; -import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation"; +import { + connectOrDisconnect, + connectOrNot, + queryOrNot, + whereAddressQuery, +} from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { deleteFile, @@ -189,19 +194,17 @@ export class CustomerBranchController extends Controller { @Query() pageSize: number = 30, ) { const where = { - OR: query - ? [ - { registerName: { contains: query } }, - { registerNameEN: { contains: query } }, - { email: { contains: query } }, - { code: { contains: query } }, - { firstName: { contains: query } }, - { firstNameEN: { contains: query } }, - { lastName: { contains: query } }, - { lastNameEN: { contains: query } }, - ...whereAddressQuery(query), - ] - : undefined, + OR: queryOrNot(query, [ + { registerName: { contains: query } }, + { registerNameEN: { contains: query } }, + { email: { contains: query } }, + { code: { contains: query } }, + { firstName: { contains: query } }, + { firstNameEN: { contains: query } }, + { lastName: { contains: query } }, + { lastNameEN: { contains: query } }, + ...whereAddressQuery(query), + ]), AND: { customer: isSystem(req.user) ? { registeredBranchId } @@ -273,13 +276,13 @@ export class CustomerBranchController extends Controller { @Query() pageSize: number = 30, ) { const where = { - OR: [ + OR: queryOrNot(query, [ { firstName: { contains: query } }, { firstNameEN: { contains: query } }, { lastName: { contains: query } }, { lastNameEN: { contains: query } }, ...whereAddressQuery(query), - ], + ]), AND: { ...filterStatus(status), customerBranchId: branchId, diff --git a/src/controllers/03-customer-controller.ts b/src/controllers/03-customer-controller.ts index 2c37638..b5d72f3 100644 --- a/src/controllers/03-customer-controller.ts +++ b/src/controllers/03-customer-controller.ts @@ -35,7 +35,7 @@ import { setFile, } from "../utils/minio"; import { isUsedError, notFoundError, relationError } from "../utils/error"; -import { connectOrNot } from "../utils/relation"; +import { connectOrNot, queryOrNot } from "../utils/relation"; const MANAGE_ROLES = [ "system", @@ -165,16 +165,14 @@ export class CustomerController extends Controller { @Query() company: boolean = false, ) { const where = { - OR: query - ? [ - { branch: { some: { namePrefix: { contains: query } } } }, - { branch: { some: { customerName: { contains: query } } } }, - { branch: { some: { firstName: { contains: query } } } }, - { branch: { some: { firstNameEN: { contains: query } } } }, - { branch: { some: { lastName: { contains: query } } } }, - { branch: { some: { lastNameEN: { contains: query } } } }, - ] - : undefined, + OR: queryOrNot(query, [ + { branch: { some: { namePrefix: { contains: query } } } }, + { branch: { some: { customerName: { contains: query } } } }, + { branch: { some: { firstName: { contains: query } } } }, + { branch: { some: { firstNameEN: { contains: query } } } }, + { branch: { some: { lastName: { contains: query } } } }, + { branch: { some: { lastNameEN: { contains: query } } } }, + ]), AND: { customerType, ...filterStatus(status), diff --git a/src/controllers/03-employee-controller.ts b/src/controllers/03-employee-controller.ts index 794c119..cffc544 100644 --- a/src/controllers/03-employee-controller.ts +++ b/src/controllers/03-employee-controller.ts @@ -25,7 +25,12 @@ import { createPermCheck, createPermCondition, } from "../services/permission"; -import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation"; +import { + connectOrDisconnect, + connectOrNot, + queryOrNot, + whereAddressQuery, +} from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { deleteFile, @@ -155,13 +160,13 @@ export class EmployeeController extends Controller { _count: true, by: ["gender"], where: { - OR: [ + OR: queryOrNot(query, [ { firstName: { contains: query } }, { firstNameEN: { contains: query } }, { lastName: { contains: query } }, { lastNameEN: { contains: query } }, ...whereAddressQuery(query), - ], + ]), AND: { ...filterStatus(status), customerBranchId, @@ -196,13 +201,13 @@ export class EmployeeController extends Controller { @Query() pageSize: number = 30, ) { const where = { - OR: [ + OR: queryOrNot(query, [ { firstName: { contains: query } }, { firstNameEN: { contains: query } }, { lastName: { contains: query } }, { lastNameEN: { contains: query } }, ...whereAddressQuery(query), - ], + ]), AND: { ...filterStatus(status), customerBranch: { diff --git a/src/controllers/04-product-controller.ts b/src/controllers/04-product-controller.ts index 3c1f03e..4fbbe13 100644 --- a/src/controllers/04-product-controller.ts +++ b/src/controllers/04-product-controller.ts @@ -27,6 +27,7 @@ import { isSystem } from "../utils/keycloak"; import { filterStatus } from "../services/prisma"; import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio"; import { isUsedError, notFoundError, relationError } from "../utils/error"; +import { queryOrNot } from "../utils/relation"; const MANAGE_ROLES = [ "system", @@ -120,13 +121,11 @@ export class ProductController extends Controller { @Query() pageSize: number = 30, ) { const where = { - OR: query - ? [ - { name: { contains: query } }, - { detail: { contains: query } }, - { code: { contains: query, mode: "insensitive" } }, - ] - : undefined, + OR: queryOrNot(query, [ + { name: { contains: query } }, + { detail: { contains: query } }, + { code: { contains: query, mode: "insensitive" } }, + ]), AND: { ...filterStatus(status), productGroupId, diff --git a/src/controllers/04-product-group-controller.ts b/src/controllers/04-product-group-controller.ts index eb91350..0282585 100644 --- a/src/controllers/04-product-group-controller.ts +++ b/src/controllers/04-product-group-controller.ts @@ -26,6 +26,7 @@ import { } from "../services/permission"; import { filterStatus } from "../services/prisma"; import { isUsedError, notFoundError, relationError } from "../utils/error"; +import { queryOrNot } from "../utils/relation"; type ProductGroupCreate = { name: string; @@ -87,11 +88,11 @@ export class ProductGroup extends Controller { @Query() pageSize: number = 30, ) { const where = { - OR: [ + OR: queryOrNot(query, [ { name: { contains: query } }, { detail: { contains: query } }, { code: { contains: query, mode: "insensitive" } }, - ], + ]), AND: [ { ...filterStatus(status), diff --git a/src/controllers/04-service-controller.ts b/src/controllers/04-service-controller.ts index e36bf08..7a9d542 100644 --- a/src/controllers/04-service-controller.ts +++ b/src/controllers/04-service-controller.ts @@ -28,6 +28,7 @@ import { import { filterStatus } from "../services/prisma"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { deleteFile, fileLocation, getFile, getPresigned, listFile, setFile } from "../utils/minio"; +import { queryOrNot } from "../utils/relation"; const MANAGE_ROLES = [ "system", @@ -144,13 +145,11 @@ export class ServiceController extends Controller { @Query() fullDetail?: boolean, ) { const where = { - OR: query - ? [ - { name: { contains: query } }, - { detail: { contains: query } }, - { code: { contains: query, mode: "insensitive" } }, - ] - : undefined, + OR: queryOrNot(query, [ + { name: { contains: query } }, + { detail: { contains: query } }, + { code: { contains: query, mode: "insensitive" } }, + ]), AND: { ...filterStatus(status), productGroupId, diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index 40a9738..045d6ce 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -24,6 +24,7 @@ import { import { isSystem } from "../utils/keycloak"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { precisionRound } from "../utils/arithmetic"; +import { queryOrNot } from "../utils/relation"; type QuotationCreate = { registeredBranchId: string; @@ -183,7 +184,7 @@ export class QuotationController extends Controller { @Query() query = "", ) { const where = { - OR: [ + OR: queryOrNot(query, [ { code: { contains: query, mode: "insensitive" } }, { workName: { contains: query } }, { @@ -198,7 +199,7 @@ export class QuotationController extends Controller { ], }, }, - ], + ]), payCondition, registeredBranch: isSystem(req.user) ? undefined : { OR: permissionCond(req.user) }, } satisfies Prisma.QuotationWhereInput;