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

145 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-06-07 14:49:11 +07:00
import {
Body,
Controller,
Delete,
Get,
2024-09-06 10:54:17 +07:00
Middlewares,
2024-06-07 14:49:11 +07:00
Path,
Post,
Put,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { RequestWithUser } from "../interfaces/user";
import prisma from "../db";
import HttpStatus from "../interfaces/http-status";
2024-09-06 10:54:17 +07:00
import { permissionCheck } from "../middlewares/employee";
2024-09-13 17:03:46 +07:00
import { notFoundError } from "../utils/error";
2024-06-07 14:49:11 +07:00
const MANAGE_ROLES = [
"system",
"head_of_admin",
"admin",
"head_of_account",
"account",
"head_of_sale",
];
2024-09-06 10:54:17 +07:00
function globalAllow(user: RequestWithUser["user"]) {
const allowList = ["system", "head_of_admin", "head_of_account", "head_of_sale"];
2024-09-06 10:54:17 +07:00
return allowList.some((v) => user.roles?.includes(v));
}
2024-07-03 15:42:49 +07:00
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")
2024-09-06 10:54:17 +07:00
@Middlewares(permissionCheck(globalAllow))
2024-06-07 14:49:11 +07:00
export class EmployeeWorkController extends Controller {
@Get()
2024-07-03 15:42:49 +07:00
@Security("keycloak")
2024-06-07 14:49:11 +07:00
async list(@Path() employeeId: string) {
return prisma.employeeWork.findMany({
2024-07-01 14:38:07 +07:00
include: {
createdBy: true,
updatedBy: true,
},
2024-06-07 14:49:11 +07:00
orderBy: { createdAt: "asc" },
where: { employeeId },
});
}
@Get("{workId}")
2024-07-03 15:42:49 +07:00
@Security("keycloak")
2024-06-07 14:49:11 +07:00
async getById(@Path() employeeId: string, @Path() workId: string) {
const record = await prisma.employeeWork.findFirst({
2024-07-01 14:38:07 +07:00
include: {
createdBy: true,
updatedBy: true,
},
2024-06-07 14:49:11 +07:00
where: { id: workId, employeeId },
});
2024-09-13 17:03:46 +07:00
if (!record) throw notFoundError("Employee Work");
2024-06-07 14:49:11 +07:00
return record;
}
@Post()
2024-07-03 15:42:49 +07:00
@Security("keycloak", MANAGE_ROLES)
2024-06-07 14:49:11 +07:00
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
) {
const record = await prisma.employeeWork.create({
2024-07-01 14:38:07 +07:00
include: {
createdBy: true,
updatedBy: true,
},
2024-06-07 14:49:11 +07:00
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}")
2024-07-03 15:42:49 +07:00
@Security("keycloak", MANAGE_ROLES)
2024-06-07 14:49:11 +07:00
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
) {
2024-09-13 17:03:46 +07:00
const work = await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } });
if (!work) throw notFoundError("Employee Work");
2024-06-07 14:49:11 +07:00
const record = await prisma.employeeWork.update({
2024-07-01 14:38:07 +07:00
include: {
createdBy: true,
updatedBy: true,
},
2024-06-07 14:49:11 +07:00
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}")
2024-07-03 15:42:49 +07:00
@Security("keycloak", MANAGE_ROLES)
2024-06-07 14:49:11 +07:00
async deleteById(@Path() employeeId: string, @Path() workId: string) {
2024-07-01 14:38:07 +07:00
const record = await prisma.employeeWork.findFirst({
include: {
createdBy: true,
updatedBy: true,
},
where: { id: workId, employeeId },
});
2024-06-07 14:49:11 +07:00
2024-09-13 17:03:46 +07:00
if (!record) throw notFoundError("Employee Work");
2024-06-07 14:49:11 +07:00
return await prisma.employeeWork.delete({ where: { id: workId, employeeId } });
}
}