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 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 } });