diff --git a/src/controllers/03-employee-checkup-controller.ts b/src/controllers/03-employee-checkup-controller.ts index 0568b4a..dab5d8d 100644 --- a/src/controllers/03-employee-checkup-controller.ts +++ b/src/controllers/03-employee-checkup-controller.ts @@ -17,6 +17,8 @@ import prisma from "../db"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { permissionCheck } from "../middlewares/employee"; +import { notFoundError, relationError } from "../utils/error"; +import { connectOrNot } from "../utils/relation"; const MANAGE_ROLES = [ "system", @@ -73,13 +75,7 @@ export class EmployeeCheckupController extends Controller { }, where: { id: checkupId, employeeId }, }); - if (!record) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Employee checkup cannot be found.", - "employeeCheckupNotFound", - ); - } + if (!record) throw notFoundError("Employee Other Info"); return record; } @@ -90,23 +86,12 @@ export class EmployeeCheckupController extends Controller { @Path() employeeId: string, @Body() body: EmployeeCheckupPayload, ) { - if (body.provinceId || employeeId) { - const [province, employee] = await prisma.$transaction([ + if (body.provinceId) { + const [province] = await prisma.$transaction([ prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), prisma.employee.findFirst({ where: { id: employeeId } }), ]); - if (body.provinceId && !province) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Province cannot be found.", - "provinceNotFound", - ); - if (!employee) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Employee cannot be found.", - "employeeNotFound", - ); + if (body.provinceId && !province) throw relationError("Province"); } const { provinceId, ...rest } = body; @@ -115,7 +100,7 @@ export class EmployeeCheckupController extends Controller { include: { province: true, createdBy: true, updatedBy: true }, data: { ...rest, - province: { connect: provinceId ? { id: provinceId } : undefined }, + province: connectOrNot(provinceId), employee: { connect: { id: employeeId } }, createdBy: { connect: { id: req.user.sub } }, updatedBy: { connect: { id: req.user.sub } }, @@ -136,38 +121,19 @@ export class EmployeeCheckupController extends Controller { @Body() body: EmployeeCheckupPayload, ) { if (body.provinceId || employeeId) { - const [province, employee] = await prisma.$transaction([ + const [province] = await prisma.$transaction([ prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), - prisma.employee.findFirst({ where: { id: employeeId } }), ]); - if (body.provinceId && !province) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Province cannot be found.", - "provinceNotFound", - ); - if (!employee) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Employee cannot be found.", - "employeeNotFound", - ); + if (body.provinceId && !province) throw relationError("Province"); } const { provinceId, ...rest } = body; - if ( - !(await prisma.employeeCheckup.findUnique({ - include: { createdBy: true, updatedBy: true }, - where: { id: checkupId, employeeId }, - })) - ) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Employee checkup cannot be found.", - "employeeCheckupNotFound", - ); - } + const checkup = await prisma.employeeCheckup.findUnique({ + where: { id: checkupId, employeeId }, + }); + + if (!checkup) throw notFoundError("Employee Other Info"); const record = await prisma.employeeCheckup.update({ include: { province: true, createdBy: true, updatedBy: true }, diff --git a/src/controllers/03-employee-other-info-controller.ts b/src/controllers/03-employee-other-info-controller.ts index 969a12e..b56e6b5 100644 --- a/src/controllers/03-employee-other-info-controller.ts +++ b/src/controllers/03-employee-other-info-controller.ts @@ -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: { diff --git a/src/controllers/03-employee-work-controller.ts b/src/controllers/03-employee-work-controller.ts index ee84d8b..c2f74b0 100644 --- a/src/controllers/03-employee-work-controller.ts +++ b/src/controllers/03-employee-work-controller.ts @@ -15,8 +15,8 @@ import { import { RequestWithUser } from "../interfaces/user"; import prisma from "../db"; import HttpStatus from "../interfaces/http-status"; -import HttpError from "../interfaces/http-error"; import { permissionCheck } from "../middlewares/employee"; +import { notFoundError } from "../utils/error"; const MANAGE_ROLES = [ "system", @@ -71,13 +71,7 @@ export class EmployeeWorkController extends Controller { }, where: { id: workId, employeeId }, }); - if (!record) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Employee work cannot be found.", - "employeeWorkNotFound", - ); - } + if (!record) throw notFoundError("Employee Work"); return record; } @@ -88,9 +82,6 @@ export class EmployeeWorkController extends Controller { @Path() employeeId: string, @Body() body: EmployeeWorkPayload, ) { - if (!(await prisma.employee.findUnique({ where: { id: employeeId } }))) - throw new HttpError(HttpStatus.BAD_REQUEST, "Employee cannot be found.", "employeeBadReq"); - const record = await prisma.employeeWork.create({ include: { createdBy: true, @@ -117,13 +108,9 @@ export class EmployeeWorkController extends Controller { @Path() workId: string, @Body() body: EmployeeWorkPayload, ) { - if (!(await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }))) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Employee work cannot be found.", - "employeeWorkNotFound", - ); - } + const work = await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }); + + if (!work) throw notFoundError("Employee Work"); const record = await prisma.employeeWork.update({ include: { @@ -150,13 +137,7 @@ export class EmployeeWorkController extends Controller { where: { id: workId, employeeId }, }); - if (!record) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Employee work cannot be found.", - "employeeWorkNotFound", - ); - } + if (!record) throw notFoundError("Employee Work"); return await prisma.employeeWork.delete({ where: { id: workId, employeeId } }); } diff --git a/src/utils/minio.ts b/src/utils/minio.ts index 1937e3a..8c93a40 100644 --- a/src/utils/minio.ts +++ b/src/utils/minio.ts @@ -67,8 +67,8 @@ export const fileLocation = { visa: (employeeId: string, visaId?: string) => `employee/visa-${employeeId}/${visaId || ""}`, passport: (employeeId: string, passportId?: string) => `employee/passport-${employeeId}/${passportId || ""}`, - reportInCountry: (employeeId: string, reportId?: string) => - `employee/report-in-country-${employeeId}/${reportId || ""}`, + inCountryNotice: (employeeId: string, noticeId?: string) => + `employee/in-country-notice-${employeeId}/${noticeId || ""}`, }, product: { img: (productId: string, name?: string) => `product/img-${productId}/${name || ""}`,