From c3e16d058068da0f59f1c5850dc35f508168bf56 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:36:11 +0700 Subject: [PATCH] refactor: adjust code --- src/controllers/customer-controller.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts index f97ca6d..2a51ad1 100644 --- a/src/controllers/customer-controller.ts +++ b/src/controllers/customer-controller.ts @@ -24,8 +24,11 @@ if (!process.env.MINIO_BUCKET) { } const MINIO_BUCKET = process.env.MINIO_BUCKET; +const MANAGE_ROLES = ["system", "head_of_admin", "admin", "head_of_sale", "sale"]; export type CustomerCreate = { + registeredBranchId?: string; + status?: Status; personName: string; personNameEN?: string; @@ -68,6 +71,8 @@ export type CustomerCreate = { }; export type CustomerUpdate = { + registeredBranchId?: string; + status?: "ACTIVE" | "INACTIVE"; personName?: string; personNameEN?: string; @@ -232,7 +237,7 @@ export class CustomerController extends Controller { } @Post() - @Security("keycloak", ["system", "head_of_admin", "admin", "head_of_sale", "sale"]) + @Security("keycloak", MANAGE_ROLES) async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) { const { customerBranch, ...payload } = body; @@ -250,10 +255,11 @@ export class CustomerController extends Controller { return acc; }, []); - const [province, district, subDistrict] = await prisma.$transaction([ + const [province, district, subDistrict, branch] = await prisma.$transaction([ prisma.province.findMany({ where: { id: { in: provinceId } } }), prisma.district.findMany({ where: { id: { in: districtId } } }), prisma.subDistrict.findMany({ where: { id: { in: subDistrictId } } }), + prisma.branch.findFirst({ where: { id: body.registeredBranchId } }), ]); if (provinceId && province.length !== provinceId?.length) { @@ -277,6 +283,13 @@ export class CustomerController extends Controller { "relationSubDistrictNotFound", ); } + if (body.registeredBranchId && !branch) { + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Branch cannot be found.", + "relationBranchNotFound", + ); + } const record = await prisma.$transaction( async (tx) => { @@ -344,7 +357,7 @@ export class CustomerController extends Controller { } @Put("{customerId}") - @Security("keycloak", ["system", "head_of_admin", "admin", "head_of_sale", "sale"]) + @Security("keycloak", MANAGE_ROLES) async editById( @Path() customerId: string, @Request() req: RequestWithUser, @@ -504,7 +517,7 @@ export class CustomerController extends Controller { } @Delete("{customerId}") - @Security("keycloak", ["system", "head_of_admin", "admin", "head_of_sale", "sale"]) + @Security("keycloak", MANAGE_ROLES) async deleteById(@Path() customerId: string) { const record = await prisma.customer.findFirst({ where: { id: customerId } });