diff --git a/src/controllers/EmployeePosLevelController.ts b/src/controllers/EmployeePosLevelController.ts index 9351c0c8..782cf561 100644 --- a/src/controllers/EmployeePosLevelController.ts +++ b/src/controllers/EmployeePosLevelController.ts @@ -18,8 +18,12 @@ 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 { EmployeePosType } from "../entities/EmployeePosType"; +import { + EmployeePosLevel, + CreateEmployeePosLevel, + UpdateEmployeePosLevel, +} from "../entities/EmployeePosLevel"; import { EmployeePosDict } from "../entities/EmployeePosDict"; @Route("api/v1/org/employee/pos/level") @@ -31,7 +35,6 @@ import { EmployeePosDict } from "../entities/EmployeePosDict"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class EmployeePosLevelController extends Controller { - private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict); private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); @@ -52,9 +55,9 @@ export class EmployeePosLevelController extends Controller { if (!EmpPosLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - - const EmpPosType = await this.employeePosTypeRepository.findOne({ - where: { id: requestBody.employeePosTypeId } + + const EmpPosType = await this.employeePosTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, }); if (!EmpPosType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้"); @@ -98,8 +101,8 @@ export class EmployeePosLevelController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้"); } - const EmpPosType = await this.employeePosTypeRepository.findOne({ - where: { id: requestBody.employeePosTypeId } + const EmpPosType = await this.employeePosTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, }); if (!EmpPosType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้"); @@ -137,10 +140,10 @@ export class EmployeePosLevelController extends Controller { if (!delEmpPosLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้"); } - + //ตารางตำแหน่งลูกจ้างประจำ const EmpPosition = await this.employeePosDictRepository.find({ - where: { employeePosLevelId: id }, + where: { posLevelId: id }, }); if (EmpPosition.length > 0) { throw new HttpError( @@ -163,8 +166,8 @@ export class EmployeePosLevelController extends Controller { @Get("{id}") async GetEmpLevelById(@Path() id: string) { const getEmpPosLevel = await this.employeePosLevelRepository.findOne({ - relations: ["employeePosType"], - select: ["id", "posLevelName", "posLevelRank",], + relations: ["posType"], + select: ["id", "posLevelName", "posLevelRank"], where: { id: id }, }); if (!getEmpPosLevel) { @@ -173,9 +176,9 @@ export class EmployeePosLevelController extends Controller { const mapEmpPosLevel = { id: getEmpPosLevel.id, posLevelName: getEmpPosLevel.posLevelName, - posTypeName: getEmpPosLevel.employeePosType == null ? null : getEmpPosLevel.employeePosType.posTypeName, //กลุ่มงาน - commander: null //ผู้มีอำนาจสั่งบรรจุ - } + posTypeName: getEmpPosLevel.posType == null ? null : getEmpPosLevel.posType.posTypeName, //กลุ่มงาน + commander: null, //ผู้มีอำนาจสั่งบรรจุ + }; return new HttpSuccess(mapEmpPosLevel); } @@ -188,15 +191,15 @@ export class EmployeePosLevelController extends Controller { @Get() async GetEmpPosLevel() { const empPosLevel = await this.employeePosLevelRepository.find({ - relations: ["employeePosType"], - select: ["id","posLevelName", "posLevelRank",], + relations: ["posType"], + select: ["id", "posLevelName", "posLevelRank"], }); const mapEmpPosLevel = empPosLevel.map((item) => ({ id: item.id, posLevelName: item.posLevelName, - posTypeName: item.employeePosType == null ? null : item.employeePosType.posTypeName, //กลุ่มงาน - commander: null //ผู้มีอำนาจสั่งบรรจุ + posTypeName: item.posType == null ? null : item.posType.posTypeName, //กลุ่มงาน + commander: null, //ผู้มีอำนาจสั่งบรรจุ })); return new HttpSuccess(mapEmpPosLevel); } -} \ No newline at end of file +} diff --git a/src/controllers/EmployeePosTypeController.ts b/src/controllers/EmployeePosTypeController.ts index 4545594a..d20e2206 100644 --- a/src/controllers/EmployeePosTypeController.ts +++ b/src/controllers/EmployeePosTypeController.ts @@ -18,7 +18,11 @@ import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Not } from "typeorm"; -import { EmployeePosType, CreateEmployeePosType, UpdateEmployeePosType } from "../entities/EmployeePosType"; +import { + EmployeePosType, + CreateEmployeePosType, + UpdateEmployeePosType, +} from "../entities/EmployeePosType"; import { EmployeePosLevel } from "../entities/EmployeePosLevel"; import { EmployeePosDict } from "../entities/EmployeePosDict"; @@ -31,7 +35,6 @@ import { EmployeePosDict } from "../entities/EmployeePosDict"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class EmployeePosTypeController extends Controller { - private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict); private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); @@ -59,10 +62,7 @@ export class EmployeePosTypeController extends Controller { }, }); if (chkEmpPosTypeName) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ชื่อกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว", - ); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว"); } EmpPosType.createdUserId = request.user.sub; EmpPosType.createdFullName = request.user.name; @@ -96,10 +96,7 @@ export class EmployeePosTypeController extends Controller { }, }); if (chkEmpPosType) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ชื่อกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว", - ); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว"); } EmpPosType.lastUpdateUserId = request.user.sub; EmpPosType.lastUpdateFullName = request.user.name; @@ -123,7 +120,7 @@ export class EmployeePosTypeController extends Controller { } //ตารางระดับชั้นงาน const EmpPosLevel = await this.employeePosLevelRepository.find({ - where: { employeePosTypeId: id }, + where: { posTypeId: id }, }); if (EmpPosLevel.length > 0) { throw new HttpError( @@ -133,7 +130,7 @@ export class EmployeePosTypeController extends Controller { } //ตารางตำแหน่งลูกจ้างประจำ const EmpPosition = await this.employeePosDictRepository.find({ - where: { employeePosTypeId: id }, + where: { posTypeId: id }, }); if (EmpPosition.length > 0) { throw new HttpError( @@ -156,8 +153,8 @@ export class EmployeePosTypeController extends Controller { @Get("{id}") async GetEmpTypeById(@Path() id: string) { const getEmpPosType = await this.employeePosTypeRepository.findOne({ + relations: ["posLevels"], select: ["id", "posTypeName", "posTypeRank"], - relations: ["employeePosLevels"], where: { id: id }, }); if (!getEmpPosType) { @@ -168,7 +165,7 @@ export class EmployeePosTypeController extends Controller { id: getEmpPosType.id, posTypeName: getEmpPosType.posTypeName, posTypeRank: getEmpPosType.posTypeRank, - posLevels: getEmpPosType.employeePosLevels.map((empPosLevel) => ({ + posLevels: getEmpPosType.posLevels.map((empPosLevel) => ({ id: empPosLevel.id, posLevelName: empPosLevel.posLevelName, posLevelRank: empPosLevel.posLevelRank, @@ -187,15 +184,15 @@ export class EmployeePosTypeController extends Controller { @Get() async GetEmpPosType() { const empPosType = await this.employeePosTypeRepository.find({ + relations: ["posLevels"], select: ["id", "posTypeName", "posTypeRank"], - relations: ["employeePosLevels"], }); const mapEmpPosType = empPosType.map((item) => ({ id: item.id, posTypeName: item.posTypeName, posTypeRank: item.posTypeRank, - posLevels: item.employeePosLevels.map((empPosLevel) => ({ + posLevels: item.posLevels.map((empPosLevel) => ({ id: empPosLevel.id, posLevelName: empPosLevel.posLevelName, posLevelRank: empPosLevel.posLevelRank, @@ -203,4 +200,4 @@ export class EmployeePosTypeController extends Controller { })); return new HttpSuccess(mapEmpPosType); } -} \ No newline at end of file +} diff --git a/src/controllers/EmployeePositionController.ts b/src/controllers/EmployeePositionController.ts index 8077b2eb..3279fffe 100644 --- a/src/controllers/EmployeePositionController.ts +++ b/src/controllers/EmployeePositionController.ts @@ -4,14 +4,12 @@ import { Post, Put, Delete, - Patch, Route, Security, Tags, Body, Path, Request, - Example, SuccessResponse, Response, Query, @@ -20,11 +18,23 @@ 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"; +import { In, IsNull, Like, Not, Brackets } from "typeorm"; +import { + EmployeePosDict, + CreateEmployeePosDict, + UpdateEmployeePosDict, +} from "../entities/EmployeePosDict"; +import { EmployeePosType } from "../entities/EmployeePosType"; +import { EmployeePosLevel } from "../entities/EmployeePosLevel"; +import { CreateEmployeePosMaster, EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { EmployeePosition } from "../entities/EmployeePosition"; +import { Profile } from "../entities/Profile"; +import { OrgRevision } from "../entities/OrgRevision"; +import { OrgRoot } from "../entities/OrgRoot"; +import { OrgChild1 } from "../entities/OrgChild1"; +import { OrgChild2 } from "../entities/OrgChild2"; +import { OrgChild3 } from "../entities/OrgChild3"; +import { OrgChild4 } from "../entities/OrgChild4"; @Route("api/v1/org/employee/pos") @Tags("Employee") @@ -35,11 +45,19 @@ import { KeyObject } from "crypto"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class EmployeePositionController extends Controller { - private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict); private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); - + private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); + private employeePositionRepository = AppDataSource.getRepository(EmployeePosition); + private profileRepository = AppDataSource.getRepository(Profile); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + private orgRootRepository = AppDataSource.getRepository(OrgRoot); + private child1Repository = AppDataSource.getRepository(OrgChild1); + private child2Repository = AppDataSource.getRepository(OrgChild2); + private child3Repository = AppDataSource.getRepository(OrgChild3); + private child4Repository = AppDataSource.getRepository(OrgChild4); + /** * API เพิ่มตำแหน่งลูกจ้างประจำ * @@ -52,21 +70,20 @@ export class EmployeePositionController extends Controller { requestBody: CreateEmployeePosDict, @Request() request: { user: Record }, ) { - 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) }, + where: { id: String(requestBody.posTypeId) }, }); if (!EmpPosType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } const EmpPosLevel = await this.employeePosLevelRepository.findOne({ - where: { id: String(requestBody.employeePosLevelId) }, + where: { id: String(requestBody.posLevelId) }, }); if (!EmpPosLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้"); @@ -75,8 +92,8 @@ export class EmployeePositionController extends Controller { const rowRepeated = await this.employeePosDictRepository.findOne({ where: { posDictName: String(requestBody.posDictName), - employeePosTypeId: String(requestBody.employeePosTypeId), - employeePosLevelId: String(requestBody.employeePosLevelId), + posTypeId: String(requestBody.posTypeId), + posLevelId: String(requestBody.posLevelId), }, }); if (rowRepeated) { @@ -112,14 +129,14 @@ export class EmployeePositionController extends Controller { } const checkEmpPosTypeId = await this.employeePosTypeRepository.findOne({ - where: { id: requestBody.employeePosTypeId }, + where: { id: requestBody.posTypeId }, }); if (!checkEmpPosTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } const checkEmpPosLevelId = await this.employeePosLevelRepository.findOne({ - where: { id: requestBody.employeePosLevelId }, + where: { id: requestBody.posLevelId }, }); if (!checkEmpPosLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับกลุ่มงานนี้"); @@ -129,8 +146,8 @@ export class EmployeePositionController extends Controller { where: { id: Not(id), posDictName: requestBody.posDictName, - employeePosTypeId: requestBody.employeePosTypeId, - employeePosLevelId: requestBody.employeePosLevelId, + posTypeId: requestBody.posTypeId, + posLevelId: requestBody.posLevelId, }, }); if (rowRepeated) { @@ -170,7 +187,7 @@ export class EmployeePositionController extends Controller { @Get("position/{id}") async GetEmpPositionById(@Path() id: string) { const empPosDict = await this.employeePosDictRepository.findOne({ - select: ["id", "posDictName", "employeePosTypeId", "employeePosLevelId"], + select: ["id", "posDictName", "posTypeId", "posLevelId"], where: { id: id }, }); if (!empPosDict) { @@ -178,25 +195,25 @@ export class EmployeePositionController extends Controller { } const empPosType = await this.employeePosTypeRepository.findOne({ select: ["id", "posTypeName", "posTypeRank", "posTypeShortName"], - where: { id: empPosDict.employeePosTypeId }, + where: { id: empPosDict.posTypeId }, }); if (!empPosType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้"); } const empPosLevel = await this.employeePosLevelRepository.findOne({ select: ["id", "posLevelName", "posLevelRank"], - where: { id: empPosDict.employeePosLevelId }, + where: { id: empPosDict.posLevelId }, }); 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); } @@ -213,7 +230,7 @@ export class EmployeePositionController extends Controller { case "posDictName": findData = await this.employeePosDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) }, - relations: ["employeePosType", "employeePosLevel"], + relations: ["posType", "posLevel"], }); break; @@ -223,31 +240,31 @@ export class EmployeePositionController extends Controller { select: ["id"], }); findData = await this.employeePosDictRepository.find({ - where: { employeePosTypeId: In(findEmpTypes.map((x) => x.id)) }, - relations: ["employeePosType", "employeePosLevel"], + where: { posTypeId: In(findEmpTypes.map((x) => x.id)) }, + relations: ["posType", "posLevel"], }); break; case "posLevelName": - if(!isNaN(Number(keyword))) { + 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"], + where: { posLevelId: In(findEmpLevels.map((x) => x.id)) }, + relations: ["posType", "posLevel"], }); - } - else { //กรณีเลือกค้นหาจาก"ระดับชั้นงาน" แต่กรอกไม่ใช่ number ให้ปล่อยมาหมดเลย + } else { + //กรณีเลือกค้นหาจาก"ระดับชั้นงาน" แต่กรอกไม่ใช่ number ให้ปล่อยมาหมดเลย findData = await this.employeePosDictRepository.find({ - relations: ["employeePosType", "employeePosLevel"], + relations: ["posType", "posLevel"], }); } break; default: findData = await this.employeePosDictRepository.find({ - relations: ["employeePosType", "employeePosLevel"], + relations: ["posType", "posLevel"], }); break; } @@ -255,12 +272,12 @@ export class EmployeePositionController extends Controller { findData.map(async (item: any) => { let posTypeName = null; let posLevelName = null; - - if (item.employeePosType !== null && item.employeePosType !== undefined) { - posTypeName = item.employeePosType.posTypeName; + + if (item.posType !== null && item.posType !== undefined) { + posTypeName = item.posType.posTypeName; } - if (item.employeePosLevel !== null && item.employeePosLevel !== undefined) { - posLevelName = item.employeePosLevel.posLevelName; + if (item.posLevel !== null && item.posLevel !== undefined) { + posLevelName = item.posLevel.posLevelName; } return { @@ -273,4 +290,1747 @@ export class EmployeePositionController extends Controller { ); return new HttpSuccess(mapDataEmpPosDict); } + + /** + * API เพิ่มอัตรากำลัง + * + * @summary ORG_ - เพิ่มอัตรากำลัง (ADMIN) + * + */ + @Post("master") + async createEmpMaster( + @Body() + requestBody: CreateEmployeePosMaster, + @Request() request: { user: Record }, + ) { + const posMaster = Object.assign(new EmployeePosMaster(), requestBody); + if (!posMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + let orgRoot: any = null; + let SName: any = null; + if (requestBody.orgRootId != null) + orgRoot = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, + }); + if (!orgRoot) { + let orgChild1: any = null; + if (requestBody.orgChild1Id != null) + orgChild1 = await this.child1Repository.findOne({ + where: { id: requestBody.orgChild1Id }, + }); + if (!orgChild1) { + let orgChild2: any = null; + if (requestBody.orgChild2Id != null) + orgChild2 = await this.child2Repository.findOne({ + where: { id: requestBody.orgChild2Id }, + }); + if (!orgChild2) { + let orgChild3: any = null; + if (requestBody.orgChild3Id != null) + orgChild3 = await this.child3Repository.findOne({ + where: { id: requestBody.orgChild3Id }, + }); + if (!orgChild3) { + let orgChild4: any = null; + if (requestBody.orgChild4Id != null) + orgChild4 = await this.child4Repository.findOne({ + where: { id: requestBody.orgChild4Id }, + }); + if (!orgChild4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); + } else { + const order: any = await this.employeePosMasterRepository.findOne({ + where: { + orgChild4Id: orgChild4.id, + }, + order: { posMasterOrder: "DESC" }, + }); + posMaster.posMasterOrder = + order !== null && order !== undefined && order.posMasterOrder + ? order.posMasterOrder + 1 + : 1; + posMaster.orgRootId = orgChild4.orgRootId; + posMaster.orgChild1Id = orgChild4.orgChild1Id; + posMaster.orgChild2Id = orgChild4.orgChild2Id; + posMaster.orgChild3Id = orgChild4.orgChild3Id; + posMaster.orgChild4Id = orgChild4.id; + posMaster.orgRevisionId = orgChild4.orgRevisionId; + SName = orgChild4.orgChild4ShortName; + } + } else { + const order: any = await this.employeePosMasterRepository.findOne({ + where: { + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + }, + order: { posMasterOrder: "DESC" }, + }); + posMaster.posMasterOrder = + order !== null && order !== undefined && order.posMasterOrder + ? order.posMasterOrder + 1 + : 1; + posMaster.orgRootId = orgChild3.orgRootId; + posMaster.orgChild1Id = orgChild3.orgChild1Id; + posMaster.orgChild2Id = orgChild3.orgChild2Id; + posMaster.orgChild3Id = orgChild3.id; + posMaster.orgRevisionId = orgChild3.orgRevisionId; + SName = orgChild3.orgChild3ShortName; + } + } else { + const order: any = await this.employeePosMasterRepository.findOne({ + where: { + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + order: { posMasterOrder: "DESC" }, + }); + posMaster.posMasterOrder = + order !== null && order !== undefined && order.posMasterOrder + ? order.posMasterOrder + 1 + : 1; + posMaster.orgRootId = orgChild2.orgRootId; + posMaster.orgChild1Id = orgChild2.orgChild1Id; + posMaster.orgChild2Id = orgChild2.id; + posMaster.orgRevisionId = orgChild2.orgRevisionId; + SName = orgChild2.orgChild2ShortName; + } + } else { + const order: any = await this.employeePosMasterRepository.findOne({ + where: { + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + order: { posMasterOrder: "DESC" }, + }); + posMaster.posMasterOrder = + order !== null && order !== undefined && order.posMasterOrder + ? order.posMasterOrder + 1 + : 1; + posMaster.orgRootId = orgChild1.orgRootId; + posMaster.orgChild1Id = orgChild1.id; + posMaster.orgRevisionId = orgChild1.orgRevisionId; + SName = orgChild1.orgChild1ShortName; + } + } else { + const order: any = await this.employeePosMasterRepository.findOne({ + where: { + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + order: { posMasterOrder: "DESC" }, + }); + posMaster.posMasterOrder = + order !== null && order !== undefined && order.posMasterOrder + ? order.posMasterOrder + 1 + : 1; + posMaster.orgRootId = orgRoot.id; + posMaster.orgRevisionId = orgRoot.orgRevisionId; + SName = orgRoot.orgRootShortName; + } + + const chk_SName0 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgRoot: { orgRootShortName: SName }, + orgChild1Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgRoot"], + }); + if (chk_SName0 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName1 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild1: { orgChild1ShortName: SName }, + orgChild2Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild1"], + }); + if (chk_SName1 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName2 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild2: { orgChild2ShortName: SName }, + orgChild3Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild2"], + }); + if (chk_SName2 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName3 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild3: { orgChild3ShortName: SName }, + orgChild4Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild3"], + }); + if (chk_SName3 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName4 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild4: { orgChild4ShortName: SName }, + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild4"], + }); + if (chk_SName4 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + await this.employeePosMasterRepository.save(posMaster); + await Promise.all( + requestBody.positions.map(async (x: any) => { + const position = Object.assign(new EmployeePosition()); + position.positionName = x.posDictName; + position.posTypeId = x.posTypeId == "" ? null : x.posTypeId; + position.posLevelId = x.posLevelId == "" ? null : x.posLevelId; + position.positionIsSelected = false; + position.posMasterId = posMaster.id; + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + await this.employeePositionRepository.save(position); + }), + ); + return new HttpSuccess(posMaster.id); + } + + /** + * API แก้ไขเลขที่ตำแหน่ง + * + * @summary ORG_ - แก้ไขเลขที่ตำแหน่ง (ADMIN) + * + */ + @Put("master/{id}") + async updateEmpMaster( + @Path() id: string, + @Body() + requestBody: CreateEmployeePosMaster, + @Request() request: { user: Record }, + ) { + const posMaster = await this.employeePosMasterRepository.findOne({ where: { id: id } }); + if (!posMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลอัตรากำลัง"); + } + posMaster.posMasterNo = requestBody.posMasterNo; + posMaster.posMasterNoPrefix = requestBody.posMasterNoPrefix; + posMaster.posMasterNoSuffix = requestBody.posMasterNoSuffix; + posMaster.orgRootId = null; + posMaster.orgChild1Id = null; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + + let orgRoot: any = null; + let SName: any = null; + if (requestBody.orgRootId != null) + orgRoot = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, + }); + if (!orgRoot) { + let orgChild1: any = null; + if (requestBody.orgChild1Id != null) + orgChild1 = await this.child1Repository.findOne({ + where: { id: requestBody.orgChild1Id }, + }); + if (!orgChild1) { + let orgChild2: any = null; + if (requestBody.orgChild2Id != null) + orgChild2 = await this.child2Repository.findOne({ + where: { id: requestBody.orgChild2Id }, + }); + if (!orgChild2) { + let orgChild3: any = null; + if (requestBody.orgChild3Id != null) + orgChild3 = await this.child3Repository.findOne({ + where: { id: requestBody.orgChild3Id }, + }); + if (!orgChild3) { + let orgChild4: any = null; + if (requestBody.orgChild4Id != null) + orgChild4 = await this.child4Repository.findOne({ + where: { id: requestBody.orgChild4Id }, + }); + if (!orgChild4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); + } else { + posMaster.orgRootId = orgChild4.orgRootId; + posMaster.orgChild1Id = orgChild4.orgChild1Id; + posMaster.orgChild2Id = orgChild4.orgChild2Id; + posMaster.orgChild3Id = orgChild4.orgChild3Id; + posMaster.orgChild4Id = orgChild4.id; + posMaster.orgRevisionId = orgChild4.orgRevisionId; + SName = orgChild4.orgChild4ShortName; + } + } else { + posMaster.orgRootId = orgChild3.orgRootId; + posMaster.orgChild1Id = orgChild3.orgChild1Id; + posMaster.orgChild2Id = orgChild3.orgChild2Id; + posMaster.orgChild3Id = orgChild3.id; + posMaster.orgRevisionId = orgChild3.orgRevisionId; + SName = orgChild3.orgChild3ShortName; + } + } else { + posMaster.orgRootId = orgChild2.orgRootId; + posMaster.orgChild1Id = orgChild2.orgChild1Id; + posMaster.orgChild2Id = orgChild2.id; + posMaster.orgRevisionId = orgChild2.orgRevisionId; + SName = orgChild2.orgChild2ShortName; + } + } else { + posMaster.orgRootId = orgChild1.orgRootId; + posMaster.orgChild1Id = orgChild1.id; + posMaster.orgRevisionId = orgChild1.orgRevisionId; + SName = orgChild1.orgChild1ShortName; + } + } else { + posMaster.orgRootId = orgRoot.id; + posMaster.orgRevisionId = orgRoot.orgRevisionId; + SName = orgRoot.orgRootShortName; + } + + const chk_SName0 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgRoot: { orgRootShortName: SName }, + orgChild1Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + id: Not(posMaster.id), + }, + relations: ["orgRoot"], + }); + if (chk_SName0 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName1 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild1: { orgChild1ShortName: SName }, + orgChild2Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + id: Not(posMaster.id), + }, + relations: ["orgChild1"], + }); + if (chk_SName1 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName2 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild2: { orgChild2ShortName: SName }, + orgChild3Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + id: Not(posMaster.id), + }, + relations: ["orgChild2"], + }); + if (chk_SName2 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName3 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild3: { orgChild3ShortName: SName }, + orgChild4Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + id: Not(posMaster.id), + }, + relations: ["orgChild3"], + }); + if (chk_SName3 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + const chk_SName4 = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild4: { orgChild4ShortName: SName }, + posMasterNo: requestBody.posMasterNo, + id: Not(posMaster.id), + }, + relations: ["orgChild4"], + }); + if (chk_SName4 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + + posMaster.createdUserId = request.user.sub; //สงสัยว่าทำให้ bug แก้ไขไม่ได้ + posMaster.createdFullName = request.user.name; + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + await this.employeePosMasterRepository.save(posMaster); + await this.employeePositionRepository.delete({ posMasterId: posMaster.id }); + + await Promise.all( + requestBody.positions.map(async (x: any) => { + const position = Object.assign(new EmployeePosition()); + position.positionName = x.posDictName; + position.posTypeId = x.posTypeId == "" ? null : x.posTypeId; + position.posLevelId = x.posLevelId == "" ? null : x.posLevelId; + position.positionIsSelected = false; + position.posMasterId = posMaster.id; + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + await this.employeePositionRepository.save(position); + }), + ); + return new HttpSuccess(posMaster.id); + } + + /** + * API รายละเอียดอัตรากำลัง + * + * @summary ORG_ - รายละเอียดอัตรากำลัง (ADMIN) + * + */ + // @Get("position/{id}") + // async detailEmpPosition(@Path() id: string) { + // const posMaster = await this.employeePosMasterRepository.findOne({ + // where: { id }, + // }); + // if (!posMaster) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // } + // const positions = await this.employeePositionRepository.find({ + // where: { posMasterId: posMaster.id }, + // relations: ["posType", "posLevel"], + // order: { lastUpdatedAt: "ASC" }, + // }); + // const formattedData = { + // id: posMaster.id, + // posMasterNoPrefix: posMaster.posMasterNoPrefix, + // posMasterNo: posMaster.posMasterNo, + // posMasterNoSuffix: posMaster.posMasterNoSuffix, + // positions: positions.map((position) => ({ + // id: position.id, + // positionName: position.positionName, + // posTypeId: position.posTypeId, + // posTypeName: position.posType == null ? null : position.posType.posTypeName, + // posLevelId: position.posLevelId, + // posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, + // positionIsSelected: position.positionIsSelected, + // })), + // }; + // return new HttpSuccess(formattedData); + // } + + /** + * API ลบอัตรากำลัง + * + * @summary ORG_ - ลบอัตรากำลัง (ADMIN) + * + * @param {string} id Id ตำแหน่ง + */ + @Delete("master/{id}") + async deleteEmpPosMaster(@Path() id: string) { + const delPosMaster = await this.employeePosMasterRepository.findOne({ + where: { id }, + }); + if (!delPosMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในสายงานนี้"); + } + await this.employeePositionRepository.delete({ posMasterId: id }); + await this.employeePosMasterRepository.delete({ id }); + return new HttpSuccess(); + } + + /** + * API รายการอัตรากำลัง + * + * @summary ORG_ - รายการอัตรากำลัง (ADMIN) + * + */ + @Post("master/list") + async listEmp( + @Body() + body: { + id: string; + revisionId: string; + type: number; + isAll: boolean; + page: number; + pageSize: number; + keyword?: string; + }, + ) { + let typeCondition: any = {}; + let checkChildConditions: any = {}; + let keywordAsInt: any; + let searchShortName = ""; + let searchShortName0 = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName1 = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName2 = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName3 = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + let searchShortName4 = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + + if (body.type === 0) { + typeCondition = { + orgRootId: body.id, + }; + if (!body.isAll) { + checkChildConditions = { + orgChild1Id: IsNull(), + }; + searchShortName = `CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 1) { + typeCondition = { + orgChild1Id: body.id, + }; + if (!body.isAll) { + checkChildConditions = { + orgChild2Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 2) { + typeCondition = { + orgChild2Id: body.id, + }; + if (!body.isAll) { + checkChildConditions = { + orgChild3Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 3) { + typeCondition = { + orgChild3Id: body.id, + }; + if (!body.isAll) { + checkChildConditions = { + orgChild4Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } else { + } + } else if (body.type === 4) { + typeCondition = { + orgChild4Id: body.id, + }; + searchShortName = `CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`; + } + let findPosition: any; + let masterId = new Array(); + if (body.keyword != null && body.keyword != "") { + const findTypes: EmployeePosType[] = await this.employeePosTypeRepository.find({ + where: { posTypeName: Like(`%${body.keyword}%`) }, + select: ["id"], + }); + findPosition = await this.employeePositionRepository.find({ + where: { posTypeId: In(findTypes.map((x) => x.id)) }, + select: ["posMasterId"], + }); + masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); + // const findLevel: EmployeePosLevel[] = await this.employeePosLevelRepository.find({ + // where: { posLevelName: Like(`%${body.keyword}%`) }, + // select: ["id"], + // }); + // findPosition = await this.employeePositionRepository.find({ + // where: { posLevelId: In(findLevel.map((x) => x.id)) }, + // select: ["posMasterId"], + // }); + // masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); + findPosition = await this.employeePositionRepository.find({ + where: { positionName: Like(`%${body.keyword}%`) }, + select: ["posMasterId"], + }); + masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); + keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); + if (isNaN(keywordAsInt)) { + keywordAsInt = "P@ssw0rd!z"; + } + masterId = [...new Set(masterId)]; + } + + const revisionCondition = { + orgRevisionId: body.revisionId, + }; + const conditions = [ + { + ...checkChildConditions, + ...typeCondition, + ...revisionCondition, + ...(body.keyword && + (masterId.length > 0 + ? { id: In(masterId) } + : { posMasterNo: Like(`%${body.keyword}%`) })), + }, + ]; + + const [posMaster, total] = await AppDataSource.getRepository(EmployeePosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") + .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") + .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") + .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") + .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .leftJoinAndSelect("posMaster.next_holder", "next_holder") + .leftJoinAndSelect("posMaster.orgRevision", "orgRevision") + .where(conditions) + .orWhere( + new Brackets((qb) => { + qb.andWhere( + body.keyword != null && body.keyword != "" + ? body.isAll == false + ? searchShortName + : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` + : "1=1", + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition); + }), + ) + .orWhere( + new Brackets((qb) => { + qb.andWhere( + body.keyword != null && body.keyword != "" + ? `CONCAT(current_holder.prefix, current_holder.firstName," ",current_holder.lastName) like '%${body.keyword}%'` + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition); + }), + ) + .orWhere( + new Brackets((qb) => { + qb.andWhere( + body.keyword != null && body.keyword != "" + ? `CASE WHEN orgRevision.orgRevisionIsDraft = true THEN CONCAT(next_holder.prefix, next_holder.firstName,' ', next_holder.lastName) ELSE CONCAT(current_holder.prefix, current_holder.firstName,' ' , current_holder.lastName) END LIKE '%${body.keyword}%'` + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition); + }), + ) + .orderBy("posMaster.posMasterOrder", "ASC") + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + + const formattedData = await Promise.all( + posMaster.map(async (posMaster) => { + const positions = await this.employeePositionRepository.find({ + where: { + posMasterId: posMaster.id, + }, + relations: ["posLevel", "posType", "posExecutive"], + }); + + let profile: any; + const chkRevision = await this.orgRevisionRepository.findOne({ + where: { id: posMaster.orgRevisionId }, + }); + if (chkRevision?.orgRevisionIsCurrent && !chkRevision?.orgRevisionIsDraft) { + profile = await this.profileRepository.findOne({ + where: { id: String(posMaster.current_holderId) }, + }); + } else if (!chkRevision?.orgRevisionIsCurrent && chkRevision?.orgRevisionIsDraft) { + profile = await this.profileRepository.findOne({ + where: { id: String(posMaster.next_holderId) }, + }); + } + const type = await this.employeePosTypeRepository.findOne({ + where: { id: String(profile?.posTypeId) }, + }); + const level = await this.employeePosLevelRepository.findOne({ + where: { id: String(profile?.posLevelId) }, + }); + + let shortName = ""; + + if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id == null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null + ) { + body.type = 0; + shortName = posMaster.orgRoot.orgRootShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null + ) { + body.type = 1; + shortName = posMaster.orgChild1.orgChild1ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id == null + ) { + body.type = 2; + shortName = posMaster.orgChild2.orgChild2ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id !== null + ) { + body.type = 3; + shortName = posMaster.orgChild3.orgChild3ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id !== null + ) { + body.type = 4; + shortName = posMaster.orgChild4.orgChild4ShortName; + } + + return { + id: posMaster.id, + orgRootId: posMaster.orgRootId, + orgChild1Id: posMaster.orgChild1Id, + orgChild2Id: posMaster.orgChild2Id, + orgChild3Id: posMaster.orgChild3Id, + orgChild4Id: posMaster.orgChild4Id, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + fullNameCurrentHolder: + posMaster.current_holder == null + ? null + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + fullNameNextHolder: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + orgShortname: shortName, + isSit: posMaster.isSit, + profilePosition: profile == null || profile.position == null ? null : profile.position, + profilePostype: type == null || type.posTypeName == null ? null : type.posTypeName, + profilePoslevel: level == null || level.posLevelName == null ? null : level.posLevelName, + positions: positions.map((position) => ({ + id: position.id, + positionName: position.positionName, + posTypeId: position.posTypeId, + posTypeName: position.posType == null ? null : position.posType.posTypeName, + posLevelId: position.posLevelId, + posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, + positionIsSelected: position.positionIsSelected, + })), + }; + }), + ); + return new HttpSuccess({ data: formattedData, total }); + } + + /** + * API จัดลำดับตำแหน่ง + * + * @summary ORG_ - จัดลำดับตำแหน่ง (ADMIN) + * + */ + @Post("sort") + async SortEmp(@Body() requestBody: { id: string; type: number; sortId: string[] }) { + switch (requestBody.type) { + case 0: { + const rootId = await this.employeePosMasterRepository.findOne({ + where: { orgRootId: requestBody.id }, + }); + if (!rootId?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: " + requestBody.id); + } + const listPosMasterId_0 = await this.employeePosMasterRepository.find({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull(), + orgChild2Id: IsNull(), + orgChild3Id: IsNull(), + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 0."); + } + const sortData_0 = listPosMasterId_0.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.employeePosMasterRepository.save(sortData_0); + break; + } + + case 1: { + const child1Id = await this.employeePosMasterRepository.findOne({ + where: { orgChild1Id: requestBody.id }, + }); + if (!child1Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: " + requestBody.id); + } + const listPosMasterId_1 = await this.employeePosMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull(), + orgChild3Id: IsNull(), + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 1."); + } + const sortData_1 = listPosMasterId_1.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.employeePosMasterRepository.save(sortData_1); + break; + } + + case 2: { + const child2Id = await this.employeePosMasterRepository.findOne({ + where: { orgChild2Id: requestBody.id }, + }); + if (!child2Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: " + requestBody.id); + } + const listPosMasterId_2 = await this.employeePosMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull(), + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 2."); + } + const sortData_2 = listPosMasterId_2.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.employeePosMasterRepository.save(sortData_2); + break; + } + + case 3: { + const child3Id = await this.employeePosMasterRepository.findOne({ + where: { orgChild3Id: requestBody.id }, + }); + if (!child3Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found chil3Id: " + requestBody.id); + } + const listPosMasterId_3 = await this.employeePosMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), + orgChild2Id: Not(IsNull()), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull(), + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_3) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 3."); + } + const sortData_3 = listPosMasterId_3.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.employeePosMasterRepository.save(sortData_3); + break; + } + + case 4: { + const child4Id = await this.employeePosMasterRepository.findOne({ + where: { orgChild4Id: requestBody.id }, + }); + if (!child4Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id: " + requestBody.id); + } + const listPosMasterId_4 = await this.employeePosMasterRepository.find({ + where: { + orgRootId: Not(IsNull()), + orgChild1Id: Not(IsNull()), + orgChild2Id: Not(IsNull()), + orgChild3Id: Not(IsNull()), + orgChild4Id: requestBody.id, + }, + select: ["id", "posMasterOrder"], + }); + if (!listPosMasterId_4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 4."); + } + const sortData_4 = listPosMasterId_4.map((data) => ({ + id: data.id, + posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.employeePosMasterRepository.save(sortData_4); + break; + } + + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.type); + } + return new HttpSuccess(); + } + + /** + * API ดูประวัติอัตรากำลัง + * + * @summary ORG_ - ดูประวัติอัตรากำลัง (ADMIN) + * + * @param {string} id Id อัตรากำลัง + */ + @Get("history/{id}") + async getEmpHistoryPosMater(@Path() id: string) { + const posMaster = await this.employeePosMasterRepository.findOne({ + where: { id }, + }); + if (!posMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + } + const posMasters = await this.employeePosMasterRepository.find({ + where: { + ancestorDNA: + posMaster.ancestorDNA == null || posMaster.ancestorDNA == "" + ? "123" + : posMaster.ancestorDNA, + }, + order: { lastUpdatedAt: "DESC" }, + relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], + }); + const _data = posMasters.map((item) => ({ + id: item.id, + orgShortName: + item.orgRoot == null + ? null + : item.orgChild1 == null + ? item.orgRoot.orgRootShortName + : item.orgChild2 == null + ? item.orgChild1.orgChild1ShortName + : item.orgChild3 == null + ? item.orgChild2.orgChild2ShortName + : item.orgChild4 == null + ? item.orgChild3.orgChild3ShortName + : item.orgChild4.orgChild4ShortName, + lastUpdatedAt: item.lastUpdatedAt ? item.lastUpdatedAt : null, + posMasterNoPrefix: item.posMasterNoPrefix ? item.posMasterNoPrefix : null, + posMasterNo: item.posMasterNo ? item.posMasterNo : null, + posMasterNoSuffix: item.posMasterNoSuffix ? item.posMasterNoSuffix : null, + })); + return new HttpSuccess(_data); + } + + /** + * API ย้ายอัตรากำลัง + * + * @summary ORG_ - ย้ายอัตรากำลัง (ADMIN) + * + */ + @Post("move") + async moveEmpPosMaster( + @Body() requestBody: { id: string; type: number; positionMaster: string[] }, + @Request() request: { user: Record }, + ) { + const posMasters = await this.employeePosMasterRepository.find({ + where: { id: In(requestBody.positionMaster) }, + }); + + const type0LastPosMasterNo = + requestBody.type == 0 + ? await this.employeePosMasterRepository.find({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull(), + }, + }) + : []; + + const type1LastPosMasterNo = + requestBody.type == 1 + ? await this.employeePosMasterRepository.find({ + where: { + orgChild1Id: requestBody.id, + orgChild2Id: IsNull(), + }, + }) + : []; + + const type2LastPosMasterNo = + requestBody.type == 2 + ? await this.employeePosMasterRepository.find({ + where: { + orgChild2Id: requestBody.id, + orgChild3Id: IsNull(), + }, + }) + : []; + + const type3LastPosMasterNo = + requestBody.type == 3 + ? await this.employeePosMasterRepository.find({ + where: { + orgChild3Id: requestBody.id, + orgChild4Id: IsNull(), + }, + }) + : []; + + const type4LastPosMasterNo = + requestBody.type == 4 + ? await this.employeePosMasterRepository.find({ + where: { + orgChild4Id: requestBody.id, + }, + }) + : []; + + const allLastPosMasterNo = [ + ...type0LastPosMasterNo, + ...type1LastPosMasterNo, + ...type2LastPosMasterNo, + ...type3LastPosMasterNo, + ...type4LastPosMasterNo, + ]; + + let maxPosMasterNo = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterNo), 0); + + let maxPosMasterOrder = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterOrder), 0); + posMasters.forEach(async (posMaster: any) => { + let change = true; + + if (requestBody.type == 0) { + const org = await this.orgRootRepository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + if ( + posMaster.orgRootId == org.id && + posMaster.orgChild1Id == null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild1Id = null; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } + } + if (requestBody.type == 1) { + const org = await this.child1Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + if ( + posMaster.orgChild1Id == org.id && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } + } + if (requestBody.type == 2) { + const org = await this.child2Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + if ( + posMaster.orgChild2Id == org.id && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } + } + if (requestBody.type == 3) { + const org = await this.child3Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + if (posMaster.orgChild3Id == org.id && posMaster.orgChild4Id == null) change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild4Id = null; + } + } + if (requestBody.type == 4) { + const org = await this.child4Repository.findOne({ + where: { id: requestBody.id }, + }); + if (org != null) { + if (posMaster.orgChild4Id == org.id) change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.orgChild3Id; + posMaster.orgChild4Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + } + } + if (change == true) { + posMaster.posMasterNo = maxPosMasterNo += 1; + posMaster.posMasterOrder = maxPosMasterOrder += 1; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + await this.employeePosMasterRepository.save(posMaster); + } + }); + return new HttpSuccess(); + } + + /** + * API ตำแหน่งทั้งหมด + * + * @summary ORG_ - ตำแหน่งทั้งหมด (ADMIN) + * + */ + @Post("summary") + async PositionEmpSummary(@Body() requestBody: { id: string; type: number; isNode: boolean }) { + let summary: any; + let totalPosition: any; + let totalPositionCurrentUse: any; + let totalPositionCurrentVacant: any; + let totalPositionNextUse: any; + let totalPositionNextVacant: any; + + if (requestBody.isNode === true) { + switch (requestBody.type) { + case 0: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { orgRootId: requestBody.id }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + case 1: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { orgChild1Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + case 2: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { orgChild2Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + case 3: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { orgChild3Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + case 4: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { orgChild4Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + default: + break; + } + } else { + switch (requestBody.type) { + case 0: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 1: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: requestBody.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 2: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: requestBody.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 3: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: requestBody.id, + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 4: { + totalPosition = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + }, + }); + totalPositionCurrentUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.employeePosMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id: Not(IsNull()) || Not(""), + orgChild2Id: Not(IsNull()) || Not(""), + orgChild3Id: Not(IsNull()) || Not(""), + orgChild4Id: requestBody.id, + next_holderId: IsNull() || "", + }, + }); + break; + } + default: + break; + } + } + + summary = { + totalPosition: totalPosition, + totalPositionCurrentUse: totalPositionCurrentUse, + totalPositionCurrentVacant: totalPositionCurrentVacant, + totalPositionNextUse: totalPositionNextUse, + totalPositionNextVacant: totalPositionNextVacant, + }; + return new HttpSuccess(summary); + } + /** + * API สร้างคนครองตำแหน่ง + * + * @summary ORG_ - สร้างคนครองตำแหน่ง (ADMIN) + * + */ + @Post("profile") + async createEmpHolder( + @Body() requestBody: { posMaster: string; position: string; profileId: string; isSit: boolean }, + ) { + const dataMaster = await this.employeePosMasterRepository.findOne({ + where: { id: requestBody.posMaster }, + relations: ["positions"], + }); + if (!dataMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + } + dataMaster.positions.forEach(async (position) => { + if (position.id === requestBody.position) { + position.positionIsSelected = true; + } else { + position.positionIsSelected = false; + } + await this.employeePositionRepository.save(position); + }); + + dataMaster.isSit = requestBody.isSit; + dataMaster.next_holderId = requestBody.profileId; + await this.employeePosMasterRepository.save(dataMaster); + + return new HttpSuccess(); + } + + /** + * API ลบคนครองตำแหน่ง + * + * @summary ORG_ - ลบคนครองตำแหน่ง (ADMIN) + * + * @param {string} id *Id posMaster + */ + @Post("profile/delete/{id}") + async deleteEmpHolder(@Path() id: string) { + const dataMaster = await this.employeePosMasterRepository.findOne({ + where: { id: id }, + relations: ["positions"], + }); + if (!dataMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + } + await this.employeePosMasterRepository.update(id, { + isSit: false, + next_holderId: null, + }); + + dataMaster.positions.forEach(async (position) => { + await this.employeePositionRepository.update(position.id, { + positionIsSelected: false, + }); + }); + + return new HttpSuccess(); + } + + /** + * API สืบทอดตำแหน่ง + * + * @summary ORG_ - สืบทอดตำแหน่ง (ADMIN) + * + */ + @Post("dna") + async dnaEmp(@Body() requestBody: { draftPositionId: string; publishPositionId: string }) { + const findDraft = await this.orgRevisionRepository.findOne({ + where: { + orgRevisionIsDraft: true, + }, + }); + if (!findDraft) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้างที่เผยแพร่"); + } + + const dataPublish = await this.employeePosMasterRepository.findOne({ + where: { + id: requestBody.publishPositionId, + }, + }); + if (!dataPublish) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + } + + const dataDraft = await this.employeePosMasterRepository.findOne({ + where: { + id: requestBody.draftPositionId, + }, + }); + if (!dataDraft) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + } + + await this.employeePosMasterRepository.update( + { orgRevisionId: findDraft.id, ancestorDNA: dataPublish.ancestorDNA }, + { ancestorDNA: "" }, + ); + if (dataPublish.ancestorDNA == null || dataPublish.ancestorDNA == "") + dataPublish.ancestorDNA = dataPublish.id; + dataDraft.ancestorDNA = dataPublish.ancestorDNA; + await this.employeePosMasterRepository.save(dataDraft); + await this.employeePosMasterRepository.save(dataPublish); + + return new HttpSuccess(); + } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 30452ae2..79d0cca5 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -2097,7 +2097,7 @@ export class PositionController extends Controller { } /** - * API สร้างทะเบียนประวัติ + * API ลบคนครองตำแหน่ง * * @summary ORG_066 - ลบคนครองตำแหน่ง (ADMIN) #71 * diff --git a/src/entities/EmployeePosDict.ts b/src/entities/EmployeePosDict.ts index 070379fe..c708ed55 100644 --- a/src/entities/EmployeePosDict.ts +++ b/src/entities/EmployeePosDict.ts @@ -1,4 +1,4 @@ -import { Entity, Column, ManyToOne, JoinColumn, } from "typeorm"; +import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { EmployeePosType } from "./EmployeePosType"; import { EmployeePosLevel } from "./EmployeePosLevel"; @@ -17,21 +17,21 @@ export class EmployeePosDict extends EntityBase { length: 40, comment: "คีย์นอก(FK)ของตาราง employeePosType", }) - employeePosTypeId: string; + posTypeId: string; @Column({ length: 40, comment: "คีย์นอก(FK)ของตาราง employeePosLevel", }) - employeePosLevelId: string; + posLevelId: string; - @ManyToOne(() => EmployeePosType, (employeePosType) => employeePosType) - @JoinColumn({ name: "employeePosTypeId" }) - employeePosType: EmployeePosType; + @ManyToOne(() => EmployeePosType, (posType) => posType) + @JoinColumn({ name: "posTypeId" }) + posType: EmployeePosType; - @ManyToOne(() => EmployeePosLevel, (employeePosLevel) => employeePosLevel) - @JoinColumn({ name: "employeePosLevelId" }) - employeePosLevel: EmployeePosLevel; + @ManyToOne(() => EmployeePosLevel, (posLevel) => posLevel) + @JoinColumn({ name: "posLevelId" }) + posLevel: EmployeePosLevel; } export class CreateEmployeePosDict { @@ -39,10 +39,10 @@ export class CreateEmployeePosDict { posDictName: string | null; @Column("uuid") - employeePosTypeId: string | null; + posTypeId: string | null; @Column("uuid") - employeePosLevelId: string | null; + posLevelId: string | null; } export class UpdateEmployeePosDict { @@ -50,8 +50,8 @@ export class UpdateEmployeePosDict { posDictName: string; @Column("uuid") - employeePosTypeId: string; + posTypeId: string; @Column("uuid") - employeePosLevelId: string; -} \ No newline at end of file + posLevelId: string; +} diff --git a/src/entities/EmployeePosLevel.ts b/src/entities/EmployeePosLevel.ts index 70f71fd8..ae3c1713 100644 --- a/src/entities/EmployeePosLevel.ts +++ b/src/entities/EmployeePosLevel.ts @@ -21,15 +21,14 @@ export class EmployeePosLevel extends EntityBase { length: 40, comment: "คีย์นอก(FK)ของตาราง employeePosType", }) - employeePosTypeId: string; + posTypeId: string; - @ManyToOne(() => EmployeePosType, (employeePosType) => employeePosType.employeePosLevels) - @JoinColumn({ name: "employeePosTypeId" }) - employeePosType: EmployeePosType; - - @OneToMany(() => EmployeePosDict, (employeePosDict) => employeePosDict.employeePosLevel) - employeePosDicts: EmployeePosDict[]; + @ManyToOne(() => EmployeePosType, (posType) => posType.posLevels) + @JoinColumn({ name: "posTypeId" }) + posType: EmployeePosType; + @OneToMany(() => EmployeePosDict, (posDict) => posDict.posLevel) + posDicts: EmployeePosDict[]; } export class CreateEmployeePosLevel { @@ -40,8 +39,7 @@ export class CreateEmployeePosLevel { posLevelRank: number; @Column("uuid") - employeePosTypeId: string; - + posTypeId: string; } -export type UpdateEmployeePosLevel= Partial; \ No newline at end of file +export type UpdateEmployeePosLevel = Partial; diff --git a/src/entities/EmployeePosMaster.ts b/src/entities/EmployeePosMaster.ts new file mode 100644 index 00000000..bef2b22c --- /dev/null +++ b/src/entities/EmployeePosMaster.ts @@ -0,0 +1,218 @@ +import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany, ManyToMany } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { CreateEmployeePosDict } from "./EmployeePosDict"; +import { OrgRevision } from "./OrgRevision"; +import { CreateEmployeePosition, EmployeePosition } from "./EmployeePosition"; +import { OrgRoot } from "./OrgRoot"; +import { OrgChild1 } from "./OrgChild1"; +import { OrgChild2 } from "./OrgChild2"; +import { OrgChild3 } from "./OrgChild3"; +import { OrgChild4 } from "./OrgChild4"; +import { Profile } from "./Profile"; + +enum EmployeePosMasterLine { + MAIN = "MAIN", + SUPPORT = "SUPPORT", +} + +@Entity("employeePosMaster") +export class EmployeePosMaster extends EntityBase { + @Column({ + nullable: true, + comment: "Prefix นำหน้าเลขที่ตำแหน่ง เป็น Optional (ไม่ใช่อักษรย่อของหน่วยงาน/ส่วนราชการ)", + length: 16, + default: null, + }) + posMasterNoPrefix: string; + + @Column({ + nullable: true, + comment: "เลขที่ตำแหน่ง เป็นตัวเลข", + default: null, + }) + posMasterNo: number; + + @Column({ + nullable: true, + comment: "Suffix หลังเลขที่ตำแหน่ง เช่น ช.", + length: 16, + default: null, + }) + posMasterNoSuffix: string; + + @Column({ + nullable: true, + type: "datetime", + comment: "วัน-เวลาที่สร้าง", + default: null, + }) + posMasterCreatedAt: Date; + + @Column({ + nullable: true, + comment: + "รหัส DNA ใช้ในกรณีที่มีการทำสำเนาโครงสร้างและตำแหน่ง ตำแหน่งที่ทำสำเนามากับตำแหน่งเก่าจะต้องมี DNA เดียวกัน เพื่อให้ track ประวัติการแก้ไขตำแหน่งย้อนหลังได้", + length: 40, + default: null, + }) + ancestorDNA: string; + + @Column({ + nullable: true, + comment: "ลำดับที่แสดงผล", + default: null, + }) + posMasterOrder: number; + + @Column({ + nullable: true, + comment: "ลำดับความสำคัญ", + default: null, + }) + posMasterPriority: number; + + @Column({ + nullable: true, + comment: "สายงานในอัตรากำลัง (หลัก / สนับสนุน) คนละฟิลด์กับสายงานของตำแหน่ง", + type: "enum", + enum: EmployeePosMasterLine, + default: null, + }) + posMasterLine: EmployeePosMasterLine; + + @Column({ + comment: "นั่งทับตำแหน่งไหม", + default: false, + }) + isSit: boolean; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง orgRoot", + default: null, + }) + orgRootId?: string | null; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง orgChild1", + default: null, + }) + orgChild1Id?: string | null; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง orgChild2", + default: null, + }) + orgChild2Id?: string | null; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง orgChild3", + default: null, + }) + orgChild3Id?: string | null; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง orgChild4", + default: null, + }) + orgChild4Id?: string | null; + + @Column({ + nullable: true, + length: 40, + comment: + "คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้", + default: null, + }) + current_holderId?: string | null; + + @Column({ + nullable: true, + length: 40, + comment: + "คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย", + default: null, + }) + next_holderId?: string | null; + + @Column({ + length: 40, + comment: "คีย์นอก(FK)ของตาราง orgRevision", + }) + orgRevisionId: string; //fk + + @ManyToOne(() => OrgRevision, (orgRevision) => orgRevision.posMasters) + @JoinColumn({ name: "orgRevisionId" }) + orgRevision: OrgRevision; + + @ManyToOne(() => OrgRoot, (orgRoot) => orgRoot.posMasters) + @JoinColumn({ name: "orgRootId" }) + orgRoot: OrgRoot; + + @ManyToOne(() => OrgChild1, (orgChild1) => orgChild1.posMasters) + @JoinColumn({ name: "orgChild1Id" }) + orgChild1: OrgChild1; + + @ManyToOne(() => OrgChild2, (orgChild2) => orgChild2.posMasters) + @JoinColumn({ name: "orgChild2Id" }) + orgChild2: OrgChild2; + + @ManyToOne(() => OrgChild3, (orgChild3) => orgChild3.posMasters) + @JoinColumn({ name: "orgChild3Id" }) + orgChild3: OrgChild3; + + @ManyToOne(() => OrgChild4, (orgChild4) => orgChild4.posMasters) + @JoinColumn({ name: "orgChild4Id" }) + orgChild4: OrgChild4; + + @ManyToOne(() => Profile, (posMaster) => posMaster.current_holders) + @JoinColumn({ name: "current_holderId" }) + current_holder: Profile; + + @ManyToOne(() => Profile, (posMaster) => posMaster.next_holders) + @JoinColumn({ name: "next_holderId" }) + next_holder: Profile; + + @OneToMany(() => EmployeePosition, (position) => position.posMaster) + positions: EmployeePosition[]; +} + +export class CreateEmployeePosMaster { + @Column() + posMasterNoPrefix: string; + + @Column() + posMasterNo: number; + + @Column() + posMasterNoSuffix: string; + + @Column("uuid") + positions: CreateEmployeePosDict[]; + + @Column("uuid") + orgRootId?: string | null; + + @Column("uuid") + orgChild1Id?: string | null; + + @Column("uuid") + orgChild2Id?: string | null; + + @Column("uuid") + orgChild3Id?: string | null; + + @Column("uuid") + orgChild4Id?: string | null; +} + +export type UpdateEmployeePosMaster = Partial; diff --git a/src/entities/EmployeePosType.ts b/src/entities/EmployeePosType.ts index cbb6ef9b..cdb4e4dc 100644 --- a/src/entities/EmployeePosType.ts +++ b/src/entities/EmployeePosType.ts @@ -26,12 +26,11 @@ export class EmployeePosType extends EntityBase { }) posTypeShortName: string; - @OneToMany(() => EmployeePosLevel, (employeePosLevel) => employeePosLevel.employeePosType) - employeePosLevels: EmployeePosLevel[]; + @OneToMany(() => EmployeePosLevel, (posLevel) => posLevel.posType) + posLevels: EmployeePosLevel[]; - @OneToMany(() => EmployeePosDict, (employeePosDict) => employeePosDict.employeePosType) - employeePosDicts: EmployeePosDict[]; - + @OneToMany(() => EmployeePosDict, (posDict) => posDict.posType) + posDicts: EmployeePosDict[]; } export class CreateEmployeePosType { @@ -43,7 +42,6 @@ export class CreateEmployeePosType { @Column() posTypeShortName: string; - } export type UpdateEmployeePosType = Partial; diff --git a/src/entities/EmployeePosition.ts b/src/entities/EmployeePosition.ts new file mode 100644 index 00000000..e363ba69 --- /dev/null +++ b/src/entities/EmployeePosition.ts @@ -0,0 +1,74 @@ +import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { EmployeePosType } from "./EmployeePosType"; +import { EmployeePosLevel } from "./EmployeePosLevel"; +import { EmployeePosMaster } from "./EmployeePosMaster"; + +@Entity("employeePosition") +export class EmployeePosition extends EntityBase { + @Column({ + nullable: true, + comment: "ชื่อตำแหน่ง", + length: 255, + default: null, + }) + positionName: string; + + @Column({ + length: 40, + comment: "คีย์นอก(FK)ของตาราง employeePosType", + }) + posTypeId: string; + + @Column({ + length: 40, + comment: "คีย์นอก(FK)ของตาราง employeePosLevel", + }) + posLevelId: string; + + @Column({ + comment: "เป็นตำแหน่งที่ถูกเลือกในรอบนั้นๆ หรือไม่?", + default: false, + }) + positionIsSelected: boolean; + + @Column({ + length: 40, + comment: "เชื่อมโยงกับตารางเลขที่ตำแหน่ง", + }) + posMasterId: string; + + @ManyToOne(() => EmployeePosMaster, (posMaster) => posMaster) + @JoinColumn({ name: "posMasterId" }) + posMaster: EmployeePosMaster; + + @ManyToOne(() => EmployeePosType, (posType) => posType) + @JoinColumn({ name: "posTypeId" }) + posType: EmployeePosType; + + @ManyToOne(() => EmployeePosLevel, (posLevel) => posLevel) + @JoinColumn({ name: "posLevelId" }) + posLevel: EmployeePosLevel; +} + +export class CreateEmployeePosition { + @Column() + positionName: string | null; + + @Column("uuid") + posTypeId: string | null; + + @Column("uuid") + posLevelId: string | null; +} + +export class UpdateEmployeePosition { + @Column() + positionName: string; + + @Column("uuid") + posTypeId: string; + + @Column("uuid") + posLevelId: string; +} diff --git a/src/migration/1710328629522-add_table_employeePosition.ts b/src/migration/1710328629522-add_table_employeePosition.ts new file mode 100644 index 00000000..c95c4031 --- /dev/null +++ b/src/migration/1710328629522-add_table_employeePosition.ts @@ -0,0 +1,38 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableEmployeePosition1710328629522 implements MigrationInterface { + name = 'AddTableEmployeePosition1710328629522' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE \`employeePosition\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`positionName\` varchar(255) NULL COMMENT 'ชื่อตำแหน่ง', \`posTypeId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosType', \`posLevelId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosLevel', \`positionIsSelected\` tinyint NOT NULL COMMENT 'เป็นตำแหน่งที่ถูกเลือกในรอบนั้นๆ หรือไม่?' DEFAULT 0, \`posMasterId\` varchar(40) NOT NULL COMMENT 'เชื่อมโยงกับตารางเลขที่ตำแหน่ง', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`CREATE TABLE \`employeePosMaster\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`posMasterNoPrefix\` varchar(16) NULL COMMENT 'Prefix นำหน้าเลขที่ตำแหน่ง เป็น Optional (ไม่ใช่อักษรย่อของหน่วยงาน/ส่วนราชการ)', \`posMasterNo\` int NULL COMMENT 'เลขที่ตำแหน่ง เป็นตัวเลข', \`posMasterNoSuffix\` varchar(16) NULL COMMENT 'Suffix หลังเลขที่ตำแหน่ง เช่น ช.', \`posMasterCreatedAt\` datetime NULL COMMENT 'วัน-เวลาที่สร้าง', \`ancestorDNA\` varchar(40) NULL COMMENT 'รหัส DNA ใช้ในกรณีที่มีการทำสำเนาโครงสร้างและตำแหน่ง ตำแหน่งที่ทำสำเนามากับตำแหน่งเก่าจะต้องมี DNA เดียวกัน เพื่อให้ track ประวัติการแก้ไขตำแหน่งย้อนหลังได้', \`posMasterOrder\` int NULL COMMENT 'ลำดับที่แสดงผล', \`posMasterPriority\` int NULL COMMENT 'ลำดับความสำคัญ', \`posMasterLine\` enum ('MAIN', 'SUPPORT') NULL COMMENT 'สายงานในอัตรากำลัง (หลัก / สนับสนุน) คนละฟิลด์กับสายงานของตำแหน่ง', \`isSit\` tinyint NOT NULL COMMENT 'นั่งทับตำแหน่งไหม' DEFAULT 0, \`orgRootId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง orgRoot', \`orgChild1Id\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง orgChild1', \`orgChild2Id\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง orgChild2', \`orgChild3Id\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง orgChild3', \`orgChild4Id\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง orgChild4', \`current_holderId\` varchar(40) NULL COMMENT 'คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้', \`next_holderId\` varchar(40) NULL COMMENT 'คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย', \`orgRevisionId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง orgRevision', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`ALTER TABLE \`employeePosition\` ADD CONSTRAINT \`FK_1c8174b8e11333448f250657c49\` FOREIGN KEY (\`posMasterId\`) REFERENCES \`employeePosMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosition\` ADD CONSTRAINT \`FK_c3b098a7bdf65ed6daec420d23e\` FOREIGN KEY (\`posTypeId\`) REFERENCES \`employeePosType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosition\` ADD CONSTRAINT \`FK_ef5fcb52f3920d3029a334ae19e\` FOREIGN KEY (\`posLevelId\`) REFERENCES \`employeePosLevel\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_85b7778f4b345c6c214f47e4a8f\` FOREIGN KEY (\`orgRevisionId\`) REFERENCES \`orgRevision\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_93aae3c3c292aadf0b3a4c9019e\` FOREIGN KEY (\`orgRootId\`) REFERENCES \`orgRoot\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_bbda5c4d36100593214af67f522\` FOREIGN KEY (\`orgChild1Id\`) REFERENCES \`orgChild1\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_768a012f53f8cb3a87876a541f5\` FOREIGN KEY (\`orgChild2Id\`) REFERENCES \`orgChild2\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_bce3a2a666b53ce5ac10aafbbfa\` FOREIGN KEY (\`orgChild3Id\`) REFERENCES \`orgChild3\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_c6dd2d044ce9a06c2e8e0e8af0f\` FOREIGN KEY (\`orgChild4Id\`) REFERENCES \`orgChild4\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_00221b20fdf6d460a86f108fc6d\` FOREIGN KEY (\`current_holderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` ADD CONSTRAINT \`FK_41945621a3f1e716dc3b2d994c3\` FOREIGN KEY (\`next_holderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_41945621a3f1e716dc3b2d994c3\``); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_00221b20fdf6d460a86f108fc6d\``); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_c6dd2d044ce9a06c2e8e0e8af0f\``); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_bce3a2a666b53ce5ac10aafbbfa\``); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_768a012f53f8cb3a87876a541f5\``); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_bbda5c4d36100593214af67f522\``); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_93aae3c3c292aadf0b3a4c9019e\``); + await queryRunner.query(`ALTER TABLE \`employeePosMaster\` DROP FOREIGN KEY \`FK_85b7778f4b345c6c214f47e4a8f\``); + await queryRunner.query(`ALTER TABLE \`employeePosition\` DROP FOREIGN KEY \`FK_ef5fcb52f3920d3029a334ae19e\``); + await queryRunner.query(`ALTER TABLE \`employeePosition\` DROP FOREIGN KEY \`FK_c3b098a7bdf65ed6daec420d23e\``); + await queryRunner.query(`ALTER TABLE \`employeePosition\` DROP FOREIGN KEY \`FK_1c8174b8e11333448f250657c49\``); + await queryRunner.query(`DROP TABLE \`employeePosMaster\``); + await queryRunner.query(`DROP TABLE \`employeePosition\``); + } + +} diff --git a/src/migration/1710330337194-add_table_employeePosition1.ts b/src/migration/1710330337194-add_table_employeePosition1.ts new file mode 100644 index 00000000..3bab54e3 --- /dev/null +++ b/src/migration/1710330337194-add_table_employeePosition1.ts @@ -0,0 +1,34 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableEmployeePosition11710330337194 implements MigrationInterface { + name = 'AddTableEmployeePosition11710330337194' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`employeePosLevel\` DROP FOREIGN KEY \`FK_a822851af7b58288be65debfdd5\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP FOREIGN KEY \`FK_28d45cec912604b7d6f3dfac39a\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP FOREIGN KEY \`FK_73ad56ef383399f567d58b213e2\``); + await queryRunner.query(`ALTER TABLE \`employeePosLevel\` CHANGE \`employeePosTypeId\` \`posTypeId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosType'`); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP COLUMN \`employeePosTypeId\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP COLUMN \`employeePosLevelId\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD \`posTypeId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosType'`); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD \`posLevelId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosLevel'`); + await queryRunner.query(`ALTER TABLE \`employeePosLevel\` ADD CONSTRAINT \`FK_7fb9ab868f3f46b44f460c984f1\` FOREIGN KEY (\`posTypeId\`) REFERENCES \`employeePosType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD CONSTRAINT \`FK_63e8a14abd222c948e0e175e941\` FOREIGN KEY (\`posTypeId\`) REFERENCES \`employeePosType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD CONSTRAINT \`FK_73da087c2ee4b6e74c9590ae3a2\` FOREIGN KEY (\`posLevelId\`) REFERENCES \`employeePosLevel\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP FOREIGN KEY \`FK_73da087c2ee4b6e74c9590ae3a2\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP FOREIGN KEY \`FK_63e8a14abd222c948e0e175e941\``); + await queryRunner.query(`ALTER TABLE \`employeePosLevel\` DROP FOREIGN KEY \`FK_7fb9ab868f3f46b44f460c984f1\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP COLUMN \`posLevelId\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` DROP COLUMN \`posTypeId\``); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD \`employeePosLevelId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosLevel'`); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD \`employeePosTypeId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosType'`); + await queryRunner.query(`ALTER TABLE \`employeePosLevel\` CHANGE \`posTypeId\` \`employeePosTypeId\` varchar(40) NOT NULL COMMENT 'คีย์นอก(FK)ของตาราง employeePosType'`); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD CONSTRAINT \`FK_73ad56ef383399f567d58b213e2\` FOREIGN KEY (\`employeePosTypeId\`) REFERENCES \`employeePosType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosDict\` ADD CONSTRAINT \`FK_28d45cec912604b7d6f3dfac39a\` FOREIGN KEY (\`employeePosLevelId\`) REFERENCES \`employeePosLevel\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`employeePosLevel\` ADD CONSTRAINT \`FK_a822851af7b58288be65debfdd5\` FOREIGN KEY (\`employeePosTypeId\`) REFERENCES \`employeePosType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + +}