From 30acdfdd32bf17559d364f40430c45457e40e22c Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 11 Apr 2024 14:28:44 +0700 Subject: [PATCH] feat: list by headOfficeId & get with contact --- src/controllers/branch-controller.ts | 35 ++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index a1463cc..ba0743b 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -18,6 +18,13 @@ import prisma from "../db"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; +import minio from "../services/minio"; + +if (!process.env.MINIO_BUCKET) { + throw Error("Require MinIO bucket."); +} + +const MINIO_BUCKET = process.env.MINIO_BUCKET; type BranchCreate = { status?: Status; @@ -104,6 +111,7 @@ export class BranchController extends Controller { async getBranch( @Query() zipCode?: string, @Query() filter?: "head" | "sub", + @Query() headOfficeId?: string, @Query() tree?: boolean, @Query() query: string = "", @Query() page: number = 1, @@ -111,8 +119,8 @@ export class BranchController extends Controller { ) { const where = { AND: { - headOfficeId: filter === "head" || tree ? null : undefined, - NOT: { headOfficeId: filter === "sub" ? null : undefined }, + headOfficeId: headOfficeId ?? (filter === "head" || tree ? null : undefined), + NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined }, }, OR: [ { nameEN: { contains: query }, zipCode }, @@ -147,7 +155,11 @@ export class BranchController extends Controller { } @Get("{branchId}") - async getBranchById(@Path() branchId: string, @Query() includeSubBranch?: boolean) { + async getBranchById( + @Path() branchId: string, + @Query() includeSubBranch?: boolean, + @Query() includeContact?: boolean, + ) { const record = await prisma.branch.findFirst({ include: { province: true, @@ -160,6 +172,7 @@ export class BranchController extends Controller { subDistrict: true, }, }, + contact: includeContact, }, where: { id: branchId }, }); @@ -168,7 +181,21 @@ export class BranchController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); } - return record; + return { + ...record, + contact: record.contact + ? await Promise.all( + record.contact.map(async (v) => + Object.assign(v, { + qrCodeImageUrl: await minio.presignedGetObject( + MINIO_BUCKET, + `branch/contact-${record.id}`, + ), + }), + ), + ) + : undefined, + }; } @Post()