refactor: don't include condition if no query
This commit is contained in:
parent
b0bd401f25
commit
af463ee509
10 changed files with 77 additions and 57 deletions
|
|
@ -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<Prisma.BranchWhereInput[]>(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([
|
||||
|
|
|
|||
|
|
@ -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<Prisma.BranchUserWhereInput[]>(query, [
|
||||
{ branch: { name: { contains: query } } },
|
||||
{ branch: { nameEN: { contains: query } } },
|
||||
]),
|
||||
} satisfies Prisma.BranchUserWhereInput;
|
||||
|
||||
const [result, total] = await prisma.$transaction([
|
||||
|
|
|
|||
|
|
@ -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<Prisma.UserWhereInput[]>(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,
|
||||
|
|
|
|||
|
|
@ -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<Prisma.CustomerBranchWhereInput[]>(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<Prisma.EmployeeWhereInput[]>(query, [
|
||||
{ firstName: { contains: query } },
|
||||
{ firstNameEN: { contains: query } },
|
||||
{ lastName: { contains: query } },
|
||||
{ lastNameEN: { contains: query } },
|
||||
...whereAddressQuery(query),
|
||||
],
|
||||
]),
|
||||
AND: {
|
||||
...filterStatus(status),
|
||||
customerBranchId: branchId,
|
||||
|
|
|
|||
|
|
@ -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<Prisma.CustomerWhereInput[]>(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),
|
||||
|
|
|
|||
|
|
@ -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<Prisma.EmployeeWhereInput[]>(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<Prisma.EmployeeWhereInput[]>(query, [
|
||||
{ firstName: { contains: query } },
|
||||
{ firstNameEN: { contains: query } },
|
||||
{ lastName: { contains: query } },
|
||||
{ lastNameEN: { contains: query } },
|
||||
...whereAddressQuery(query),
|
||||
],
|
||||
]),
|
||||
AND: {
|
||||
...filterStatus(status),
|
||||
customerBranch: {
|
||||
|
|
|
|||
|
|
@ -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<Prisma.ProductWhereInput[]>(query, [
|
||||
{ name: { contains: query } },
|
||||
{ detail: { contains: query } },
|
||||
{ code: { contains: query, mode: "insensitive" } },
|
||||
]),
|
||||
AND: {
|
||||
...filterStatus(status),
|
||||
productGroupId,
|
||||
|
|
|
|||
|
|
@ -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<Prisma.ProductGroupWhereInput[]>(query, [
|
||||
{ name: { contains: query } },
|
||||
{ detail: { contains: query } },
|
||||
{ code: { contains: query, mode: "insensitive" } },
|
||||
],
|
||||
]),
|
||||
AND: [
|
||||
{
|
||||
...filterStatus(status),
|
||||
|
|
|
|||
|
|
@ -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<Prisma.ServiceWhereInput[]>(query, [
|
||||
{ name: { contains: query } },
|
||||
{ detail: { contains: query } },
|
||||
{ code: { contains: query, mode: "insensitive" } },
|
||||
]),
|
||||
AND: {
|
||||
...filterStatus(status),
|
||||
productGroupId,
|
||||
|
|
|
|||
|
|
@ -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<Prisma.QuotationWhereInput[]>(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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue