From 63293b671b3d8dd2c52e6386b7f60a2e3366efb0 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 17 Apr 2024 13:42:11 +0700 Subject: [PATCH] refactor: update branch endpoint --- src/controllers/branch-contact-controller.ts | 7 ------- src/controllers/branch-controller.ts | 18 +++++++++++++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/controllers/branch-contact-controller.ts b/src/controllers/branch-contact-controller.ts index 450ca55..b71aea5 100644 --- a/src/controllers/branch-contact-controller.ts +++ b/src/controllers/branch-contact-controller.ts @@ -14,17 +14,10 @@ import { } from "tsoa"; import prisma from "../db"; -import minio from "../services/minio"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; -if (!process.env.MINIO_BUCKET) { - throw Error("Require MinIO bucket."); -} - -const MINIO_BUCKET = process.env.MINIO_BUCKET; - type BranchContactCreate = { telephoneNo: string; }; diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index 97196d1..552e5e4 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -35,7 +35,8 @@ type BranchCreate = { address: string; zipCode: string; email: string; - telephoneNo: string; + contactName: string; + telephoneNo: string | string[]; lineId: string; longitude: string; latitude: string; @@ -55,7 +56,8 @@ type BranchUpdate = { address?: string; zipCode?: string; email?: string; - telephoneNo?: string; + contactName?: string; + telephoneNo?: string | string[]; lineId?: string; longitude?: string; latitude?: string; @@ -238,7 +240,7 @@ export class BranchController extends Controller { "missing_or_invalid_parameter", ); - const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body; + const { provinceId, districtId, subDistrictId, headOfficeId, telephoneNo, ...rest } = body; const year = new Date().getFullYear(); @@ -279,7 +281,17 @@ export class BranchController extends Controller { this.setStatus(HttpStatus.CREATED); + if (record && telephoneNo) { + await prisma.branchContact.createMany({ + data: + typeof telephoneNo === "string" + ? [{ telephoneNo, branchId: record.id }] + : telephoneNo.map((v) => ({ telephoneNo: v, branchId: record.id })), + }); + } + return Object.assign(record, { + contact: await prisma.branchContact.findMany({ where: { branchId: record.id } }), qrCodeImageUrl: await minio.presignedGetObject( MINIO_BUCKET, lineImageLoc(record.id),