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