refactor: use utility function

This commit is contained in:
Methapon Metanipat 2024-09-10 16:49:18 +07:00
parent 42d5e3e26d
commit 2fff5f7e47

View file

@ -26,6 +26,7 @@ import {
} from "../services/permission";
import { filterStatus } from "../services/prisma";
import { connectOrDisconnect, connectOrNot } from "../utils/relation";
import { throwNotFound } from "../utils/error";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -297,9 +298,7 @@ export class BranchController extends Controller {
where: { id: branchId },
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
}
if (!record) return throwNotFound("Branch");
return record;
}
@ -541,16 +540,10 @@ export class BranchController extends Controller {
where: { id: branchId },
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
}
if (!record) return throwNotFound("Branch");
await permissionCheck(req.user, record);
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
}
if (record.status !== Status.CREATED) {
throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "branchInUsed");
}
@ -596,9 +589,7 @@ export class BranchFileController extends Controller {
include: branchRelationPermInclude(user),
where: { id },
});
if (!data) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
}
if (!data) return throwNotFound("Branch");
await permissionCheck(user, data);
}