148 lines
3.6 KiB
TypeScript
148 lines
3.6 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";
|
|
|
|
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "branch_manager", "head_of_sale", "sale"];
|
|
|
|
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;
|
|
};
|
|
|
|
@Route("api/v1/employee/{employeeId}/work")
|
|
@Tags("Employee Work")
|
|
export class EmployeeWorkController extends Controller {
|
|
@Get()
|
|
@Security("keycloak")
|
|
async list(@Path() employeeId: string) {
|
|
return prisma.employeeWork.findMany({
|
|
include: {
|
|
createdBy: true,
|
|
updatedBy: true,
|
|
},
|
|
orderBy: { createdAt: "asc" },
|
|
where: { employeeId },
|
|
});
|
|
}
|
|
|
|
@Get("{workId}")
|
|
@Security("keycloak")
|
|
async getById(@Path() employeeId: string, @Path() workId: string) {
|
|
const record = await prisma.employeeWork.findFirst({
|
|
include: {
|
|
createdBy: true,
|
|
updatedBy: true,
|
|
},
|
|
where: { id: workId, employeeId },
|
|
});
|
|
if (!record) {
|
|
throw new HttpError(
|
|
HttpStatus.NOT_FOUND,
|
|
"Employee work cannot be found.",
|
|
"employeeWorkNotFound",
|
|
);
|
|
}
|
|
return record;
|
|
}
|
|
|
|
@Post()
|
|
@Security("keycloak", MANAGE_ROLES)
|
|
async create(
|
|
@Request() req: RequestWithUser,
|
|
@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,
|
|
updatedBy: true,
|
|
},
|
|
data: {
|
|
...body,
|
|
employee: { connect: { id: employeeId } },
|
|
createdBy: { connect: { id: req.user.sub } },
|
|
updatedBy: { connect: { id: req.user.sub } },
|
|
},
|
|
});
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
return record;
|
|
}
|
|
|
|
@Put("{workId}")
|
|
@Security("keycloak", MANAGE_ROLES)
|
|
async editById(
|
|
@Request() req: RequestWithUser,
|
|
@Path() employeeId: string,
|
|
@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 record = await prisma.employeeWork.update({
|
|
include: {
|
|
createdBy: true,
|
|
updatedBy: true,
|
|
},
|
|
where: { id: workId, employeeId },
|
|
data: { ...body, updatedByUserId: req.user.sub },
|
|
});
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
return record;
|
|
}
|
|
|
|
@Delete("{workId}")
|
|
@Security("keycloak", MANAGE_ROLES)
|
|
async deleteById(@Path() employeeId: string, @Path() workId: string) {
|
|
const record = await prisma.employeeWork.findFirst({
|
|
include: {
|
|
createdBy: true,
|
|
updatedBy: true,
|
|
},
|
|
where: { id: workId, employeeId },
|
|
});
|
|
|
|
if (!record) {
|
|
throw new HttpError(
|
|
HttpStatus.NOT_FOUND,
|
|
"Employee work cannot be found.",
|
|
"employeeWorkNotFound",
|
|
);
|
|
}
|
|
|
|
return await prisma.employeeWork.delete({ where: { id: workId, employeeId } });
|
|
}
|
|
}
|