From 155962b996dfea9f11b6f875353e6dd2bf2aaec9 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Thu, 12 Sep 2024 10:39:58 +0700 Subject: [PATCH] refactor: use helper function --- src/controllers/03-customer-controller.ts | 110 ++++++---------------- 1 file changed, 29 insertions(+), 81 deletions(-) diff --git a/src/controllers/03-customer-controller.ts b/src/controllers/03-customer-controller.ts index cab6850..53ecaf7 100644 --- a/src/controllers/03-customer-controller.ts +++ b/src/controllers/03-customer-controller.ts @@ -15,7 +15,6 @@ import { } from "tsoa"; import { RequestWithUser } from "../interfaces/user"; import prisma from "../db"; -import minio, { deleteFolder } from "../services/minio"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { isSystem } from "../utils/keycloak"; @@ -25,13 +24,9 @@ import { createPermCondition, } from "../services/permission"; import { filterStatus } from "../services/prisma"; -import { fileLocation, listFile } from "../utils/minio"; +import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio"; +import { notFoundError, relationError } from "../utils/error"; -if (!process.env.MINIO_BUCKET) { - throw Error("Require MinIO bucket."); -} - -const MINIO_BUCKET = process.env.MINIO_BUCKET; const MANAGE_ROLES = [ "system", "head_of_admin", @@ -182,9 +177,7 @@ export class CustomerController extends Controller { prisma.employee.count({ where: { customerBranch: { customerId } } }), ]); - if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound"); - } + if (!record) throw notFoundError("Customer"); return Object.assign(record, { _count: { employee: countEmployee } }); } @@ -192,9 +185,7 @@ export class CustomerController extends Controller { @Security("keycloak", MANAGE_ROLES) async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) { // NOTE: handle empty string - if (!body.registeredBranchId) { - body.registeredBranchId = undefined; - } + if (!body.registeredBranchId) body.registeredBranchId = undefined; const [branch] = await prisma.$transaction([ prisma.branch.findFirst({ @@ -203,13 +194,7 @@ export class CustomerController extends Controller { }), ]); - if (!!body.registeredBranchId && !branch) { - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Branch cannot be found.", - "relationBranchNotFound", - ); - } + if (!!body.registeredBranchId && !branch) throw relationError("Branch"); if (body.registeredBranchId !== undefined && branch) { await permissionCheck(req.user, branch); @@ -262,9 +247,7 @@ export class CustomerController extends Controller { @Request() req: RequestWithUser, @Body() body: CustomerUpdate, ) { - if (body.registeredBranchId === "") { - body.registeredBranchId = undefined; - } + if (body.registeredBranchId === "") body.registeredBranchId = undefined; const customer = await prisma.customer.findUnique({ where: { id: customerId }, @@ -275,9 +258,7 @@ export class CustomerController extends Controller { }, }); - if (!customer) { - throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound"); - } + if (!customer) throw notFoundError("Branch"); const [branch] = await prisma.$transaction([ prisma.branch.findFirst({ @@ -286,13 +267,7 @@ export class CustomerController extends Controller { }), ]); - if (!!body.registeredBranchId && !branch) { - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Branch cannot be found.", - "relationBranchNotFound", - ); - } + if (!!body.registeredBranchId && !branch) throw relationError("Branch"); if (customer.registeredBranch) { await permissionCheck(req.user, customer.registeredBranch); @@ -351,24 +326,30 @@ export class CustomerController extends Controller { return await prisma.customer .delete({ where: { id: customerId } }) - .then( - async (data) => await deleteFolder(MINIO_BUCKET, `customer/${customerId}`).then(() => data), - ); + .then((data) => deleteFolder(`customer/${customerId}`).then(() => data)); } } @Route("api/v1/customer/{customerId}/image") @Tags("Customer") export class CustomerImageController extends Controller { + private async checkPermission(user: RequestWithUser["user"], id: string) { + const data = await prisma.customer.findUnique({ + include: { + registeredBranch: { + include: branchRelationPermInclude(user), + }, + }, + where: { id }, + }); + if (!data) throw notFoundError("Customer"); + await permissionCheck(user, data.registeredBranch); + } + @Get() @Security("keycloak") - async listImage(@Path() customerId: string) { - const customer = await prisma.customer.findUnique({ - where: { id: customerId }, - }); - if (!customer) { - throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found", "customerNotFound"); - } + async listImage(@Request() req: RequestWithUser, @Path() customerId: string) { + await this.checkPermission(req.user, customerId); return await listFile(fileLocation.customer.img(customerId)); } @@ -379,11 +360,7 @@ export class CustomerImageController extends Controller { @Path() name: string, ) { return req.res?.redirect( - await minio.presignedGetObject( - MINIO_BUCKET, - fileLocation.customer.img(customerId, name), - 12 * 60 * 60, - ), + await getFile(fileLocation.customer.img(customerId, name), 12 * 60 * 60), ); } @@ -394,25 +371,9 @@ export class CustomerImageController extends Controller { @Path() customerId: string, @Path() name: string, ) { - const customer = await prisma.customer.findUnique({ - where: { id: customerId }, - include: { - registeredBranch: { - include: branchRelationPermInclude(req.user), - }, - }, - }); - if (!customer) { - throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); - } - console.log(customer.registeredBranch); - await permissionCheck(req.user, customer.registeredBranch); + await this.checkPermission(req.user, customerId); return req.res?.redirect( - await minio.presignedPutObject( - MINIO_BUCKET, - fileLocation.customer.img(customerId, name), - 12 * 60 * 60, - ), + await setFile(fileLocation.customer.img(customerId, name), 12 * 60 * 60), ); } @@ -423,20 +384,7 @@ export class CustomerImageController extends Controller { @Path() customerId: string, @Path() name: string, ) { - const customer = await prisma.customer.findUnique({ - where: { id: customerId }, - include: { - registeredBranch: { - include: branchRelationPermInclude(req.user), - }, - }, - }); - if (!customer) { - throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); - } - await permissionCheck(req.user, customer.registeredBranch); - await minio.removeObject(MINIO_BUCKET, fileLocation.customer.img(customerId, name), { - forceDelete: true, - }); + await this.checkPermission(req.user, customerId); + await deleteFile(fileLocation.customer.img(customerId, name)); } }