refactor: throw common error with util function instead

This commit is contained in:
Methapon Metanipat 2024-10-03 09:41:47 +07:00
parent 68f9dc203e
commit 4de0e1da87
12 changed files with 67 additions and 142 deletions

View file

@ -25,7 +25,7 @@ import {
} from "../services/permission";
import { filterStatus } from "../services/prisma";
import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio";
import { notFoundError, relationError } from "../utils/error";
import { isUsedError, notFoundError, relationError } from "../utils/error";
import { connectOrNot } from "../utils/relation";
const MANAGE_ROLES = [
@ -361,13 +361,7 @@ export class CustomerController extends Controller {
await permissionCheck(req.user, branch);
}
if (!!body.registeredBranchId && !branch) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch cannot be found.",
"relationBranchNotFound",
);
}
if (!!body.registeredBranchId && !branch) throw relationError("Branch");
let companyBefore = (customer.registeredBranch.headOffice || customer.registeredBranch).code;
let companyAfter =
@ -418,15 +412,11 @@ export class CustomerController extends Controller {
},
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
}
if (!record) throw notFoundError("Customer");
await permissionCheck(req.user, record.registeredBranch);
if (record.status !== Status.CREATED) {
throw new HttpError(HttpStatus.FORBIDDEN, "Customer is in used.", "customerInUsed");
}
if (record.status !== Status.CREATED) throw isUsedError("Customer");
return await prisma.customer
.delete({ where: { id: customerId } })