hrms-api-salary/src/controllers/SalaryRankEmployeeController.ts

170 lines
5.9 KiB
TypeScript
Raw Normal View History

import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
Query,
} from "tsoa";
import { Not } 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";
@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(
@Body()
requestBody: CreateSalaryRankEmployee,
@Request() request: { user: Record<string, any> },
) {
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);
salaryRankEmployee.createdUserId = request.user.sub;
salaryRankEmployee.createdFullName = request.user.name;
salaryRankEmployee.lastUpdateUserId = request.user.sub;
salaryRankEmployee.lastUpdateFullName = request.user.name;
await this.salaryRankEmployeeRepository.save(salaryRankEmployee);
return new HttpSuccess();
} catch (error: any) {
throw new Error(error);
}
}
/**
* API
*
*
* @param {string} id Id
*/
@Put("{id}")
async updateSalaryRankEmployees(
@Path() id: string,
@Body()
requestBody: UpdateSalaryRankEmployee,
@Request() request: { user: Record<string, any> },
) {
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, "ไม่สามารถแก้ไขได้ เนื่องจากลำดับขั้นนี้ซ้ำ");
}
salaryRankEmployee.lastUpdateUserId = request.user.sub;
salaryRankEmployee.lastUpdateFullName = request.user.name;
this.salaryRankEmployeeRepository.merge(salaryRankEmployee, requestBody);
await this.salaryRankEmployeeRepository.save(salaryRankEmployee);
return new HttpSuccess();
}
/**
* API
*
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteSalaryRankEmployees(@Path() id: string) {
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(
@Path() id: string,
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
const [salaryRankEmployee, total] = await this.salaryRankEmployeeRepository.findAndCount({
where: {
salaryEmployeeId: id,
},
select: ["id", "step", "salaryMonth", "salaryDay"],
order: {
step: "ASC",
},
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
});
if (keyword != undefined && keyword !== "") {
const filteredSalaryRankEmployee = salaryRankEmployee.filter(
(x) =>
x.step?.toString().includes(keyword) ||
x.salaryMonth?.toString().includes(keyword) ||
x.salaryDay?.toString().includes(keyword),
);
const slicedData = filteredSalaryRankEmployee.slice((page - 1) * pageSize, page * pageSize);
return new HttpSuccess({ data: slicedData, total: filteredSalaryRankEmployee.length });
}
return new HttpSuccess({ data: salaryRankEmployee, total });
}
}