From 24c85b728c41f622565dce37752fa14576752958 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:39:44 +0700 Subject: [PATCH] feat: filter by branch type (head/sub) and relation --- src/controllers/branch/branch-controller.ts | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts index b4ae109..65b95fb 100644 --- a/src/controllers/branch/branch-controller.ts +++ b/src/controllers/branch/branch-controller.ts @@ -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 }, });