refactor: use relation helper function
This commit is contained in:
parent
cf4d3a9967
commit
42d5e3e26d
1 changed files with 10 additions and 48 deletions
|
|
@ -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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue