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,
|
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");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue