feat: filter by branch type (head/sub) and relation

This commit is contained in:
Methapon2001 2024-04-05 09:39:44 +07:00
parent 818112fff4
commit 24c85b728c

View file

@ -20,7 +20,7 @@ import HttpStatus from "../../interfaces/http-status";
import { RequestWithUser } from "../../interfaces/user";
type BranchCreate = {
code: string;
code?: string;
taxNo: string;
nameEN: string;
name: string;
@ -85,11 +85,17 @@ export class BranchController extends Controller {
@Get()
async getBranch(
@Query() zipCode?: string,
@Query() filter?: "head" | "sub",
@Query() tree?: boolean,
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
) {
const where = {
AND: {
headOfficeId: filter === "head" || tree ? null : undefined,
NOT: { headOfficeId: filter === "sub" ? null : undefined },
},
OR: [
{ nameEN: { contains: query }, zipCode },
{ name: { contains: query }, zipCode },
@ -103,6 +109,13 @@ export class BranchController extends Controller {
province: true,
district: true,
subDistrict: true,
branch: tree && {
include: {
province: true,
district: true,
subDistrict: true,
},
},
},
where,
take: pageSize,
@ -115,12 +128,19 @@ export class BranchController extends Controller {
}
@Get("{branchId}")
async getBranchById(@Path() branchId: string) {
async getBranchById(@Path() branchId: string, @Query() includeSubBranch: boolean) {
const record = await prisma.branch.findFirst({
include: {
province: true,
district: true,
subDistrict: true,
branch: includeSubBranch && {
include: {
province: true,
district: true,
subDistrict: true,
},
},
},
where: { id: branchId },
});