search startDate and endDate

This commit is contained in:
Kanjana 2025-04-17 13:41:22 +07:00
parent ee610c5686
commit 0aba9f9865
20 changed files with 123 additions and 32 deletions

View file

@ -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,

View file

@ -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([

View file

@ -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([

View file

@ -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<Prisma.CustomerBranchWhereInput[]>(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<Prisma.EmployeeWhereInput[]>(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([

View file

@ -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<Prisma.CustomerWhereInput[]>(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([

View file

@ -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([

View file

@ -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({

View file

@ -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([

View file

@ -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([

View file

@ -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<Prisma.ProductGroupWhereInput[]>(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([

View file

@ -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({

View file

@ -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([

View file

@ -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([

View file

@ -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<Prisma.QuotationWhereInput[]>(query, [
@ -253,6 +255,7 @@ export class QuotationController extends Controller {
},
}
: undefined,
...whereDateQuery(startDate, endDate),
} satisfies Prisma.QuotationWhereInput;
const [result, total] = await prisma.$transaction([

View file

@ -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<Prisma.RequestDataWhereInput[]>(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([

View file

@ -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([

View file

@ -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<Prisma.CreditNoteWhereInput[]>(query, [
@ -199,6 +201,7 @@ export class CreditNoteController extends Controller {
},
},
},
...whereDateQuery(startDate, endDate),
} satisfies Prisma.CreditNoteWhereInput;
const [result, total] = await prisma.$transaction([

View file

@ -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<Prisma.QuotationWhereInput[]>(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([

View file

@ -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<Prisma.RequestDataWhereInput[]>(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([

View file

@ -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<T>(query: string | boolean, where: T): T | undefined;
export function queryOrNot<T, U>(query: string | boolean, where: T, fallback: U): T | U;
export function queryOrNot<T, U>(query: string | boolean, where: T, fallback?: U) {
export function queryOrNot<T>(query: any, where: T): T | undefined;
export function queryOrNot<T, U>(query: any, where: T, fallback: U): T | U;
export function queryOrNot<T, U>(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,
},
};
}