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