2024-03-13 09:30:04 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Response,
|
|
|
|
|
Get,
|
|
|
|
|
Query,
|
|
|
|
|
} from "tsoa";
|
2024-03-14 17:05:35 +07:00
|
|
|
import { Not } from "typeorm";
|
2024-03-13 09:30:04 +07:00
|
|
|
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,
|
2024-03-14 16:20:47 +07:00
|
|
|
SalaryRankEmployee,
|
2024-03-13 09:30:04 +07:00
|
|
|
UpdateSalaryRankEmployee,
|
2024-03-14 16:20:47 +07:00
|
|
|
} from "../entities/SalaryRankEmployee";
|
|
|
|
|
import { SalaryEmployee } from "../entities/SalaryEmployee";
|
2024-03-13 09:33:34 +07:00
|
|
|
@Route("api/v1/salary/rate/employee")
|
2024-03-13 09:30:04 +07:00
|
|
|
@Tags("SalaryRankEmployee")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
2024-03-14 16:20:47 +07:00
|
|
|
export class SalaryRankEmployeeController extends Controller {
|
|
|
|
|
private salaryRankEmployeeRepository = AppDataSource.getRepository(SalaryRankEmployee);
|
|
|
|
|
private salaryEmployeeRepository = AppDataSource.getRepository(SalaryEmployee);
|
2024-03-13 09:30:04 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API สร้างอัตราเงินเดือนลูกจ้าง
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Post()
|
|
|
|
|
async CreateSalaryRankEmployee(
|
|
|
|
|
@Body()
|
|
|
|
|
requestBody: CreateSalaryRankEmployee,
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
try {
|
2024-03-13 13:45:57 +07:00
|
|
|
const checkSalary = await this.salaryEmployeeRepository.findOne({
|
2024-03-13 09:30:04 +07:00
|
|
|
where: { id: requestBody.salaryEmployeeId },
|
|
|
|
|
});
|
|
|
|
|
if (!checkSalary) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
|
|
|
|
}
|
2024-03-14 17:05:35 +07:00
|
|
|
const checkStep = await this.salaryRankEmployeeRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
step: requestBody.step,
|
|
|
|
|
salaryEmployeeId: requestBody.salaryEmployeeId
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if(checkStep.length > 0){
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถเพิ่มได้ เนื่องจากลำดับขั้นนี้ซ้ำ");
|
|
|
|
|
}
|
2024-03-14 16:20:47 +07:00
|
|
|
const salaryRankEmployee = Object.assign(new SalaryRankEmployee(), requestBody);
|
2024-03-13 09:30:04 +07:00
|
|
|
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, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
|
|
|
|
|
}
|
2024-03-14 17:05:35 +07:00
|
|
|
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, "ไม่สามารถแก้ไขได้ เนื่องจากลำดับขั้นนี้ซ้ำ");
|
|
|
|
|
}
|
2024-03-13 09:30:04 +07:00
|
|
|
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,
|
|
|
|
|
},
|
2024-03-13 11:25:07 +07:00
|
|
|
select: ["id", "step", "salaryMonth", "salaryDay"],
|
2024-03-13 09:30:04 +07:00
|
|
|
order: {
|
|
|
|
|
step: "ASC",
|
|
|
|
|
},
|
|
|
|
|
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
|
|
|
|
});
|
|
|
|
|
if (keyword != undefined && keyword !== "") {
|
|
|
|
|
const filteredSalaryRankEmployee = salaryRankEmployee.filter(
|
|
|
|
|
(x) =>
|
|
|
|
|
x.step?.toString().includes(keyword) ||
|
2024-03-13 11:25:07 +07:00
|
|
|
x.salaryMonth?.toString().includes(keyword) ||
|
2024-03-13 09:30:04 +07:00
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|