122 lines
3 KiB
TypeScript
122 lines
3 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Path,
|
|
Post,
|
|
Put,
|
|
Request,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
} from "tsoa";
|
|
import { RequestWithUser } from "../interfaces/user";
|
|
import prisma from "../db";
|
|
import HttpStatus from "../interfaces/http-status";
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
type EmployeeWorkCreate = {
|
|
ownerName: string;
|
|
positionName: string;
|
|
jobType: string;
|
|
workplace: string;
|
|
workPermitNo: string;
|
|
workPermitIssuDate: Date;
|
|
workPermitExpireDate: Date;
|
|
workEndDate: Date;
|
|
};
|
|
|
|
type EmployeeWorkUpdate = {
|
|
ownerName?: string;
|
|
positionName?: string;
|
|
jobType?: string;
|
|
workplace?: string;
|
|
workPermitNo?: string;
|
|
workPermitIssuDate?: Date;
|
|
workPermitExpireDate?: Date;
|
|
workEndDate?: Date;
|
|
};
|
|
|
|
@Route("api/v1/employee/{employeeId}/work")
|
|
@Tags("Employee Work")
|
|
@Security("keycloak")
|
|
export class EmployeeWorkController extends Controller {
|
|
@Get()
|
|
async list(@Path() employeeId: string) {
|
|
return prisma.employeeWork.findMany({
|
|
orderBy: { createdAt: "asc" },
|
|
where: { employeeId },
|
|
});
|
|
}
|
|
|
|
@Get("{workId}")
|
|
async getById(@Path() employeeId: string, @Path() workId: string) {
|
|
const record = await prisma.employeeWork.findFirst({
|
|
where: { id: workId, employeeId },
|
|
});
|
|
if (!record) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "data_not_found");
|
|
}
|
|
return record;
|
|
}
|
|
|
|
@Post()
|
|
async create(
|
|
@Request() req: RequestWithUser,
|
|
@Path() employeeId: string,
|
|
@Body() body: EmployeeWorkCreate,
|
|
) {
|
|
if (!(await prisma.employee.findUnique({ where: { id: employeeId } })))
|
|
throw new HttpError(
|
|
HttpStatus.BAD_REQUEST,
|
|
"Employee cannot be found.",
|
|
"missing_or_invalid_parameter",
|
|
);
|
|
|
|
const record = await prisma.employeeWork.create({
|
|
data: {
|
|
...body,
|
|
employee: { connect: { id: employeeId } },
|
|
createdBy: req.user.name,
|
|
updateBy: req.user.name,
|
|
},
|
|
});
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
return record;
|
|
}
|
|
|
|
@Put("{workId}")
|
|
async editById(
|
|
@Request() req: RequestWithUser,
|
|
@Path() employeeId: string,
|
|
@Path() workId: string,
|
|
@Body() body: EmployeeWorkUpdate,
|
|
) {
|
|
if (!(await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }))) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "data_not_found");
|
|
}
|
|
|
|
const record = await prisma.employeeWork.update({
|
|
where: { id: workId, employeeId },
|
|
data: { ...body, createdBy: req.user.name, updateBy: req.user.name },
|
|
});
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
return record;
|
|
}
|
|
|
|
@Delete("{workId}")
|
|
async deleteById(@Path() employeeId: string, @Path() workId: string) {
|
|
const record = await prisma.employeeWork.findFirst({ where: { id: workId, employeeId } });
|
|
|
|
if (!record) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "data_not_found");
|
|
}
|
|
|
|
return await prisma.employeeWork.delete({ where: { id: workId, employeeId } });
|
|
}
|
|
}
|