jws-backend/src/controllers/employee-work-controller.ts

121 lines
3 KiB
TypeScript
Raw Normal View History

2024-06-07 14:49:11 +07:00
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";
2024-06-11 11:27:00 +07:00
type EmployeeWorkPayload = {
ownerName?: string | null;
positionName?: string | null;
jobType?: string | null;
workplace?: string | null;
workPermitNo?: string | null;
workPermitIssuDate?: Date | null;
workPermitExpireDate?: Date | null;
workEndDate?: Date | null;
remark?: string | null;
2024-06-07 14:49:11 +07:00
};
@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) {
2024-07-01 13:24:02 +07:00
throw new HttpError(
HttpStatus.NOT_FOUND,
"Employee work cannot be found.",
"employeeWorkNotFound",
);
2024-06-07 14:49:11 +07:00
}
return record;
}
@Post()
async create(
@Request() req: RequestWithUser,
@Path() employeeId: string,
2024-06-11 11:27:00 +07:00
@Body() body: EmployeeWorkPayload,
2024-06-07 14:49:11 +07:00
) {
if (!(await prisma.employee.findUnique({ where: { id: employeeId } })))
2024-07-01 13:24:02 +07:00
throw new HttpError(HttpStatus.BAD_REQUEST, "Employee cannot be found.", "employeeBadReq");
2024-06-07 14:49:11 +07:00
const record = await prisma.employeeWork.create({
data: {
...body,
employee: { connect: { id: employeeId } },
2024-07-01 13:24:02 +07:00
createdBy: { connect: { id: req.user.sub } },
updatedBy: { connect: { id: req.user.sub } },
2024-06-07 14:49:11 +07:00
},
});
this.setStatus(HttpStatus.CREATED);
return record;
}
@Put("{workId}")
async editById(
@Request() req: RequestWithUser,
@Path() employeeId: string,
@Path() workId: string,
2024-06-11 11:27:00 +07:00
@Body() body: EmployeeWorkPayload,
2024-06-07 14:49:11 +07:00
) {
if (!(await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }))) {
2024-07-01 13:24:02 +07:00
throw new HttpError(
HttpStatus.NOT_FOUND,
"Employee work cannot be found.",
"employeeWorkNotFound",
);
2024-06-07 14:49:11 +07:00
}
const record = await prisma.employeeWork.update({
where: { id: workId, employeeId },
2024-07-01 13:24:02 +07:00
data: { ...body, updatedByUserId: req.user.sub },
2024-06-07 14:49:11 +07:00
});
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) {
2024-07-01 13:24:02 +07:00
throw new HttpError(
HttpStatus.NOT_FOUND,
"Employee work cannot be found.",
"employeeWorkNotFound",
);
2024-06-07 14:49:11 +07:00
}
return await prisma.employeeWork.delete({ where: { id: workId, employeeId } });
}
}