From b089d9279ab1d931e8615e9c694df80d5711b589 Mon Sep 17 00:00:00 2001 From: kittapath Date: Tue, 19 Nov 2024 10:43:25 +0700 Subject: [PATCH 1/3] no message --- src/controllers/ReportController.ts | 352 ++++++++++++++---- src/controllers/WorkflowController.ts | 34 +- src/entities/ProfileEmployee.ts | 4 + src/entities/RoleKeycloak.ts | 8 + src/entities/StateOperatorUser.ts | 22 ++ src/entities/Workflow.ts | 8 + ...1941837869-updata_workflow_add_rmployee.ts | 20 + ...987222018-updata_workflow_add_rmployee1.ts | 14 + 8 files changed, 374 insertions(+), 88 deletions(-) create mode 100644 src/migration/1731941837869-updata_workflow_add_rmployee.ts create mode 100644 src/migration/1731987222018-updata_workflow_add_rmployee1.ts diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 5a63d0d8..b9753f1a 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3,7 +3,7 @@ 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 { In } from "typeorm"; +import { In, LessThan } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild1 } from "../entities/OrgChild1"; @@ -11,6 +11,7 @@ import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import Extension from "../interfaces/extension"; +import HttpStatus from "../interfaces/http-status"; @Route("api/v1/org/report") @Tags("Report") @Security("bearerAuth") @@ -2843,6 +2844,189 @@ export class ReportController extends Controller { return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } + /** + * API Report2 + * + * @summary Report2 + * + */ + @Get("report2-history/{rootId}") + async findReport2History(@Path() rootId: string) { + const orgRootData = await this.orgRootRepository.find({ + where: { + id: rootId, + }, + order: { orgRootOrder: "ASC" }, + relations: [ + "orgRevision", + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.posLevel", + "posMasters.current_holder.posType", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", + "posMasters.current_holder.current_holders", + "posMasters.current_holder.current_holders.positions", + "posMasters.current_holder.current_holders.orgRoot", + "posMasters.current_holder.current_holders.orgChild1", + "posMasters.current_holder.current_holders.orgChild2", + "posMasters.current_holder.current_holders.orgChild3", + "posMasters.current_holder.current_holders.orgChild4", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + + const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; + const orgChild1Data = await this.child1Repository.find({ + where: { + orgRootId: In(orgRootIds), + }, + order: { orgChild1Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.posLevel", + "posMasters.current_holder.posType", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", + "posMasters.current_holder.current_holders", + "posMasters.current_holder.current_holders.positions", + "posMasters.current_holder.current_holders.orgRoot", + "posMasters.current_holder.current_holders.orgChild1", + "posMasters.current_holder.current_holders.orgChild2", + "posMasters.current_holder.current_holders.orgChild3", + "posMasters.current_holder.current_holders.orgChild4", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + + const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; + const orgChild2Data = await this.child2Repository.find({ + where: { + orgChild1: In(orgChild1Ids), + }, + order: { orgChild2Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.posLevel", + "posMasters.current_holder.posType", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", + "posMasters.current_holder.current_holders", + "posMasters.current_holder.current_holders.positions", + "posMasters.current_holder.current_holders.orgRoot", + "posMasters.current_holder.current_holders.orgChild1", + "posMasters.current_holder.current_holders.orgChild2", + "posMasters.current_holder.current_holders.orgChild3", + "posMasters.current_holder.current_holders.orgChild4", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + + const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; + const orgChild3Data = await this.child3Repository.find({ + where: { + orgChild2: In(orgChild2Ids), + }, + order: { orgChild3Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.posLevel", + "posMasters.current_holder.posType", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", + "posMasters.current_holder.current_holders", + "posMasters.current_holder.current_holders.positions", + "posMasters.current_holder.current_holders.orgRoot", + "posMasters.current_holder.current_holders.orgChild1", + "posMasters.current_holder.current_holders.orgChild2", + "posMasters.current_holder.current_holders.orgChild3", + "posMasters.current_holder.current_holders.orgChild4", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + + const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; + const orgChild4Data = await this.child4Repository.find({ + where: { + orgChild3: In(orgChild3Ids), + }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.posLevel", + "posMasters.current_holder.posType", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", + "posMasters.current_holder.current_holders", + "posMasters.current_holder.current_holders.positions", + "posMasters.current_holder.current_holders.orgRoot", + "posMasters.current_holder.current_holders.orgChild1", + "posMasters.current_holder.current_holders.orgChild2", + "posMasters.current_holder.current_holders.orgChild3", + "posMasters.current_holder.current_holders.orgChild4", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + + if (orgRootData.length < 0) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบโครงสร้าง"); + const _orgRevisionActive = await this.orgRevisionRepository.find({ + where: { createdAt: LessThan(orgRootData[0]?.createdAt ?? new Date()) }, + order: { createdAt: "DESC" }, + skip: 1, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + return new HttpSuccess(); + } /** * API Report3 * @@ -4088,7 +4272,6 @@ export class ReportController extends Controller { @Get("report4/{rootId}") async findReport4(@Path() rootId: string) { - const orgRootData = await this.orgRootRepository.find({ where: { id: rootId, @@ -4103,86 +4286,91 @@ export class ReportController extends Controller { ], }); let data: any = []; - var _data = [{ - type: "ทั่วไป", - level: "ปฎิบัติงาน", - total: "0", - remark: "-" - }, - { - type: "", - level: "ชำนาญงาน", - total: "0", - remark: "-" - }, - { - type: "", - level: "อาวุโส", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", - remark: "" - }, - { - type: "วิชาการ", - level: "ปฎิบัติการ", - total: "0", - remark: "-" - }, - { - type: "", - level: "ชำนาญการ", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", - remark: "" - }, - { - type: "อำนวยการ", - level: "ต้น", - total: "0", - remark: "-" - }, - { - type: "", - level: "สูง", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", - remark: "" - }, - { - type: "บริหาร", - level: "ต้น", - total: "0", - remark: "-" - }, - { - type: "", - level: "สูง", - total: "0", - remark: "-" - }, - { - type: "", - level: "รวม", - total: "0", - remark: "" - } - ] + var _data = [ + { + type: "ทั่วไป", + level: "ปฎิบัติงาน", + total: "0", + remark: "-", + }, + { + type: "", + level: "ชำนาญงาน", + total: "0", + remark: "-", + }, + { + type: "", + level: "อาวุโส", + total: "0", + remark: "-", + }, + { + type: "", + level: "รวม", + total: "0", + remark: "", + }, + { + type: "วิชาการ", + level: "ปฎิบัติการ", + total: "0", + remark: "-", + }, + { + type: "", + level: "ชำนาญการ", + total: "0", + remark: "-", + }, + { + type: "", + level: "รวม", + total: "0", + remark: "", + }, + { + type: "อำนวยการ", + level: "ต้น", + total: "0", + remark: "-", + }, + { + type: "", + level: "สูง", + total: "0", + remark: "-", + }, + { + type: "", + level: "รวม", + total: "0", + remark: "", + }, + { + type: "บริหาร", + level: "ต้น", + total: "0", + remark: "-", + }, + { + type: "", + level: "สูง", + total: "0", + remark: "-", + }, + { + type: "", + level: "รวม", + total: "0", + remark: "", + }, + ]; data.push(_data); - return new HttpSuccess({ template: "report4", reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()) , data: _data } }); + return new HttpSuccess({ + template: "report4", + reportName: "report4", + data: { dateCurrent: Extension.ToThaiShortDate(new Date()), data: _data }, + }); } } diff --git a/src/controllers/WorkflowController.ts b/src/controllers/WorkflowController.ts index 7ac69d7f..f38b2317 100644 --- a/src/controllers/WorkflowController.ts +++ b/src/controllers/WorkflowController.ts @@ -21,6 +21,7 @@ import { PosMasterAct } from "../entities/PosMasterAct"; import { getAllJSDocTagsOfKind } from "typescript"; import { viewDirectorActing } from "../entities/view/viewDirectorActing"; import { viewDirector } from "../entities/view/viewDirector"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; @Route("api/v1/org/workflow") @Tags("Workflow") @@ -32,6 +33,7 @@ export class WorkflowController extends Controller { private stateOperatorUserRepo = AppDataSource.getRepository(StateOperatorUser); private stateUserCommentRepo = AppDataSource.getRepository(StateUserComment); private profileRepo = AppDataSource.getRepository(Profile); + private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private metaWorkflowRepo = AppDataSource.getRepository(MetaWorkflow); private metaStateRepo = AppDataSource.getRepository(MetaState); @@ -51,12 +53,21 @@ export class WorkflowController extends Controller { posTypeName: string; }, ) { - const profile = await this.profileRepo.findOne({ + let profileType = "OFFICER"; + let profile: any = await this.profileRepo.findOne({ where: { keycloak: req.user.sub, }, }); - if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน"); + if (!profile) { + profileType = "EMPLOYEE"; + profile = await this.profileEmployeeRepo.findOne({ + where: { + keycloak: req.user.sub, + }, + }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน"); + } const metaWorkflow = await this.metaWorkflowRepo.findOne({ where: { @@ -81,6 +92,7 @@ export class WorkflowController extends Controller { id: undefined, ...meta, ...body, + profileType: profileType, system: body.sysName, }); await this.workflowRepo.save(workflow); @@ -117,7 +129,9 @@ export class WorkflowController extends Controller { const stateOperatorUser = new StateOperatorUser(); Object.assign(stateOperatorUser, { - profileId: profile.id, + profileId: profileType == "OFFICER" ? profile.id : null, + profileEmployeeId: profileType != "OFFICER" ? profile.id : null, + profileType: profileType, operator: "Owner", order: 1, workflowId: workflow.id, @@ -205,14 +219,22 @@ export class WorkflowController extends Controller { action: string; }, ) { - const stateOperatorUser = await this.stateOperatorUserRepo.findOne({ + let stateOperatorUser = await this.stateOperatorUserRepo.findOne({ where: { profileId: body.profileId, workflowId: body.workflowId, }, }); - if (!stateOperatorUser) - throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในกระบวนการนี้"); + if (!stateOperatorUser) { + stateOperatorUser = await this.stateOperatorUserRepo.findOne({ + where: { + profileEmployeeId: body.profileId, + workflowId: body.workflowId, + }, + }); + if (!stateOperatorUser) + throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในกระบวนการนี้"); + } const operator = await this.stateOperatorRepo.findOne({ where: { diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index 3a0da09d..31bad68c 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -34,6 +34,7 @@ import { ProfileEdit } from "./ProfileEdit"; import { ProfileDevelopment } from "./ProfileDevelopment"; import { DevelopmentRequest } from "./DevelopmentRequest"; import { RoleKeycloak } from "./RoleKeycloak"; +import { StateOperatorUser } from "./StateOperatorUser"; @Entity("profileEmployee") export class ProfileEmployee extends EntityBase { @@ -695,6 +696,9 @@ export class ProfileEmployee extends EntityBase { @OneToMany(() => ProfileFamilyCouple, (v) => v.profile) profileFamilyCouple: ProfileFamilyCouple[]; + @OneToMany(() => StateOperatorUser, (v) => v.profile) + stateOperatorUsers: StateOperatorUser[]; + //ที่อยู่ @Column({ nullable: true, diff --git a/src/entities/RoleKeycloak.ts b/src/entities/RoleKeycloak.ts index c1a9083b..556d338b 100644 --- a/src/entities/RoleKeycloak.ts +++ b/src/entities/RoleKeycloak.ts @@ -13,6 +13,14 @@ export class RoleKeycloak extends EntityBase { }) name: string; + @Column({ + nullable: true, + comment: "คำอธิบาย", + length: 255, + default: null, + }) + description: string; + @ManyToMany(() => Profile, (profile) => profile.roleKeycloaks) profiles: Profile[]; diff --git a/src/entities/StateOperatorUser.ts b/src/entities/StateOperatorUser.ts index 2fb40199..90e906b3 100644 --- a/src/entities/StateOperatorUser.ts +++ b/src/entities/StateOperatorUser.ts @@ -2,6 +2,7 @@ import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { Workflow } from "./Workflow"; import { Profile } from "./Profile"; +import { ProfileEmployee } from "./ProfileEmployee"; @Entity("stateOperatorUser") export class StateOperatorUser extends EntityBase { @@ -13,6 +14,14 @@ export class StateOperatorUser extends EntityBase { }) operator: string; + @Column({ + nullable: true, + comment: "ผู้ใช้งาน", + length: 255, + default: null, + }) + profileType: string; + @Column({ nullable: true, comment: "ลำดับ", @@ -44,4 +53,17 @@ export class StateOperatorUser extends EntityBase { @ManyToOne(() => Profile, (profile) => profile.stateOperatorUsers) @JoinColumn({ name: "profileId" }) profile: Profile; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง profileEmployee", + type: "uuid", + default: null, + }) + profileEmployeeId: string; + + @ManyToOne(() => ProfileEmployee, (profileEmployee) => profileEmployee.stateOperatorUsers) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; } diff --git a/src/entities/Workflow.ts b/src/entities/Workflow.ts index e6804bf2..21b0a008 100644 --- a/src/entities/Workflow.ts +++ b/src/entities/Workflow.ts @@ -13,6 +13,14 @@ export class Workflow extends EntityBase { }) refId: string; + @Column({ + nullable: true, + comment: "ผู้ใช้งาน", + length: 255, + default: null, + }) + profileType: string; + @Column({ nullable: true, comment: "ชื่อ flow", diff --git a/src/migration/1731941837869-updata_workflow_add_rmployee.ts b/src/migration/1731941837869-updata_workflow_add_rmployee.ts new file mode 100644 index 00000000..5af00a31 --- /dev/null +++ b/src/migration/1731941837869-updata_workflow_add_rmployee.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdataWorkflowAddRmployee1731941837869 implements MigrationInterface { + name = 'UpdataWorkflowAddRmployee1731941837869' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`workflow\` ADD \`profileType\` varchar(255) NULL COMMENT 'ผู้ใช้งาน'`); + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` ADD \`profileType\` varchar(255) NULL COMMENT 'ผู้ใช้งาน'`); + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` ADD \`profileEmployeeId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง profileEmployee'`); + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` ADD CONSTRAINT \`FK_7039ce6f8c316e5bbdc879e801b\` FOREIGN KEY (\`profileEmployeeId\`) REFERENCES \`profileEmployee\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` DROP FOREIGN KEY \`FK_7039ce6f8c316e5bbdc879e801b\``); + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` DROP COLUMN \`profileEmployeeId\``); + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` DROP COLUMN \`profileType\``); + await queryRunner.query(`ALTER TABLE \`workflow\` DROP COLUMN \`profileType\``); + } + +} diff --git a/src/migration/1731987222018-updata_workflow_add_rmployee1.ts b/src/migration/1731987222018-updata_workflow_add_rmployee1.ts new file mode 100644 index 00000000..d3ad5270 --- /dev/null +++ b/src/migration/1731987222018-updata_workflow_add_rmployee1.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdataWorkflowAddRmployee11731987222018 implements MigrationInterface { + name = 'UpdataWorkflowAddRmployee11731987222018' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`roleKeycloak\` ADD \`description\` varchar(255) NULL COMMENT 'คำอธิบาย'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`roleKeycloak\` DROP COLUMN \`description\``); + } + +} From 3cd0b4e9da54b69d43ded65e6614f0687667badd Mon Sep 17 00:00:00 2001 From: kittapath Date: Tue, 19 Nov 2024 12:56:19 +0700 Subject: [PATCH 2/3] no message --- src/controllers/ReportController.ts | 1971 ++++++++++++++++++++++++++- 1 file changed, 1969 insertions(+), 2 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index b9753f1a..67a27ccb 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3012,11 +3012,12 @@ export class ReportController extends Controller { ], }); - if (orgRootData.length < 0) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบโครงสร้าง"); + if (orgRootData.length <= 0) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบโครงสร้าง"); const _orgRevisionActive = await this.orgRevisionRepository.find({ where: { createdAt: LessThan(orgRootData[0]?.createdAt ?? new Date()) }, order: { createdAt: "DESC" }, skip: 1, + take: 2, relations: [ "posMasters", "posMasters.positions", @@ -3025,7 +3026,1973 @@ export class ReportController extends Controller { "posMasters.positions.posExecutive", ], }); - return new HttpSuccess(); + if (_orgRevisionActive.length <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประวัติโครงสร้าง"); + } + let orgRevisionActive = _orgRevisionActive[0]; + + let data = new Array(); + let _node: any; + let no = 1; + for (let orgRoot of orgRootData) { + await Promise.all( + orgRoot.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild1Id == null) { + const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.current_holder != null) { + positionMasterProfileOld = posMaster.current_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + let education: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + let salary: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.current_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.current_holder.position, + posType: + posMaster.current_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType + ?.posTypeName + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + posLevel: + posMaster.current_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel + ?.posLevelName + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + posExecutive: + posMaster.current_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posExecutive?.posExecutiveName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.current_holder == null + ? orgRoot.orgRootName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.current_holder == null + ? orgRoot.orgRootShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + profilePosMasterNo: + posMaster.current_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.current_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.current_holder.position, + profilePosType: + posMaster.current_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + profilePosLevel: + posMaster.current_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.current_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + _node == null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _node = node; + } + }), + ); + _node = null; + + for (let orgChild1 of orgChild1Data.filter( + (orgChild1) => orgChild1.orgRootId === orgRoot.id, + )) { + await Promise.all( + orgChild1.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild2Id == null) { + const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.current_holder != null) { + positionMasterProfileOld = posMaster.current_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + let education: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + let salary: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.current_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.current_holder.position, + posType: + posMaster.current_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + posLevel: + posMaster.current_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + posExecutive: + posMaster.current_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posExecutive?.posExecutiveName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.current_holder == null + ? orgChild1.orgChild1Name + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.current_holder == null + ? orgChild1.orgChild1ShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + profilePosMasterNo: + posMaster.current_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.current_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.current_holder.position, + profilePosType: + posMaster.current_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + profilePosLevel: + posMaster.current_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.current_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + _node == null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _node = node; + } + }), + ); + _node = null; + + for (let orgChild2 of orgChild2Data.filter( + (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, + )) { + await Promise.all( + orgChild2.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild3Id == null) { + const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.current_holder != null) { + positionMasterProfileOld = posMaster.current_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + let education: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + let salary: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.current_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.current_holder.position, + posType: + posMaster.current_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + posLevel: + posMaster.current_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + posExecutive: + posMaster.current_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posExecutive?.posExecutiveName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.current_holder == null + ? orgChild2.orgChild2Name + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.current_holder == null + ? orgChild2.orgChild2ShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + profilePosMasterNo: + posMaster.current_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.current_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.current_holder.position, + profilePosType: + posMaster.current_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + profilePosLevel: + posMaster.current_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.current_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber( + node.profileOrgShortName.toString(), + ), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null + ? "" + : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber( + node.profilePosMasterNo.toString(), + ), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary + ? Extension.ToThaiNumber(node.salary.toLocaleString()) + : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + _node == null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null + ? "" + : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber( + node.profilePosMasterNo.toString(), + ), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary + ? Extension.ToThaiNumber(node.salary.toLocaleString()) + : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _node = node; + } + }), + ); + _node = null; + + for (let orgChild3 of orgChild3Data.filter( + (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, + )) { + await Promise.all( + orgChild3.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild4Id == null) { + const positionName = [ + ...new Set(posMaster.positions.map((x) => x.positionName)), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.current_holder != null) { + positionMasterProfileOld = posMaster.current_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + let education: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + let salary: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.current_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.current_holder.position, + posType: + posMaster.current_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + posLevel: + posMaster.current_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + posExecutive: + posMaster.current_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posExecutive?.posExecutiveName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.current_holder == null + ? orgChild3.orgChild3Name + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.current_holder == null + ? orgChild3.orgChild3ShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + profilePosMasterNo: + posMaster.current_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.current_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.current_holder.position, + profilePosType: + posMaster.current_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + profilePosLevel: + posMaster.current_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.current_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber( + node.profileOrgShortName.toString(), + ), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null + ? "" + : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null + ? "" + : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber( + node.profilePosMasterNo.toString(), + ), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary + ? Extension.ToThaiNumber(node.salary.toLocaleString()) + : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + _node == null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null + ? "" + : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null + ? "" + : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber( + node.profilePosMasterNo.toString(), + ), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary + ? Extension.ToThaiNumber(node.salary.toLocaleString()) + : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _node = node; + } + }), + ); + _node = null; + + for (let orgChild4 of orgChild4Data.filter( + (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, + )) { + await Promise.all( + orgChild4.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + const positionName = [ + ...new Set(posMaster.positions.map((x) => x.positionName)), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.current_holder != null) { + positionMasterProfileOld = posMaster.current_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + let education: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + let salary: any = ""; + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.current_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.current_holder.position, + posType: + posMaster.current_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + posLevel: + posMaster.current_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + posExecutive: + posMaster.current_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posExecutive?.posExecutiveName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.current_holder == null + ? orgChild4.orgChild4Name + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.current_holder == null + ? orgChild4.orgChild4ShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + profilePosMasterNo: + posMaster.current_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.current_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.current_holder.position, + profilePosType: + posMaster.current_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.current_holder.posType == null + ? "-" + : posMaster.current_holder.posType.posTypeName, + profilePosLevel: + posMaster.current_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.current_holder.posLevel == null + ? "-" + : posMaster.current_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.current_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber( + node.profileOrgShortName.toString(), + ), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null + ? "" + : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null + ? "" + : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber( + node.profilePosMasterNo.toString(), + ), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary + ? Extension.ToThaiNumber(node.salary.toLocaleString()) + : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + _node == null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null + ? "" + : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null + ? "" + : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber( + node.profilePosMasterNo.toString(), + ), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary + ? Extension.ToThaiNumber(node.salary.toLocaleString()) + : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _node = node; + }), + ); + _node = null; + } + } + } + } + } + return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /** * API Report3 From b456fe46ff51105ea4845bfa0ca2580721798b6c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 19 Nov 2024 14:34:33 +0700 Subject: [PATCH 3/3] sort data #798 --- src/controllers/PositionController.ts | 10 +++++++++- src/controllers/ProfileController.ts | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 0eb6f2fa..9cc106e4 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1121,7 +1121,15 @@ export class PositionController extends Controller { const positions = await this.positionRepository.find({ where: { posMasterId: posMaster.id }, relations: ["posType", "posLevel", "posExecutive"], - order: { lastUpdatedAt: "ASC" }, + // order: { lastUpdatedAt: "ASC" }, + order: { + posType: { + posTypeRank: "ASC" + }, + posLevel: { + posLevelRank: "ASC" + } + }, }); const formattedData = { id: posMaster.id, diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 5d5d9faf..77940e9e 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -4310,6 +4310,8 @@ export class ProfileController extends Controller { ) .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) + .orderBy("posType.posTypeRank", "ASC") + .addOrderBy("posLevel.posLevelRank", "ASC") .getManyAndCount(); const data = profiles.map((_data) => ({