refactor: use helper
This commit is contained in:
parent
3394ff0caa
commit
77739da154
4 changed files with 29 additions and 93 deletions
|
|
@ -17,6 +17,8 @@ import prisma from "../db";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import { permissionCheck } from "../middlewares/employee";
|
import { permissionCheck } from "../middlewares/employee";
|
||||||
|
import { notFoundError, relationError } from "../utils/error";
|
||||||
|
import { connectOrNot } from "../utils/relation";
|
||||||
|
|
||||||
const MANAGE_ROLES = [
|
const MANAGE_ROLES = [
|
||||||
"system",
|
"system",
|
||||||
|
|
@ -73,13 +75,7 @@ export class EmployeeCheckupController extends Controller {
|
||||||
},
|
},
|
||||||
where: { id: checkupId, employeeId },
|
where: { id: checkupId, employeeId },
|
||||||
});
|
});
|
||||||
if (!record) {
|
if (!record) throw notFoundError("Employee Other Info");
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
"Employee checkup cannot be found.",
|
|
||||||
"employeeCheckupNotFound",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,23 +86,12 @@ export class EmployeeCheckupController extends Controller {
|
||||||
@Path() employeeId: string,
|
@Path() employeeId: string,
|
||||||
@Body() body: EmployeeCheckupPayload,
|
@Body() body: EmployeeCheckupPayload,
|
||||||
) {
|
) {
|
||||||
if (body.provinceId || employeeId) {
|
if (body.provinceId) {
|
||||||
const [province, employee] = await prisma.$transaction([
|
const [province] = await prisma.$transaction([
|
||||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
prisma.employee.findFirst({ where: { id: employeeId } }),
|
prisma.employee.findFirst({ where: { id: employeeId } }),
|
||||||
]);
|
]);
|
||||||
if (body.provinceId && !province)
|
if (body.provinceId && !province) throw relationError("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",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { provinceId, ...rest } = body;
|
const { provinceId, ...rest } = body;
|
||||||
|
|
@ -115,7 +100,7 @@ export class EmployeeCheckupController extends Controller {
|
||||||
include: { province: true, createdBy: true, updatedBy: true },
|
include: { province: true, createdBy: true, updatedBy: true },
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
province: connectOrNot(provinceId),
|
||||||
employee: { connect: { id: employeeId } },
|
employee: { connect: { id: employeeId } },
|
||||||
createdBy: { connect: { id: req.user.sub } },
|
createdBy: { connect: { id: req.user.sub } },
|
||||||
updatedBy: { connect: { id: req.user.sub } },
|
updatedBy: { connect: { id: req.user.sub } },
|
||||||
|
|
@ -136,38 +121,19 @@ export class EmployeeCheckupController extends Controller {
|
||||||
@Body() body: EmployeeCheckupPayload,
|
@Body() body: EmployeeCheckupPayload,
|
||||||
) {
|
) {
|
||||||
if (body.provinceId || employeeId) {
|
if (body.provinceId || employeeId) {
|
||||||
const [province, employee] = await prisma.$transaction([
|
const [province] = await prisma.$transaction([
|
||||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
prisma.employee.findFirst({ where: { id: employeeId } }),
|
|
||||||
]);
|
]);
|
||||||
if (body.provinceId && !province)
|
if (body.provinceId && !province) throw relationError("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",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { provinceId, ...rest } = body;
|
const { provinceId, ...rest } = body;
|
||||||
|
|
||||||
if (
|
const checkup = await prisma.employeeCheckup.findUnique({
|
||||||
!(await prisma.employeeCheckup.findUnique({
|
where: { id: checkupId, employeeId },
|
||||||
include: { createdBy: true, updatedBy: true },
|
});
|
||||||
where: { id: checkupId, employeeId },
|
|
||||||
}))
|
if (!checkup) throw notFoundError("Employee Other Info");
|
||||||
) {
|
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
"Employee checkup cannot be found.",
|
|
||||||
"employeeCheckupNotFound",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const record = await prisma.employeeCheckup.update({
|
const record = await prisma.employeeCheckup.update({
|
||||||
include: { province: true, createdBy: true, updatedBy: true },
|
include: { province: true, createdBy: true, updatedBy: true },
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,10 @@ import {
|
||||||
} from "tsoa";
|
} from "tsoa";
|
||||||
|
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import HttpError from "../interfaces/http-error";
|
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
import { permissionCheck } from "../middlewares/employee";
|
import { permissionCheck } from "../middlewares/employee";
|
||||||
|
import { notFoundError } from "../utils/error";
|
||||||
|
|
||||||
const MANAGE_ROLES = [
|
const MANAGE_ROLES = [
|
||||||
"system",
|
"system",
|
||||||
|
|
@ -72,9 +72,6 @@ export class EmployeeOtherInfo extends Controller {
|
||||||
@Path() employeeId: string,
|
@Path() employeeId: string,
|
||||||
@Body() body: EmployeeOtherInfoPayload,
|
@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({
|
const record = await prisma.employeeOtherInfo.create({
|
||||||
include: {
|
include: {
|
||||||
createdBy: true,
|
createdBy: true,
|
||||||
|
|
@ -101,13 +98,11 @@ export class EmployeeOtherInfo extends Controller {
|
||||||
@Path() otherInfoId: string,
|
@Path() otherInfoId: string,
|
||||||
@Body() body: EmployeeOtherInfoPayload,
|
@Body() body: EmployeeOtherInfoPayload,
|
||||||
) {
|
) {
|
||||||
if (!(await prisma.employeeOtherInfo.findUnique({ where: { id: otherInfoId, employeeId } }))) {
|
const otherInfo = await prisma.employeeOtherInfo.findUnique({
|
||||||
throw new HttpError(
|
where: { id: otherInfoId, employeeId },
|
||||||
HttpStatus.NOT_FOUND,
|
});
|
||||||
"Employee other info cannot be found.",
|
|
||||||
"employeeOtherNotFound",
|
if (!otherInfo) throw notFoundError("Employee Other Info");
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const record = await prisma.employeeOtherInfo.update({
|
const record = await prisma.employeeOtherInfo.update({
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -130,13 +125,7 @@ export class EmployeeOtherInfo extends Controller {
|
||||||
where: { id: otherInfoId, employeeId },
|
where: { id: otherInfoId, employeeId },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!record) {
|
if (!record) throw notFoundError("Employee Other Info");
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
"Employee other info cannot be found.",
|
|
||||||
"employeeOtherNotFound",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await prisma.employeeOtherInfo.delete({
|
return await prisma.employeeOtherInfo.delete({
|
||||||
include: {
|
include: {
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@ import {
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import HttpError from "../interfaces/http-error";
|
|
||||||
import { permissionCheck } from "../middlewares/employee";
|
import { permissionCheck } from "../middlewares/employee";
|
||||||
|
import { notFoundError } from "../utils/error";
|
||||||
|
|
||||||
const MANAGE_ROLES = [
|
const MANAGE_ROLES = [
|
||||||
"system",
|
"system",
|
||||||
|
|
@ -71,13 +71,7 @@ export class EmployeeWorkController extends Controller {
|
||||||
},
|
},
|
||||||
where: { id: workId, employeeId },
|
where: { id: workId, employeeId },
|
||||||
});
|
});
|
||||||
if (!record) {
|
if (!record) throw notFoundError("Employee Work");
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
"Employee work cannot be found.",
|
|
||||||
"employeeWorkNotFound",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,9 +82,6 @@ export class EmployeeWorkController extends Controller {
|
||||||
@Path() employeeId: string,
|
@Path() employeeId: string,
|
||||||
@Body() body: EmployeeWorkPayload,
|
@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({
|
const record = await prisma.employeeWork.create({
|
||||||
include: {
|
include: {
|
||||||
createdBy: true,
|
createdBy: true,
|
||||||
|
|
@ -117,13 +108,9 @@ export class EmployeeWorkController extends Controller {
|
||||||
@Path() workId: string,
|
@Path() workId: string,
|
||||||
@Body() body: EmployeeWorkPayload,
|
@Body() body: EmployeeWorkPayload,
|
||||||
) {
|
) {
|
||||||
if (!(await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }))) {
|
const work = await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } });
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.NOT_FOUND,
|
if (!work) throw notFoundError("Employee Work");
|
||||||
"Employee work cannot be found.",
|
|
||||||
"employeeWorkNotFound",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const record = await prisma.employeeWork.update({
|
const record = await prisma.employeeWork.update({
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -150,13 +137,7 @@ export class EmployeeWorkController extends Controller {
|
||||||
where: { id: workId, employeeId },
|
where: { id: workId, employeeId },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!record) {
|
if (!record) throw notFoundError("Employee Work");
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
"Employee work cannot be found.",
|
|
||||||
"employeeWorkNotFound",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await prisma.employeeWork.delete({ where: { id: workId, employeeId } });
|
return await prisma.employeeWork.delete({ where: { id: workId, employeeId } });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,8 +67,8 @@ export const fileLocation = {
|
||||||
visa: (employeeId: string, visaId?: string) => `employee/visa-${employeeId}/${visaId || ""}`,
|
visa: (employeeId: string, visaId?: string) => `employee/visa-${employeeId}/${visaId || ""}`,
|
||||||
passport: (employeeId: string, passportId?: string) =>
|
passport: (employeeId: string, passportId?: string) =>
|
||||||
`employee/passport-${employeeId}/${passportId || ""}`,
|
`employee/passport-${employeeId}/${passportId || ""}`,
|
||||||
reportInCountry: (employeeId: string, reportId?: string) =>
|
inCountryNotice: (employeeId: string, noticeId?: string) =>
|
||||||
`employee/report-in-country-${employeeId}/${reportId || ""}`,
|
`employee/in-country-notice-${employeeId}/${noticeId || ""}`,
|
||||||
},
|
},
|
||||||
product: {
|
product: {
|
||||||
img: (productId: string, name?: string) => `product/img-${productId}/${name || ""}`,
|
img: (productId: string, name?: string) => `product/img-${productId}/${name || ""}`,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue