267 lines
10 KiB
TypeScript
267 lines
10 KiB
TypeScript
|
|
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 HttpError from "../interfaces/http-error";
|
||
|
|
import { Equal, ILike, In, IsNull, Like, Not, Brackets, Between } from "typeorm";
|
||
|
|
import { EmployeePosDict, CreateEmployeePosDict, UpdateEmployeePosDict } from "../entities/EmployeePosDict";
|
||
|
|
import { EmployeePosType, CreateEmployeePosType, UpdateEmployeePosType } from "../entities/EmployeePosType";
|
||
|
|
import { EmployeePosLevel, CreateEmployeePosLevel, UpdateEmployeePosLevel } from "../entities/EmployeePosLevel";
|
||
|
|
import { KeyObject } from "crypto";
|
||
|
|
|
||
|
|
@Route("api/v1/org/employee/pos")
|
||
|
|
@Tags("Employee")
|
||
|
|
@Security("bearerAuth")
|
||
|
|
@Response(
|
||
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||
|
|
)
|
||
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||
|
|
export class EmployeePositionController extends Controller {
|
||
|
|
|
||
|
|
private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict);
|
||
|
|
private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType);
|
||
|
|
private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API เพิ่มตำแหน่งลูกจ้างประจำ
|
||
|
|
*
|
||
|
|
* @summary ORG_ - เพิ่มตำแหน่งลูกจ้างประจำ (ADMIN) #
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Post("position")
|
||
|
|
async CreateEmployeePosition(
|
||
|
|
@Body()
|
||
|
|
requestBody: CreateEmployeePosDict,
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
|
||
|
|
const empPosDict = Object.assign(new EmployeePosDict(), requestBody);
|
||
|
|
if (!empPosDict) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||
|
|
}
|
||
|
|
|
||
|
|
const EmpPosType = await this.employeePosTypeRepository.findOne({
|
||
|
|
where: { id: String(requestBody.employeePosTypeId) },
|
||
|
|
});
|
||
|
|
if (!EmpPosType) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
const EmpPosLevel = await this.employeePosLevelRepository.findOne({
|
||
|
|
where: { id: String(requestBody.employeePosLevelId) },
|
||
|
|
});
|
||
|
|
if (!EmpPosLevel) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
const rowRepeated = await this.employeePosDictRepository.findOne({
|
||
|
|
where: {
|
||
|
|
posDictName: String(requestBody.posDictName),
|
||
|
|
employeePosTypeId: String(requestBody.employeePosTypeId),
|
||
|
|
employeePosLevelId: String(requestBody.employeePosLevelId),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
if (rowRepeated) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
|
||
|
|
}
|
||
|
|
|
||
|
|
empPosDict.createdUserId = request.user.sub;
|
||
|
|
empPosDict.createdFullName = request.user.name;
|
||
|
|
empPosDict.lastUpdateUserId = request.user.sub;
|
||
|
|
empPosDict.lastUpdateFullName = request.user.name;
|
||
|
|
await this.employeePosDictRepository.save(empPosDict);
|
||
|
|
return new HttpSuccess(empPosDict.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API แก้ไขตำแหน่งลูกจ้างประจำ
|
||
|
|
*
|
||
|
|
* @summary แก้ไขตำแหน่งลูกจ้างประจำ (ADMIN)
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Put("position/{id}")
|
||
|
|
async updatePosition(
|
||
|
|
@Path() id: string,
|
||
|
|
@Body()
|
||
|
|
requestBody: UpdateEmployeePosDict,
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const empPosDict = await this.employeePosDictRepository.findOne({
|
||
|
|
where: { id: id },
|
||
|
|
});
|
||
|
|
if (!empPosDict) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
const checkEmpPosTypeId = await this.employeePosTypeRepository.findOne({
|
||
|
|
where: { id: requestBody.employeePosTypeId },
|
||
|
|
});
|
||
|
|
if (!checkEmpPosTypeId) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
const checkEmpPosLevelId = await this.employeePosLevelRepository.findOne({
|
||
|
|
where: { id: requestBody.employeePosLevelId },
|
||
|
|
});
|
||
|
|
if (!checkEmpPosLevelId) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับกลุ่มงานนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
const rowRepeated = await this.employeePosDictRepository.findOne({
|
||
|
|
where: {
|
||
|
|
id: Not(id),
|
||
|
|
posDictName: requestBody.posDictName,
|
||
|
|
employeePosTypeId: requestBody.employeePosTypeId,
|
||
|
|
employeePosLevelId: requestBody.employeePosLevelId,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
if (rowRepeated) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
|
||
|
|
}
|
||
|
|
|
||
|
|
empPosDict.lastUpdateUserId = request.user.sub;
|
||
|
|
empPosDict.lastUpdateFullName = request.user.name;
|
||
|
|
this.employeePosDictRepository.merge(empPosDict, requestBody);
|
||
|
|
await this.employeePosDictRepository.save(empPosDict);
|
||
|
|
return new HttpSuccess(empPosDict.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ลบตำแหน่งลูกจ้างประจำ
|
||
|
|
*
|
||
|
|
* @summary ORG_ - ลบตำแหน่งลูกจ้างประจำ (ADMIN) #
|
||
|
|
*
|
||
|
|
* @param {string} id Id ตำแหน่งลูกจ้างประจำ
|
||
|
|
*/
|
||
|
|
@Delete("position/{id}")
|
||
|
|
async delete(@Path() id: string) {
|
||
|
|
const delEmpPosDict = await this.employeePosDictRepository.findOne({ where: { id } });
|
||
|
|
if (!delEmpPosDict) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้");
|
||
|
|
}
|
||
|
|
await this.employeePosDictRepository.remove(delEmpPosDict);
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API รายละเอียดข้อมูลตำแหน่งลูกจ้างประจำ
|
||
|
|
*
|
||
|
|
* @summary ORG_037 - รายละเอียดข้อมูลตำแหน่งลูกจ้างประจำ (ADMIN) #
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Get("position/{id}")
|
||
|
|
async GetEmpPositionById(@Path() id: string) {
|
||
|
|
const empPosDict = await this.employeePosDictRepository.findOne({
|
||
|
|
select: ["id", "posDictName", "employeePosTypeId", "employeePosLevelId"],
|
||
|
|
where: { id: id },
|
||
|
|
});
|
||
|
|
if (!empPosDict) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
||
|
|
}
|
||
|
|
const empPosType = await this.employeePosTypeRepository.findOne({
|
||
|
|
select: ["id", "posTypeName", "posTypeRank", "posTypeShortName"],
|
||
|
|
where: { id: empPosDict.employeePosTypeId },
|
||
|
|
});
|
||
|
|
if (!empPosType) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้");
|
||
|
|
}
|
||
|
|
const empPosLevel = await this.employeePosLevelRepository.findOne({
|
||
|
|
select: ["id", "posLevelName", "posLevelRank"],
|
||
|
|
where: { id: empPosDict.employeePosLevelId },
|
||
|
|
});
|
||
|
|
if (!empPosLevel) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
const mapData = {
|
||
|
|
id: empPosDict.id,
|
||
|
|
posDictName: empPosDict.posDictName, //ชื่อตำแหน่ง
|
||
|
|
posTypeName: empPosType.posTypeName, //กลุ่มงาน
|
||
|
|
posLevelName: empPosLevel.posLevelName, //ระดับขั้นงาน
|
||
|
|
}
|
||
|
|
return new HttpSuccess(mapData);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ค้นหารายการตำแหน่งลูกจ้างประจำ
|
||
|
|
*
|
||
|
|
* @summary ORG_ - ค้นหารายการตำแหน่งลูกจ้างประจำ (ADMIN) #
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Get("position")
|
||
|
|
async GetEmpPosition(@Query("keyword") keyword?: string, @Query("type") type?: string) {
|
||
|
|
let findData: any;
|
||
|
|
switch (type) {
|
||
|
|
case "posDictName":
|
||
|
|
findData = await this.employeePosDictRepository.find({
|
||
|
|
where: { posDictName: Like(`%${keyword}%`) },
|
||
|
|
relations: ["employeePosType", "employeePosLevel"],
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
|
||
|
|
case "posTypeName":
|
||
|
|
const findEmpTypes: EmployeePosType[] = await this.employeePosTypeRepository.find({
|
||
|
|
where: { posTypeName: Like(`%${keyword}%`) },
|
||
|
|
select: ["id"],
|
||
|
|
});
|
||
|
|
findData = await this.employeePosDictRepository.find({
|
||
|
|
where: { employeePosTypeId: In(findEmpTypes.map((x) => x.id)) },
|
||
|
|
relations: ["employeePosType", "employeePosLevel"],
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
|
||
|
|
case "posLevelName":
|
||
|
|
if(!isNaN(Number(keyword))) {
|
||
|
|
const findEmpLevels: EmployeePosLevel[] = await this.employeePosLevelRepository.find({
|
||
|
|
where: { posLevelName: Number(keyword) },
|
||
|
|
});
|
||
|
|
findData = await this.employeePosDictRepository.find({
|
||
|
|
where: { employeePosLevelId: In(findEmpLevels.map((x) => x.id)) },
|
||
|
|
relations: ["employeePosType", "employeePosLevel"],
|
||
|
|
});
|
||
|
|
}
|
||
|
|
else { //กรณีเลือกค้นหาจาก"ระดับชั้นงาน" แต่กรอกไม่ใช่ number ให้ปล่อยมาหมดเลย
|
||
|
|
findData = await this.employeePosDictRepository.find({
|
||
|
|
relations: ["employeePosType", "employeePosLevel"],
|
||
|
|
});
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
findData = await this.employeePosDictRepository.find({
|
||
|
|
relations: ["employeePosType", "employeePosLevel"],
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
const mapDataEmpPosDict = await Promise.all(
|
||
|
|
findData.map(async (item: any) => {
|
||
|
|
return {
|
||
|
|
id: item.id,
|
||
|
|
posDictName: item.posDictName,
|
||
|
|
posTypeName: item.posType == null ? null : item.posType.posTypeName,
|
||
|
|
posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName,
|
||
|
|
};
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
return new HttpSuccess(mapDataEmpPosDict);
|
||
|
|
}
|
||
|
|
}
|