refactor: throw common error with util function instead
This commit is contained in:
parent
68f9dc203e
commit
4de0e1da87
12 changed files with 67 additions and 142 deletions
|
|
@ -18,6 +18,7 @@ import prisma from "../db";
|
|||
import { RequestWithUser } from "../interfaces/user";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { isUsedError, notFoundError } from "../utils/error";
|
||||
|
||||
type WorkCreate = {
|
||||
order: number;
|
||||
|
|
@ -83,7 +84,7 @@ export class WorkController extends Controller {
|
|||
where: { id: workId },
|
||||
});
|
||||
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
|
||||
if (!record) throw notFoundError("Work");
|
||||
|
||||
return record;
|
||||
}
|
||||
|
|
@ -219,11 +220,11 @@ export class WorkController extends Controller {
|
|||
@Body() body: WorkUpdate,
|
||||
@Path() workId: string,
|
||||
) {
|
||||
const work = await prisma.work.findUnique({ where: { id: workId } });
|
||||
|
||||
const { productId, ...payload } = body;
|
||||
|
||||
if (!(await prisma.work.findUnique({ where: { id: workId } }))) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
|
||||
}
|
||||
if (!work) throw notFoundError("Work");
|
||||
|
||||
const exist = await prisma.work.findFirst({
|
||||
include: {
|
||||
|
|
@ -313,13 +314,8 @@ export class WorkController extends Controller {
|
|||
where: { id: workId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
|
||||
}
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Work is in used.", "workInUsed");
|
||||
}
|
||||
if (!record) throw notFoundError("Work");
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Work");
|
||||
|
||||
return await prisma.work.delete({ where: { id: workId } });
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue