diff --git a/src/controllers/01-branch-controller.ts b/src/controllers/01-branch-controller.ts index f0cc512..003c4cd 100644 --- a/src/controllers/01-branch-controller.ts +++ b/src/controllers/01-branch-controller.ts @@ -39,6 +39,7 @@ import { connectOrNot, queryOrNot, whereAddressQuery, + whereDateQuery, } from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; @@ -250,6 +251,8 @@ export class BranchController extends Controller { @Query() query: string = "", @Query() page: number = 1, @Query() pageSize: number = 30, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { AND: { @@ -285,6 +288,7 @@ export class BranchController extends Controller { }, }, ]), + ...whereDateQuery(startDate, endDate), } satisfies Prisma.BranchWhereInput; const [result, total] = await prisma.$transaction([ @@ -321,6 +325,7 @@ export class BranchController extends Controller { district: true, subDistrict: true, }, + ...whereDateQuery(startDate, endDate), orderBy: { code: "asc" }, } : false, diff --git a/src/controllers/01-branch-user-controller.ts b/src/controllers/01-branch-user-controller.ts index 99d77a4..05177d2 100644 --- a/src/controllers/01-branch-user-controller.ts +++ b/src/controllers/01-branch-user-controller.ts @@ -18,7 +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"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; const MANAGE_ROLES = ["system", "head_of_admin", "admin", "branch_manager"]; @@ -97,6 +97,8 @@ export class UserBranchController extends Controller { @Query() query: string = "", @Query() page: number = 1, @Query() pageSize: number = 30, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { AND: { @@ -107,6 +109,7 @@ export class UserBranchController extends Controller { { branch: { name: { contains: query, mode: "insensitive" } } }, { branch: { nameEN: { contains: query, mode: "insensitive" } } }, ]), + ...whereDateQuery(startDate, endDate), } satisfies Prisma.BranchUserWhereInput; const [result, total] = await prisma.$transaction([ @@ -150,6 +153,8 @@ export class BranchUserController extends Controller { @Query() query: string = "", @Query() page: number = 1, @Query() pageSize: number = 30, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { AND: { @@ -164,6 +169,7 @@ export class BranchUserController extends Controller { { user: { email: { contains: query, mode: "insensitive" } } }, { user: { telephoneNo: { contains: query, mode: "insensitive" } } }, ], + ...whereDateQuery(startDate, endDate), } 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 6639511..17630eb 100644 --- a/src/controllers/02-user-controller.ts +++ b/src/controllers/02-user-controller.ts @@ -51,6 +51,7 @@ import { connectOrNot, queryOrNot, whereAddressQuery, + whereDateQuery, } from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { retry } from "../utils/func"; @@ -305,6 +306,8 @@ export class UserController extends Controller { @Query() status?: Status, @Query() responsibleDistrictId?: string, @Query() activeBranchOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, @Body() body?: { userId?: string[]; @@ -368,6 +371,7 @@ export class UserController extends Controller { }, }, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.UserWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/03-customer-branch-controller.ts b/src/controllers/03-customer-branch-controller.ts index 7818bef..5c94a2f 100644 --- a/src/controllers/03-customer-branch-controller.ts +++ b/src/controllers/03-customer-branch-controller.ts @@ -30,6 +30,7 @@ import { connectOrNot, queryOrNot, whereAddressQuery, + whereDateQuery, } from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { @@ -195,6 +196,8 @@ export class CustomerBranchController extends Controller { @Query() page: number = 1, @Query() pageSize: number = 30, @Query() activeRegisBranchOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -229,6 +232,7 @@ export class CustomerBranchController extends Controller { subDistrict: zipCode ? { zipCode } : undefined, ...filterStatus(activeRegisBranchOnly ? Status.ACTIVE : status), }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.CustomerBranchWhereInput; const [result, total] = await prisma.$transaction([ @@ -285,6 +289,8 @@ export class CustomerBranchController extends Controller { @Query() visa?: boolean, @Query() page: number = 1, @Query() pageSize: number = 30, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -300,6 +306,7 @@ export class CustomerBranchController extends Controller { subDistrict: zipCode ? { zipCode } : undefined, gender, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.EmployeeWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/03-customer-controller.ts b/src/controllers/03-customer-controller.ts index 8cac3d9..122726f 100644 --- a/src/controllers/03-customer-controller.ts +++ b/src/controllers/03-customer-controller.ts @@ -36,7 +36,7 @@ import { setFile, } from "../utils/minio"; import { isUsedError, notFoundError, relationError } from "../utils/error"; -import { connectOrNot, queryOrNot } from "../utils/relation"; +import { connectOrNot, queryOrNot, whereDateQuery } from "../utils/relation"; const MANAGE_ROLES = [ "system", @@ -165,6 +165,8 @@ export class CustomerController extends Controller { @Query() includeBranch: boolean = false, @Query() company: boolean = false, @Query() activeBranchOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -188,6 +190,7 @@ export class CustomerController extends Controller { : permissionCond(req.user, { activeOnly: activeBranchOnly }), }, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.CustomerWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/03-employee-controller.ts b/src/controllers/03-employee-controller.ts index 44629c1..5f8a1d4 100644 --- a/src/controllers/03-employee-controller.ts +++ b/src/controllers/03-employee-controller.ts @@ -30,6 +30,7 @@ import { connectOrNot, queryOrNot, whereAddressQuery, + whereDateQuery, } from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { @@ -154,6 +155,8 @@ export class EmployeeController extends Controller { @Query() customerBranchId?: string, @Query() status?: Status, @Query() query: string = "", + @Query() startDate?: Date, + @Query() endDate?: Date, ) { return await prisma.employee .groupBy({ @@ -183,6 +186,7 @@ export class EmployeeController extends Controller { }, }, }, + ...whereDateQuery(startDate, endDate), }, }) .then((res) => @@ -240,6 +244,8 @@ export class EmployeeController extends Controller { @Query() page: number = 1, @Query() pageSize: number = 30, @Query() activeOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, @Body() body?: { passport?: string[]; @@ -288,6 +294,7 @@ export class EmployeeController extends Controller { subDistrict: zipCode ? { zipCode } : undefined, gender, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.EmployeeWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/04-flow-template-controller.ts b/src/controllers/04-flow-template-controller.ts index 0f80d61..97bd1b9 100644 --- a/src/controllers/04-flow-template-controller.ts +++ b/src/controllers/04-flow-template-controller.ts @@ -24,7 +24,7 @@ import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { notFoundError } from "../utils/error"; import { filterStatus } from "../services/prisma"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; type WorkflowPayload = { name: string; @@ -58,6 +58,8 @@ export class FlowTemplateController extends Controller { @Query() status?: Status, @Query() query = "", @Query() activeOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -74,6 +76,7 @@ export class FlowTemplateController extends Controller { OR: permissionCondCompany(req.user, { activeOnly: true }), }, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.WorkflowTemplateWhereInput; const [result, total] = await prisma.$transaction([ prisma.workflowTemplate.findMany({ diff --git a/src/controllers/04-invoice-controller.ts b/src/controllers/04-invoice-controller.ts index 64efee2..fb3fdf0 100644 --- a/src/controllers/04-invoice-controller.ts +++ b/src/controllers/04-invoice-controller.ts @@ -21,6 +21,7 @@ import { createPermCondition, } from "../services/permission"; import { PaymentStatus } from "../generated/kysely/types"; +import { whereDateQuery } from "../utils/relation"; type InvoicePayload = { quotationId: string; @@ -95,6 +96,8 @@ export class InvoiceController extends Controller { @Query() quotationId?: string, @Query() debitNoteId?: string, @Query() pay?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where: Prisma.InvoiceWhereInput = { OR: [ @@ -132,6 +135,7 @@ export class InvoiceController extends Controller { OR: permissionCondCompany(req.user), }, }, + ...whereDateQuery(startDate, endDate), }; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/04-product-controller.ts b/src/controllers/04-product-controller.ts index 8cb8c3e..09dce42 100644 --- a/src/controllers/04-product-controller.ts +++ b/src/controllers/04-product-controller.ts @@ -27,7 +27,7 @@ import { isSystem } from "../utils/keycloak"; import { filterStatus } from "../services/prisma"; import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio"; import { isUsedError, notFoundError, relationError } from "../utils/error"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; const MANAGE_ROLES = [ "system", @@ -139,6 +139,8 @@ export class ProductController extends Controller { @Query() orderField?: keyof Product, @Query() orderBy?: "asc" | "desc", @Query() activeOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { // NOTE: will be used to scope product within product group that is shared between branch but not company when select shared product if user is system const targetGroup = @@ -194,6 +196,7 @@ export class ProductController extends Controller { : []), ], }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.ProductWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/04-product-group-controller.ts b/src/controllers/04-product-group-controller.ts index ee27265..ecd4ac6 100644 --- a/src/controllers/04-product-group-controller.ts +++ b/src/controllers/04-product-group-controller.ts @@ -27,7 +27,7 @@ import { } from "../services/permission"; import { filterStatus } from "../services/prisma"; import { isUsedError, notFoundError, relationError } from "../utils/error"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; type ProductGroupCreate = { name: string; @@ -90,6 +90,8 @@ export class ProductGroup extends Controller { @Query() page: number = 1, @Query() pageSize: number = 30, @Query() activeOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -105,6 +107,7 @@ export class ProductGroup extends Controller { : { OR: permissionCond(req.user, { activeOnly }) }, }, ], + ...whereDateQuery(startDate, endDate), } satisfies Prisma.ProductGroupWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/04-properties-controller.ts b/src/controllers/04-properties-controller.ts index 35f94a8..6203ec6 100644 --- a/src/controllers/04-properties-controller.ts +++ b/src/controllers/04-properties-controller.ts @@ -24,7 +24,7 @@ import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { notFoundError } from "../utils/error"; import { filterStatus } from "../services/prisma"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; type PropertyPayload = { name: string; @@ -49,6 +49,8 @@ export class PropertiesController extends Controller { @Query() status?: Status, @Query() query = "", @Query() activeOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -61,6 +63,7 @@ export class PropertiesController extends Controller { OR: permissionCondCompany(req.user, { activeOnly: true }), }, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.PropertyWhereInput; const [result, total] = await prisma.$transaction([ prisma.property.findMany({ diff --git a/src/controllers/04-service-controller.ts b/src/controllers/04-service-controller.ts index c193fbd..ed46c18 100644 --- a/src/controllers/04-service-controller.ts +++ b/src/controllers/04-service-controller.ts @@ -36,7 +36,7 @@ import { listFile, setFile, } from "../utils/minio"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; const MANAGE_ROLES = [ "system", @@ -164,6 +164,8 @@ export class ServiceController extends Controller { @Query() fullDetail?: boolean, @Query() activeOnly?: boolean, @Query() shared?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { // NOTE: will be used to scope product within product group that is shared between branch but not company when select shared product if user is system const targetGroup = @@ -219,6 +221,7 @@ export class ServiceController extends Controller { : []), ], }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.ServiceWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/04-work-controller.ts b/src/controllers/04-work-controller.ts index 9b875b6..c8a140b 100644 --- a/src/controllers/04-work-controller.ts +++ b/src/controllers/04-work-controller.ts @@ -18,6 +18,7 @@ import prisma from "../db"; import { RequestWithUser } from "../interfaces/user"; import HttpStatus from "../interfaces/http-status"; import { isUsedError, notFoundError } from "../utils/error"; +import { whereDateQuery } from "../utils/relation"; type WorkCreate = { order: number; @@ -45,9 +46,12 @@ export class WorkController extends Controller { @Query() query: string = "", @Query() page: number = 1, @Query() pageSize: number = 30, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: [{ name: { contains: query }, serviceId: baseOnly ? null : undefined }], + ...whereDateQuery(startDate, endDate), } satisfies Prisma.WorkWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index fa3d6b3..b454db3 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -25,7 +25,7 @@ import { import { isSystem } from "../utils/keycloak"; import { isUsedError, notFoundError, relationError } from "../utils/error"; import { precisionRound } from "../utils/arithmetic"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; import { deleteFile, fileLocation, getFile, getPresigned, listFile, setFile } from "../utils/minio"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; @@ -206,6 +206,8 @@ export class QuotationController extends Controller { @Query() forDebitNote?: boolean, @Query() code?: string, @Query() query = "", + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -253,6 +255,7 @@ export class QuotationController extends Controller { }, } : undefined, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.QuotationWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/06-request-list-controller.ts b/src/controllers/06-request-list-controller.ts index e1da5f2..6b5e5f8 100644 --- a/src/controllers/06-request-list-controller.ts +++ b/src/controllers/06-request-list-controller.ts @@ -27,7 +27,7 @@ import { createPermCheck, createPermCondition, } from "../services/permission"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; import { notFoundError } from "../utils/error"; import { deleteFile, fileLocation, getFile, getPresigned, listFile, setFile } from "../utils/minio"; import HttpError from "../interfaces/http-error"; @@ -81,6 +81,8 @@ export class RequestDataController extends Controller { @Query() quotationId?: string, @Query() code?: string, @Query() incomplete?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -147,6 +149,7 @@ export class RequestDataController extends Controller { id: quotationId, registeredBranch: { OR: permissionCond(req.user) }, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.RequestDataWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/07-task-controller.ts b/src/controllers/07-task-controller.ts index 03009b3..fd5c4a2 100644 --- a/src/controllers/07-task-controller.ts +++ b/src/controllers/07-task-controller.ts @@ -42,7 +42,7 @@ import { listFile, setFile, } from "../utils/minio"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; const MANAGE_ROLES = ["system", "head_of_admin", "admin", "document_checker"]; @@ -107,6 +107,8 @@ export class TaskController extends Controller { @Query() assignedUserId?: string, @Query() taskOrderStatus?: TaskOrderStatus, @Body() body?: { code?: string[] }, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { taskOrderStatus, @@ -125,6 +127,7 @@ export class TaskController extends Controller { { contactName: { contains: query, mode: "insensitive" } }, { contactTel: { contains: query, mode: "insensitive" } }, ]), + ...whereDateQuery(startDate, endDate), } satisfies Prisma.TaskOrderWhereInput; const [result, total] = await prisma.$transaction([ @@ -979,6 +982,8 @@ export class UserTaskController extends Controller { @Query() page = 1, @Query() pageSize = 30, @Query() userTaskStatus?: UserTaskStatus, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { taskList: { @@ -1025,6 +1030,7 @@ export class UserTaskController extends Controller { { contactName: { contains: query, mode: "insensitive" } }, { contactTel: { contains: query, mode: "insensitive" } }, ]), + ...whereDateQuery(startDate, endDate), } satisfies Prisma.TaskOrderWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/08-credit-note-controller.ts b/src/controllers/08-credit-note-controller.ts index 5fda3eb..2e80575 100644 --- a/src/controllers/08-credit-note-controller.ts +++ b/src/controllers/08-credit-note-controller.ts @@ -35,7 +35,7 @@ import { } from "../utils/minio"; import { notFoundError } from "../utils/error"; import { CreditNotePaybackType, CreditNoteStatus, Prisma, RequestDataStatus } from "@prisma/client"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; import { PaybackStatus, RequestWorkStatus } from "../generated/kysely/types"; const MANAGE_ROLES = [ @@ -143,6 +143,8 @@ export class CreditNoteController extends Controller { @Query() quotationId?: string, @Query() creditNoteStatus?: CreditNoteStatus, @Body() body?: {}, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -199,6 +201,7 @@ export class CreditNoteController extends Controller { }, }, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.CreditNoteWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/09-debit-note-controller.ts b/src/controllers/09-debit-note-controller.ts index a499f88..993037a 100644 --- a/src/controllers/09-debit-note-controller.ts +++ b/src/controllers/09-debit-note-controller.ts @@ -36,7 +36,7 @@ import { setFile, } from "../utils/minio"; import { isUsedError, notFoundError, relationError } from "../utils/error"; -import { queryOrNot } from "../utils/relation"; +import { queryOrNot, whereDateQuery } from "../utils/relation"; import { isSystem } from "../utils/keycloak"; import { precisionRound } from "../utils/arithmetic"; @@ -196,6 +196,8 @@ export class DebitNoteController extends Controller { @Query() includeRegisteredBranch?: boolean, @Query() code?: string, @Body() body?: {}, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -220,6 +222,7 @@ export class DebitNoteController extends Controller { debitNoteQuotationId: quotationId, registeredBranch: isSystem(req.user) ? undefined : { OR: permissionCond(req.user) }, quotationStatus: status, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.QuotationWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/controllers/09-line-controller.ts b/src/controllers/09-line-controller.ts index 0a6ea3c..91c06bb 100644 --- a/src/controllers/09-line-controller.ts +++ b/src/controllers/09-line-controller.ts @@ -25,7 +25,7 @@ import { TaskStatus, RequestWorkStatus, } from "@prisma/client"; -import { queryOrNot, whereAddressQuery } from "../utils/relation"; +import { queryOrNot, whereAddressQuery, whereDateQuery } from "../utils/relation"; import { filterStatus } from "../services/prisma"; // import { RequestWorkStatus } from "../generated/kysely/types"; import { deleteFile, fileLocation, getFile, getPresigned, listFile, setFile } from "../utils/minio"; @@ -51,6 +51,8 @@ export class LineController extends Controller { @Query() page: number = 1, @Query() pageSize: number = 30, @Query() activeOnly?: boolean, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: !!query @@ -87,6 +89,7 @@ export class LineController extends Controller { subDistrict: zipCode ? { zipCode } : undefined, gender, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.EmployeeWhereInput; const [result, total] = await prisma.$transaction([ @@ -173,6 +176,8 @@ export class LineController extends Controller { @Query() requestDataStatus?: RequestDataStatus, @Query() quotationId?: string, @Query() code?: string, + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: queryOrNot(query, [ @@ -247,6 +252,7 @@ export class LineController extends Controller { ], }, }, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.RequestDataWhereInput; const [result, total] = await prisma.$transaction([ @@ -604,6 +610,8 @@ export class LineController extends Controller { @Query() includeRegisteredBranch?: boolean, @Query() code?: string, @Query() query = "", + @Query() startDate?: Date, + @Query() endDate?: Date, ) { const where = { OR: @@ -660,6 +668,7 @@ export class LineController extends Controller { }, } : undefined, + ...whereDateQuery(startDate, endDate), } satisfies Prisma.QuotationWhereInput; const [result, total] = await prisma.$transaction([ diff --git a/src/utils/relation.ts b/src/utils/relation.ts index 3e96f11..aebb744 100644 --- a/src/utils/relation.ts +++ b/src/utils/relation.ts @@ -10,26 +10,35 @@ export function connectOrDisconnect(id?: string | null) { export function whereAddressQuery(query: string) { return [ - { address: { contains: query } }, - { addressEN: { contains: query } }, - { soi: { contains: query } }, - { soiEN: { contains: query } }, - { moo: { contains: query } }, - { mooEN: { contains: query } }, - { street: { contains: query } }, - { streetEN: { contains: query } }, - { province: { name: { contains: query } } }, - { province: { nameEN: { contains: query } } }, - { district: { name: { contains: query } } }, - { district: { nameEN: { contains: query } } }, - { subDistrict: { name: { contains: query } } }, - { subDistrict: { nameEN: { contains: query } } }, - { subDistrict: { zipCode: { contains: query } } }, - ]; + { address: { contains: query, mode: "insensitive" } }, + { addressEN: { contains: query, mode: "insensitive" } }, + { soi: { contains: query, mode: "insensitive" } }, + { soiEN: { contains: query, mode: "insensitive" } }, + { moo: { contains: query, mode: "insensitive" } }, + { mooEN: { contains: query, mode: "insensitive" } }, + { street: { contains: query, mode: "insensitive" } }, + { streetEN: { contains: query, mode: "insensitive" } }, + { province: { name: { contains: query, mode: "insensitive" } } }, + { province: { nameEN: { contains: query, mode: "insensitive" } } }, + { district: { name: { contains: query, mode: "insensitive" } } }, + { district: { nameEN: { contains: query, mode: "insensitive" } } }, + { subDistrict: { name: { contains: query, mode: "insensitive" } } }, + { subDistrict: { nameEN: { contains: query, mode: "insensitive" } } }, + { subDistrict: { zipCode: { contains: query, mode: "insensitive" } } }, + ] as const; } -export function queryOrNot(query: string | boolean, where: T): T | undefined; -export function queryOrNot(query: string | boolean, where: T, fallback: U): T | U; -export function queryOrNot(query: string | boolean, where: T, fallback?: U) { +export function queryOrNot(query: any, where: T): T | undefined; +export function queryOrNot(query: any, where: T, fallback: U): T | U; +export function queryOrNot(query: any, where: T, fallback?: U) { return !!query ? where : fallback; } + +export function whereDateQuery(startDate: Date | undefined, endDate: Date | undefined) { + return { + createdAt: { + gte: startDate, + lte: endDate, + }, + }; +}