refactor: reduce code

This commit is contained in:
Methapon Metanipat 2024-09-12 14:31:21 +07:00
parent b9a616b9a9
commit 20725b0357

View file

@ -378,25 +378,19 @@ export class BranchController extends Controller {
update: { value: { increment: 1 } },
});
const errorBranchExists = new HttpError(
HttpStatus.BAD_REQUEST,
"Branch with same code already exists.",
"sameBranchCodeExists",
);
if (last.value === 1) {
const exist = await tx.branch.findFirst({
where: { code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(5, "0")}` },
});
if (exist)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch with same code already exists.",
"sameBranchCodeExists",
);
}
if (last.value !== 1 && !headOfficeId) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch with same code already exists.",
"sameBranchCodeExists",
);
if (exist) throw errorBranchExists;
}
if (last.value !== 1 && !headOfficeId) throw errorBranchExists;
return await tx.branch.create({
include: {