hrms-api-org/src/controllers/EmployeePosLevelController.ts
2024-10-18 11:33:04 +07:00

223 lines
8.8 KiB
TypeScript

import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Response,
} from "tsoa";
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 { Not } from "typeorm";
import { EmployeePosType } from "../entities/EmployeePosType";
import {
EmployeePosLevel,
CreateEmployeePosLevel,
UpdateEmployeePosLevel,
} from "../entities/EmployeePosLevel";
import { EmployeePosDict } from "../entities/EmployeePosDict";
@Route("api/v1/org/employee/pos/level")
@Tags("EmployeePosLevel")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
export class EmployeePosLevelController extends Controller {
private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType);
private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel);
/**
* API เพิ่มระดับชั้นงานลูกจ้างประจำ
*
* @summary ORG_ - เพิ่มระดับชั้นงานลูกจ้างประจำ (ADMIN) #
*
*/
@Post()
async CreateEmpLevel(
@Body()
requestBody: CreateEmployeePosLevel,
@Request() request: { user: Record<string, any> },
) {
const EmpPosLevel = Object.assign(new EmployeePosLevel(), requestBody);
if (!EmpPosLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const EmpPosType = await this.employeePosTypeRepository.findOne({
where: { id: requestBody.posTypeId },
});
if (!EmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้");
}
const chkEmpPosLevelName = await this.employeePosLevelRepository.findOne({
where: {
posLevelName: requestBody.posLevelName,
posTypeId: requestBody.posTypeId,
},
});
if (chkEmpPosLevelName) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ชื่อระดับชั้นงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว",
);
}
EmpPosLevel.createdUserId = request.user.sub;
EmpPosLevel.createdFullName = request.user.name;
EmpPosLevel.lastUpdateUserId = request.user.sub;
EmpPosLevel.lastUpdateFullName = request.user.name;
EmpPosLevel.createdAt = new Date();
EmpPosLevel.lastUpdatedAt = new Date();
await this.employeePosLevelRepository.save(EmpPosLevel);
return new HttpSuccess(EmpPosLevel.id);
}
/**
* API แก้ไขระดับชั้นงานลูกจ้างประจำ
*
* @summary ORG_ - แก้ไขระดับชั้นงานลูกจ้างประจำ (ADMIN) #
*
* @param {string} id Id ระดับชั้นงานลูกจ้างประจำ
*/
@Put("{id}")
async EditEmpLevel(
@Path() id: string,
@Body() requestBody: UpdateEmployeePosLevel,
@Request() request: { user: Record<string, any> },
) {
const EmpPosLevel = await this.employeePosLevelRepository.findOne({ where: { id } });
if (!EmpPosLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้");
}
const EmpPosType = await this.employeePosTypeRepository.findOne({
where: { id: requestBody.posTypeId },
});
if (!EmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้");
}
const chkEmpPosLevel = await this.employeePosLevelRepository.findOne({
where: {
id: Not(id),
posLevelName: requestBody.posLevelName,
posTypeId: requestBody.posTypeId,
},
});
if (chkEmpPosLevel) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ชื่อระดับชั้นงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว",
);
}
EmpPosLevel.lastUpdateUserId = request.user.sub;
EmpPosLevel.lastUpdateFullName = request.user.name;
EmpPosLevel.lastUpdatedAt = new Date();
this.employeePosLevelRepository.merge(EmpPosLevel, requestBody);
await this.employeePosLevelRepository.save(EmpPosLevel);
return new HttpSuccess(EmpPosLevel.id);
}
/**
* API ลบระดับชั้นงานลูกจ้างประจำ
*
* @summary ORG_ - ลบระดับชั้นงานลูกจ้างประจำ (ADMIN) #
*
* @param {string} id Id ระดับชั้นงานลูกจ้างประจำ
*/
@Delete("{id}")
async deleteType(@Path() id: string) {
let result: any;
try {
result = await this.employeePosLevelRepository.delete({ id: id });
} catch {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบได้เนื่องจากมีการใช้งานระดับชั้นงานนี้อยู่",
);
}
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
// const delEmpPosLevel = await this.employeePosLevelRepository.findOne({ where: { id } });
// if (!delEmpPosLevel) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้");
// }
// //ตารางตำแหน่งลูกจ้างประจำ
// const EmpPosition = await this.employeePosDictRepository.find({
// where: { posLevelId: id },
// });
// if (EmpPosition.length > 0) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางตำแหน่งลูกจ้างประจำ",
// );
// }
// await this.employeePosLevelRepository.remove(delEmpPosLevel);
return new HttpSuccess();
}
/**
* API รายละเอียดระดับชั้นงานลูกจ้างประจำ
*
* @summary ORG_ - รายละเอียดระดับชั้นงานลูกจ้างประจำ (ADMIN) #
*
* @param {string} id Id ระดับชั้นงานลูกจ้างประจำ
*/
@Get("{id}")
async GetEmpLevelById(@Path() id: string) {
const getEmpPosLevel = await this.employeePosLevelRepository.findOne({
relations: ["posType"],
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"],
where: { id: id },
});
if (!getEmpPosLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้");
}
const mapEmpPosLevel = {
id: getEmpPosLevel.id,
posLevelName: getEmpPosLevel.posLevelName,
posTypeId: getEmpPosLevel.posType == null ? null : getEmpPosLevel.posType.id,
posTypeName: getEmpPosLevel.posType == null ? null : getEmpPosLevel.posType.posTypeName, //กลุ่มงาน
posLevelAuthority: getEmpPosLevel.posLevelAuthority, //ผู้มีอำนาจสั่งบรรจุ
};
return new HttpSuccess(mapEmpPosLevel);
}
/**
* API รายการระดับชั้นงานลูกจ้างประจำ
*
* @summary ORG_ - รายการระดับชั้นงานลูกจ้างประจำ (ADMIN) #
*
*/
@Get()
async GetEmpPosLevel() {
const empPosLevel = await this.employeePosLevelRepository.find({
relations: ["posType"],
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"],
order: { posLevelRank: "ASC" },
});
const mapEmpPosLevel = empPosLevel.map((item) => ({
id: item.id,
posLevelName: item.posLevelName,
posTypeId: item.posType == null ? null : item.posType.id,
posTypeName: item.posType == null ? null : item.posType.posTypeName, //กลุ่มงาน
posLevelAuthority: item.posLevelAuthority, //ผู้มีอำนาจสั่งบรรจุ
}));
return new HttpSuccess(mapEmpPosLevel);
}
}