From aa182a314eccb078d9f72fae66e51ead84a86591 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:54:49 +0700 Subject: [PATCH 1/7] feat: branch contact controller --- src/controllers/branch/contact-controller.ts | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/controllers/branch/contact-controller.ts diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts new file mode 100644 index 0000000..f782271 --- /dev/null +++ b/src/controllers/branch/contact-controller.ts @@ -0,0 +1,32 @@ +import { + Body, + Controller, + Delete, + Get, + Patch, + Path, + Post, + Query, + Request, + Route, + Security, + Tags, +} 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; + +@Route("api/branch/{branchId}/contact") +@Tags("Branch Contact") +@Security("keycloak") +export class BranchContactController extends Controller { +} From 5aded1d21527309d801e435d4fc121e393c996ce Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:55:54 +0700 Subject: [PATCH 2/7] feat: create branch contact endpoint --- src/controllers/branch/contact-controller.ts | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts index f782271..479dc5b 100644 --- a/src/controllers/branch/contact-controller.ts +++ b/src/controllers/branch/contact-controller.ts @@ -29,4 +29,30 @@ const MINIO_BUCKET = process.env.MINIO_BUCKET; @Tags("Branch Contact") @Security("keycloak") export class BranchContactController extends Controller { + @Post() + async createBranchContact( + @Request() req: RequestWithUser, + @Path() branchId: string, + @Body() body: BranchContactCreate, + ) { + if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Branch not found."); + } + const record = await prisma.branchContact.create({ + include: { branch: true }, + data: { ...body, branchId, createdBy: req.user.name, updateBy: req.user.name }, + }); + return Object.assign(record, { + qrCodeImageUrl: await minio.presignedGetObject( + MINIO_BUCKET, + `branch/contact-${record.id}`, + 12 * 60 * 60, + ), + qrCodeImageUploadUrl: await minio.presignedPutObject( + MINIO_BUCKET, + `branch/contact-${record.id}`, + 12 * 60 * 60, + ), + }); + } } From 712a0952dde66c1c480db4b376aa83e52f8cdc02 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:56:40 +0700 Subject: [PATCH 3/7] feat: delete branch contact endpoint --- src/controllers/branch/contact-controller.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts index 479dc5b..ecd4917 100644 --- a/src/controllers/branch/contact-controller.ts +++ b/src/controllers/branch/contact-controller.ts @@ -55,4 +55,10 @@ export class BranchContactController extends Controller { ), }); } + + @Delete("{contactId}") + async deleteBranchContact(@Path() branchId: string, @Path() contactId: string) { + const result = await prisma.branchContact.deleteMany({ where: { id: contactId, branchId } }); + if (result.count <= 0) throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found."); + } } From 1cea8830d600da6be99f548902ba60719f86269c Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:01:59 +0700 Subject: [PATCH 4/7] refactor: use same function to get img location --- src/controllers/branch/contact-controller.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts index ecd4917..3f22d75 100644 --- a/src/controllers/branch/contact-controller.ts +++ b/src/controllers/branch/contact-controller.ts @@ -25,6 +25,10 @@ if (!process.env.MINIO_BUCKET) { const MINIO_BUCKET = process.env.MINIO_BUCKET; +function imageLocation(id: string) { + return `branch/contact-${id}`; +} + @Route("api/branch/{branchId}/contact") @Tags("Branch Contact") @Security("keycloak") @@ -45,12 +49,12 @@ export class BranchContactController extends Controller { return Object.assign(record, { qrCodeImageUrl: await minio.presignedGetObject( MINIO_BUCKET, - `branch/contact-${record.id}`, + imageLocation(record.id), 12 * 60 * 60, ), qrCodeImageUploadUrl: await minio.presignedPutObject( MINIO_BUCKET, - `branch/contact-${record.id}`, + imageLocation(record.id), 12 * 60 * 60, ), }); From e58141b864f2927b65b82129c9e994dbf8b7208b Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:08:07 +0700 Subject: [PATCH 5/7] fix: missing type --- src/controllers/branch/contact-controller.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts index 3f22d75..a1bcb3d 100644 --- a/src/controllers/branch/contact-controller.ts +++ b/src/controllers/branch/contact-controller.ts @@ -25,6 +25,16 @@ if (!process.env.MINIO_BUCKET) { const MINIO_BUCKET = process.env.MINIO_BUCKET; +type BranchContactCreate = { + lineId: string; + telephoneNo: string; +}; + +type BranchContactUpdate = { + lineId?: string; + telephoneNo?: string; +}; + function imageLocation(id: string) { return `branch/contact-${id}`; } From 63de7b3e52582fedb3ed3a2fe624ff1ab9da5fcd Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:21:19 +0700 Subject: [PATCH 6/7] feat: edit branch endpoint --- src/controllers/branch/contact-controller.ts | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts index a1bcb3d..dcb7541 100644 --- a/src/controllers/branch/contact-controller.ts +++ b/src/controllers/branch/contact-controller.ts @@ -70,6 +70,35 @@ export class BranchContactController extends Controller { }); } + @Patch("{contactId}") + async editBranchContact( + @Request() req: RequestWithUser, + @Body() body: BranchContactUpdate, + @Path() branchId: string, + @Path() contactId: string, + ) { + const record = await prisma.branchContact.update({ + include: { branch: true }, + data: { ...body, updateBy: req.user.name }, + where: { id: contactId, branchId }, + }); + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found."); + } + return Object.assign(record, { + qrCodeImageUrl: await minio.presignedGetObject( + MINIO_BUCKET, + imageLocation(record.id), + 12 * 60 * 60, + ), + qrCodeImageUploadUrl: await minio.presignedPutObject( + MINIO_BUCKET, + imageLocation(record.id), + 12 * 60 * 60, + ), + }); + } + @Delete("{contactId}") async deleteBranchContact(@Path() branchId: string, @Path() contactId: string) { const result = await prisma.branchContact.deleteMany({ where: { id: contactId, branchId } }); From d7b7a6ebee6cf983ccb4669d92d17e886b82a920 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:25:28 +0700 Subject: [PATCH 7/7] feat: get branch endpoint --- src/controllers/branch/contact-controller.ts | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts index dcb7541..a673f2d 100644 --- a/src/controllers/branch/contact-controller.ts +++ b/src/controllers/branch/contact-controller.ts @@ -43,6 +43,35 @@ function imageLocation(id: string) { @Tags("Branch Contact") @Security("keycloak") export class BranchContactController extends Controller { + @Get() + async getBranchContact( + @Path() branchId: string, + @Query() page: number = 1, + @Query() pageSize: number = 30, + ) { + const [result, total] = await prisma.$transaction([ + prisma.branchContact.findMany({ + where: { branchId }, + take: pageSize, + skip: (page - 1) * pageSize, + }), + prisma.branchContact.count({ where: { branchId } }), + ]); + + return { result, page, pageSize, total }; + } + + @Get("{contactId}") + async getBranchContactById(@Path() branchId: string, @Path() contactId: string) { + const record = await prisma.branchContact.findFirst({ where: { id: contactId, branchId } }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found."); + + return Object.assign(record, { + qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(record.id)), + }); + } + @Post() async createBranchContact( @Request() req: RequestWithUser,