refactor: use relation helper function

This commit is contained in:
Methapon Metanipat 2024-09-10 16:42:11 +07:00
parent cf4d3a9967
commit 42d5e3e26d

View file

@ -26,6 +26,7 @@ import {
createPermCondition,
} from "../services/permission";
import { connectOrDisconnect, connectOrNot } from "../utils/relation";
import { throwRelationError } from "../utils/error";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -396,30 +397,10 @@ export class EmployeeController extends Controller {
},
}),
]);
if (body.provinceId !== province?.id)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Province cannot be found.",
"relationProvinceNotFound",
);
if (body.districtId !== district?.id)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"District cannot be found.",
"relationDistrictNotFound",
);
if (body.subDistrictId !== subDistrict?.id)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.",
"relationSubDistrictNotFound",
);
if (!customerBranch)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Customer Branch cannot be found.",
"relationCustomerBranchNotFound",
);
if (body.provinceId !== province?.id) return throwRelationError("Province");
if (body.districtId !== district?.id) return throwRelationError("District");
if (body.subDistrictId !== subDistrict?.id) return throwRelationError("Sub-district");
if (!customerBranch) return throwRelationError("Customer Branch");
await permissionCheck(req.user, customerBranch.customer.registeredBranch);
@ -574,30 +555,11 @@ export class EmployeeController extends Controller {
},
}),
]);
if (body.provinceId && !province)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Province cannot be found.",
"relationProvinceNotFound",
);
if (body.districtId && !district)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"District cannot be found.",
"relationDistrictNotFound",
);
if (body.subDistrictId && !subDistrict)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.",
"relationSubDistrictNotFound",
);
if (body.customerBranchId && !customerBranch)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Customer cannot be found.",
"relationCustomerNotFound",
);
if (body.provinceId && !province) return throwRelationError("Province");
if (body.districtId && !district) return throwRelationError("District");
if (body.subDistrictId && !subDistrict) return throwRelationError("SubDistrict");
if (body.customerBranchId && !customerBranch) return throwRelationError("Customer");
if (!employee) {
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "employeeNotFound");
}