hrms-api-salary/src/controllers/SalaryRankEmployeeController.ts
2024-10-22 08:21:00 +07:00

186 lines
7 KiB
TypeScript

import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
Query,
} from "tsoa";
import { Not, Brackets } from "typeorm";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import {
CreateSalaryRankEmployee,
SalaryRankEmployee,
UpdateSalaryRankEmployee,
} from "../entities/SalaryRankEmployee";
import { SalaryEmployee } from "../entities/SalaryEmployee";
import permission from "../interfaces/permission";
import { RequestWithUser } from "../middlewares/user";
import { setLogDataDiff } from "../interfaces/utils";
@Route("api/v1/salary/rate/employee")
@Tags("SalaryRankEmployee")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class SalaryRankEmployeeController extends Controller {
private salaryRankEmployeeRepository = AppDataSource.getRepository(SalaryRankEmployee);
private salaryEmployeeRepository = AppDataSource.getRepository(SalaryEmployee);
/**
* API สร้างอัตราเงินเดือนลูกจ้าง
*
*
*/
@Post()
async CreateSalaryRankEmployee(
@Request() request: RequestWithUser,
@Body()
requestBody: CreateSalaryRankEmployee,
) {
await new permission().PermissionCreate(request, "SYS_WAGE_CHART_EMP");
try {
const checkSalary = await this.salaryEmployeeRepository.findOne({
where: { id: requestBody.salaryEmployeeId },
});
if (!checkSalary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
}
const checkStep = await this.salaryRankEmployeeRepository.find({
where: {
step: requestBody.step,
salaryEmployeeId: requestBody.salaryEmployeeId,
},
});
if (checkStep.length > 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถเพิ่มได้ เนื่องจากลำดับขั้นนี้ซ้ำ");
}
const salaryRankEmployee = Object.assign(new SalaryRankEmployee(), requestBody);
const before = null;
salaryRankEmployee.createdUserId = request.user.sub;
salaryRankEmployee.createdFullName = request.user.name;
salaryRankEmployee.lastUpdateUserId = request.user.sub;
salaryRankEmployee.lastUpdateFullName = request.user.name;
salaryRankEmployee.createdAt = new Date();
salaryRankEmployee.lastUpdatedAt = new Date();
await this.salaryRankEmployeeRepository.save(salaryRankEmployee, { data: request });
setLogDataDiff(request, { before, after: salaryRankEmployee });
return new HttpSuccess();
} catch (error: any) {
throw new Error(error);
}
}
/**
* API แก้ไขอัตราเงินเดือนลูกจ้าง
*
*
* @param {string} id Id อัตราเงินเดือน
*/
@Put("{id}")
async updateSalaryRankEmployees(
@Request() request: RequestWithUser,
@Path() id: string,
@Body()
requestBody: UpdateSalaryRankEmployee,
) {
await new permission().PermissionUpdate(request, "SYS_WAGE_CHART_EMP");
const salaryRankEmployee = await this.salaryRankEmployeeRepository.findOne({
where: { id: id },
});
if (!salaryRankEmployee) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
}
const checkStep = await this.salaryRankEmployeeRepository.find({
where: {
id: Not(id),
step: requestBody.step,
salaryEmployeeId: salaryRankEmployee.salaryEmployeeId,
},
});
if (checkStep.length > 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถแก้ไขได้ เนื่องจากลำดับขั้นนี้ซ้ำ");
}
const before = structuredClone(salaryRankEmployee);
salaryRankEmployee.lastUpdateUserId = request.user.sub;
salaryRankEmployee.lastUpdateFullName = request.user.name;
salaryRankEmployee.lastUpdatedAt = new Date();
this.salaryRankEmployeeRepository.merge(salaryRankEmployee, requestBody);
await this.salaryRankEmployeeRepository.save(salaryRankEmployee, { data: request });
setLogDataDiff(request, { before, after: salaryRankEmployee });
return new HttpSuccess();
}
/**
* API ลบอัตราเงินเดือนลูกจ้าง
*
*
* @param {string} id Id อัตราเงินเดือน
*/
@Delete("{id}")
async deleteSalaryRankEmployees(@Request() request: RequestWithUser, @Path() id: string) {
await new permission().PermissionDelete(request, "SYS_WAGE_CHART_EMP");
const delSalaryRankEmployees = await this.salaryRankEmployeeRepository.findOne({
where: { id },
});
if (!delSalaryRankEmployees) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
}
await this.salaryRankEmployeeRepository.delete({ id: id });
return new HttpSuccess();
}
/**
* API รายการอัตราเงินเดือนลูกจ้าง
*
*
* @param {string} id Id ผังเงินเดือน
*/
@Get("{id}")
async listSalaryRankEmployees(
@Request() request: RequestWithUser,
@Path() id: string,
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
let _workflow = await new permission().Workflow(request, id, "SYS_WAGE_CHART_EMP");
if (_workflow == false) await new permission().PermissionGet(request, "SYS_WAGE_CHART_EMP");
const [salaryRankEmployee, total] = await AppDataSource.getRepository(SalaryRankEmployee)
.createQueryBuilder("salaryRankEmployee")
.andWhere(
new Brackets((qb) => {
qb.andWhere(`salaryRankEmployee.salaryEmployeeId LIKE :id`, { id: id }).andWhere(
new Brackets((qb) => {
qb.orWhere("salaryRankEmployee.step LIKE :keyword", { keyword: `%${keyword}%` })
.orWhere("salaryRankEmployee.salaryMonth LIKE :keyword", {
keyword: `%${keyword}%`,
})
.orWhere("salaryRankEmployee.salaryDay LIKE :keyword", { keyword: `%${keyword}%` });
}),
);
}),
)
.orderBy("salaryRankEmployee.salaryMonth", "DESC")
.addOrderBy("salaryRankEmployee.salaryDay", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ data: salaryRankEmployee, total });
}
}