From b455985ba11d34b8ff88b219dbda60be7b0f2c85 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 22 Apr 2024 17:16:30 +0700 Subject: [PATCH] feat: update query to also return branch --- src/controllers/customer-controller.ts | 56 +++++++++++++++++++++----- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts index 24d930f..8fbe421 100644 --- a/src/controllers/customer-controller.ts +++ b/src/controllers/customer-controller.ts @@ -55,6 +55,7 @@ export class CustomerController extends Controller { @Query() query: string = "", @Query() page: number = 1, @Query() pageSize: number = 30, + @Query() includeBranch: boolean = false, ) { const where = { OR: [{ customerName: { contains: query } }, { customerNameEN: { contains: query } }], @@ -62,6 +63,17 @@ export class CustomerController extends Controller { const [result, total] = await prisma.$transaction([ prisma.customer.findMany({ + include: { + branch: includeBranch + ? { + include: { + province: true, + district: true, + subDistrict: true, + }, + } + : undefined, + }, orderBy: { createdAt: "asc" }, where, take: pageSize, @@ -85,7 +97,18 @@ export class CustomerController extends Controller { @Get("{customerId}") async getById(@Path() customerId: string) { - const record = await prisma.customer.findFirst({ where: { id: customerId } }); + const record = await prisma.customer.findFirst({ + include: { + branch: { + include: { + province: true, + district: true, + subDistrict: true, + }, + }, + }, + where: { id: customerId }, + }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found"); return Object.assign(record, { @@ -250,19 +273,30 @@ export class CustomerController extends Controller { const { customerBranch, ...payload } = body; const record = await prisma.customer.update({ + include: { + branch: { + include: { + province: true, + district: true, + subDistrict: true, + }, + }, + }, where: { id: customerId }, data: { ...payload, - branch: { - deleteMany: { - id: { notIn: customerBranch?.map((v) => v.id) || [] }, - }, - updateMany: - customerBranch?.map((v) => ({ - where: { id: v.id }, - data: { ...v, updateBy: req.user.name }, - })) || [], - }, + branch: + (customerBranch && { + deleteMany: { + id: { notIn: customerBranch.map((v) => v.id) || [] }, + }, + updateMany: + customerBranch.map((v) => ({ + where: { id: v.id }, + data: { ...v, updateBy: req.user.name }, + })) || [], + }) || + undefined, updateBy: req.user.name, }, });