feat: employee work endpoints
This commit is contained in:
parent
23f05503c5
commit
4b7acf9a43
1 changed files with 122 additions and 0 deletions
122
src/controllers/employee-work-controller.ts
Normal file
122
src/controllers/employee-work-controller.ts
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
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 } });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue