เกนเงินเดือนลูกจ้าง
This commit is contained in:
parent
8577745a54
commit
ff39403172
10 changed files with 570 additions and 26 deletions
245
src/controllers/SalaryFormulaEmployeeController.ts
Normal file
245
src/controllers/SalaryFormulaEmployeeController.ts
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Patch,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Body,
|
||||
Path,
|
||||
Request,
|
||||
Example,
|
||||
SuccessResponse,
|
||||
Response,
|
||||
Query,
|
||||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { PosLevel, CreatePosLevel, UpdatePosLevel } from "../entities/PosLevel";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { In, Not } from "typeorm";
|
||||
import {
|
||||
CreateSalaryFormulaEmployee,
|
||||
SalaryFormulaEmployee,
|
||||
UpdateSalaryFormulaEmployee,
|
||||
} from "../entities/SalaryFormulaEmployee";
|
||||
import { EmployeePosLevel } from "../entities/EmployeePosLevel";
|
||||
import { EmployeePosType } from "../entities/EmployeePosType";
|
||||
import { SalaryEmployee } from "../entities/SalaryEmployee";
|
||||
|
||||
@Route("api/v1/salary/formula")
|
||||
@Tags("SalaryFormula")
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class SalaryFormulaEmployeeController extends Controller {
|
||||
private salaryFormulaEmployeeRepository = AppDataSource.getRepository(SalaryFormulaEmployee);
|
||||
private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel);
|
||||
private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType);
|
||||
private salaryEmployeeRepository = AppDataSource.getRepository(SalaryEmployee);
|
||||
|
||||
/**
|
||||
* API เพิ่มหลักเกณฑ์
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
async createFormula(
|
||||
@Body()
|
||||
requestBody: CreateSalaryFormulaEmployee,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const formula = Object.assign(new SalaryFormulaEmployee(), requestBody);
|
||||
|
||||
const chkPosLevel = await this.employeePosLevelRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.posLevelId,
|
||||
},
|
||||
});
|
||||
if (!chkPosLevel) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
|
||||
|
||||
const chkPosType = await this.employeePosTypeRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.posTypeId,
|
||||
},
|
||||
});
|
||||
if (!chkPosType) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานตำแหน่งนี้");
|
||||
|
||||
const chkSalaryEmployee = await this.salaryEmployeeRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.salaryEmployeeId,
|
||||
},
|
||||
});
|
||||
if (!chkSalaryEmployee)
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้");
|
||||
|
||||
const chkFormula = await this.salaryFormulaEmployeeRepository.findOne({
|
||||
where: {
|
||||
position: requestBody.position,
|
||||
posTypeId: requestBody.posTypeId,
|
||||
posLevelId: requestBody.posLevelId,
|
||||
},
|
||||
});
|
||||
if (chkFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "หลักเกณฑ์นี้มีอยู่ในระบบแล้ว");
|
||||
|
||||
const chkSalaryEmployeeMin = await this.salaryEmployeeRepository.find({
|
||||
where: {
|
||||
id: In(requestBody.salaryEmployeeMinIds),
|
||||
},
|
||||
});
|
||||
|
||||
formula.salaryEmployeeMins = chkSalaryEmployeeMin;
|
||||
formula.createdUserId = request.user.sub;
|
||||
formula.createdFullName = request.user.name;
|
||||
formula.lastUpdateUserId = request.user.sub;
|
||||
formula.lastUpdateFullName = request.user.name;
|
||||
await this.salaryFormulaEmployeeRepository.save(formula);
|
||||
return new HttpSuccess(formula);
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขหลักเกณฑ์
|
||||
*
|
||||
*
|
||||
* @param {string} id Id หลักเกณฑ์
|
||||
*/
|
||||
@Put("{id}")
|
||||
async editFormula(
|
||||
@Path() id: string,
|
||||
@Body() requestBody: UpdateSalaryFormulaEmployee,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const formula = await this.salaryFormulaEmployeeRepository.findOne({ where: { id } });
|
||||
if (!formula) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลหลักเกณฑ์นี้");
|
||||
|
||||
const chkFormula = await this.salaryFormulaEmployeeRepository.findOne({
|
||||
where: {
|
||||
id: Not(id),
|
||||
position: requestBody.position,
|
||||
posTypeId: requestBody.posTypeId,
|
||||
posLevelId: requestBody.posLevelId,
|
||||
},
|
||||
});
|
||||
if (chkFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "หลักเกณฑ์นี้มีอยู่ในระบบแล้ว");
|
||||
|
||||
const chkPosLevel = await this.employeePosLevelRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.posLevelId,
|
||||
},
|
||||
});
|
||||
if (!chkPosLevel) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
|
||||
|
||||
const chkPosType = await this.employeePosTypeRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.posTypeId,
|
||||
},
|
||||
});
|
||||
if (!chkPosType) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานตำแหน่งนี้");
|
||||
|
||||
const chkSalaryEmployee = await this.salaryEmployeeRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.salaryEmployeeId,
|
||||
},
|
||||
});
|
||||
if (!chkSalaryEmployee)
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้");
|
||||
|
||||
const chkSalaryEmployeeMin = await this.salaryEmployeeRepository.find({
|
||||
where: {
|
||||
id: In(requestBody.salaryEmployeeMinIds),
|
||||
},
|
||||
});
|
||||
formula.position = requestBody.position;
|
||||
formula.salaryMin = requestBody.salaryMin;
|
||||
formula.salary = requestBody.salary;
|
||||
formula.salaryMax = requestBody.salaryMax;
|
||||
formula.details = requestBody.details;
|
||||
formula.salaryEmployeeId = requestBody.salaryEmployeeId;
|
||||
formula.posTypeId = requestBody.posTypeId;
|
||||
formula.posLevelId = requestBody.posLevelId;
|
||||
formula.salaryEmployeeMins = chkSalaryEmployeeMin;
|
||||
formula.lastUpdateUserId = request.user.sub;
|
||||
formula.lastUpdateFullName = request.user.name;
|
||||
await this.salaryFormulaEmployeeRepository.save(formula);
|
||||
return new HttpSuccess(formula.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบหลักเกณฑ์
|
||||
*
|
||||
*
|
||||
* @param {string} id Id หลักเกณฑ์
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async deleteFormula(@Path() id: string) {
|
||||
const delFormula = await this.salaryFormulaEmployeeRepository.findOne({ where: { id } });
|
||||
if (!delFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้");
|
||||
|
||||
await this.salaryFormulaEmployeeRepository.remove(delFormula);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายละเอียดหลักเกณฑ์
|
||||
*
|
||||
*
|
||||
* @param {string} id Id หลักเกณฑ์
|
||||
*/
|
||||
@Get("{id}")
|
||||
async getFormulaDetail(@Path() id: string) {
|
||||
const getFormula = await this.salaryFormulaEmployeeRepository.findOne({
|
||||
relations: ["salaryEmployee", "posType", "posLevel"],
|
||||
where: { id: id },
|
||||
});
|
||||
if (!getFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้");
|
||||
|
||||
const mapFormula = {
|
||||
id: getFormula.id,
|
||||
posLevelId: getFormula.posLevelId,
|
||||
position: getFormula.position,
|
||||
posTypeId: getFormula.posTypeId,
|
||||
details: getFormula.details,
|
||||
salaryMin: getFormula.salaryMin,
|
||||
salary: getFormula.salary,
|
||||
salaryMax: getFormula.salaryMax,
|
||||
salaryEmployeeId: getFormula.salaryEmployeeId,
|
||||
};
|
||||
|
||||
return new HttpSuccess(mapFormula);
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายการหลักเกณฑ์
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Get()
|
||||
async getFormula() {
|
||||
const getFormula = await this.salaryFormulaEmployeeRepository.find({
|
||||
relations: ["salaryEmployee", "posType", "posLevel"],
|
||||
});
|
||||
|
||||
const mapFormula = getFormula.map((item) => ({
|
||||
id: item.id,
|
||||
// posLevelId: item.posLevelId,
|
||||
posLevel: item.posLevel != null ? item.posLevel.posLevelName : null,
|
||||
position: item.position,
|
||||
// posTypeId: item.posTypeId,
|
||||
posType: item.posType != null ? item.posType.posTypeName : null,
|
||||
details: item.details,
|
||||
salaryMin: item.salaryMin,
|
||||
salary: item.salary,
|
||||
salaryMax: item.salaryMax,
|
||||
// salaryEmployeeId: item.salaryEmployeeId,
|
||||
group: item.salaryEmployee != null ? item.salaryEmployee.group : null,
|
||||
}));
|
||||
return new HttpSuccess(mapFormula);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue