refactor: use helper

This commit is contained in:
Methapon Metanipat 2024-09-13 17:03:46 +07:00
parent 3394ff0caa
commit 77739da154
4 changed files with 29 additions and 93 deletions

View file

@ -14,10 +14,10 @@ import {
} from "tsoa";
import prisma from "../db";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
import { permissionCheck } from "../middlewares/employee";
import { notFoundError } from "../utils/error";
const MANAGE_ROLES = [
"system",
@ -72,9 +72,6 @@ export class EmployeeOtherInfo extends Controller {
@Path() employeeId: string,
@Body() body: EmployeeOtherInfoPayload,
) {
if (!(await prisma.employee.findUnique({ where: { id: employeeId } })))
throw new HttpError(HttpStatus.BAD_REQUEST, "Employee cannot be found.", "employeeBadReq");
const record = await prisma.employeeOtherInfo.create({
include: {
createdBy: true,
@ -101,13 +98,11 @@ export class EmployeeOtherInfo extends Controller {
@Path() otherInfoId: string,
@Body() body: EmployeeOtherInfoPayload,
) {
if (!(await prisma.employeeOtherInfo.findUnique({ where: { id: otherInfoId, employeeId } }))) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Employee other info cannot be found.",
"employeeOtherNotFound",
);
}
const otherInfo = await prisma.employeeOtherInfo.findUnique({
where: { id: otherInfoId, employeeId },
});
if (!otherInfo) throw notFoundError("Employee Other Info");
const record = await prisma.employeeOtherInfo.update({
include: {
@ -130,13 +125,7 @@ export class EmployeeOtherInfo extends Controller {
where: { id: otherInfoId, employeeId },
});
if (!record) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Employee other info cannot be found.",
"employeeOtherNotFound",
);
}
if (!record) throw notFoundError("Employee Other Info");
return await prisma.employeeOtherInfo.delete({
include: {