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