refactor: adjust code

This commit is contained in:
Methapon2001 2024-07-03 14:36:11 +07:00
parent 6d367aa610
commit c3e16d0580

View file

@ -24,8 +24,11 @@ if (!process.env.MINIO_BUCKET) {
} }
const MINIO_BUCKET = 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 = { export type CustomerCreate = {
registeredBranchId?: string;
status?: Status; status?: Status;
personName: string; personName: string;
personNameEN?: string; personNameEN?: string;
@ -68,6 +71,8 @@ export type CustomerCreate = {
}; };
export type CustomerUpdate = { export type CustomerUpdate = {
registeredBranchId?: string;
status?: "ACTIVE" | "INACTIVE"; status?: "ACTIVE" | "INACTIVE";
personName?: string; personName?: string;
personNameEN?: string; personNameEN?: string;
@ -232,7 +237,7 @@ export class CustomerController extends Controller {
} }
@Post() @Post()
@Security("keycloak", ["system", "head_of_admin", "admin", "head_of_sale", "sale"]) @Security("keycloak", MANAGE_ROLES)
async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) { async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) {
const { customerBranch, ...payload } = body; const { customerBranch, ...payload } = body;
@ -250,10 +255,11 @@ export class CustomerController extends Controller {
return acc; 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.province.findMany({ where: { id: { in: provinceId } } }),
prisma.district.findMany({ where: { id: { in: districtId } } }), prisma.district.findMany({ where: { id: { in: districtId } } }),
prisma.subDistrict.findMany({ where: { id: { in: subDistrictId } } }), prisma.subDistrict.findMany({ where: { id: { in: subDistrictId } } }),
prisma.branch.findFirst({ where: { id: body.registeredBranchId } }),
]); ]);
if (provinceId && province.length !== provinceId?.length) { if (provinceId && province.length !== provinceId?.length) {
@ -277,6 +283,13 @@ export class CustomerController extends Controller {
"relationSubDistrictNotFound", "relationSubDistrictNotFound",
); );
} }
if (body.registeredBranchId && !branch) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch cannot be found.",
"relationBranchNotFound",
);
}
const record = await prisma.$transaction( const record = await prisma.$transaction(
async (tx) => { async (tx) => {
@ -344,7 +357,7 @@ export class CustomerController extends Controller {
} }
@Put("{customerId}") @Put("{customerId}")
@Security("keycloak", ["system", "head_of_admin", "admin", "head_of_sale", "sale"]) @Security("keycloak", MANAGE_ROLES)
async editById( async editById(
@Path() customerId: string, @Path() customerId: string,
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@ -504,7 +517,7 @@ export class CustomerController extends Controller {
} }
@Delete("{customerId}") @Delete("{customerId}")
@Security("keycloak", ["system", "head_of_admin", "admin", "head_of_sale", "sale"]) @Security("keycloak", MANAGE_ROLES)
async deleteById(@Path() customerId: string) { async deleteById(@Path() customerId: string) {
const record = await prisma.customer.findFirst({ where: { id: customerId } }); const record = await prisma.customer.findFirst({ where: { id: customerId } });