diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index fc02ce11..362be804 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -19,23 +19,16 @@ import { import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; -import { PosExecutive } from "../entities/PosExecutive"; -import { PosType } from "../entities/PosType"; -import { PosLevel } from "../entities/PosLevel"; -import { CreatePosDict, PosDict } from "../entities/PosDict"; import HttpError from "../interfaces/http-error"; -import { Equal, ILike, In, IsNull, Like, Not } from "typeorm"; -import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; +import { In } from "typeorm"; 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"; -import { Position } from "../entities/Position"; -import { Brackets } from "typeorm/browser"; @Route("api/v1/org/report") -// @Tags("Position") +@Tags("Report") // @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, @@ -43,13 +36,6 @@ import { Brackets } from "typeorm/browser"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class ReportController extends Controller { - private posExecutiveRepository = AppDataSource.getRepository(PosExecutive); - private posTypeRepository = AppDataSource.getRepository(PosType); - private posLevelRepository = AppDataSource.getRepository(PosLevel); - private posDictRepository = AppDataSource.getRepository(PosDict); - private posMasterRepository = AppDataSource.getRepository(PosMaster); - private positionRepository = AppDataSource.getRepository(Position); - private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child1Repository = AppDataSource.getRepository(OrgChild1); @@ -86,6 +72,11 @@ export class ReportController extends Controller { "posMasters.next_holder.posType", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", + "posMasters.next_holder.current_holders.orgRoot", + "posMasters.next_holder.current_holders.orgChild1", + "posMasters.next_holder.current_holders.orgChild2", + "posMasters.next_holder.current_holders.orgChild3", + "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", @@ -94,87 +85,105 @@ export class ReportController extends Controller { }); const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; - const orgChild1Data = - orgRootIds && orgRootIds.length > 0 - ? await AppDataSource.getRepository(OrgChild1) - .createQueryBuilder("orgChild1") - .where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds }) - .select([ - "orgChild1.id", - "orgChild1.orgChild1Name", - "orgChild1.orgChild1ShortName", - "orgChild1.orgRootId", - "orgChild1.orgChild1Order", - ]) - .orderBy("orgChild1.orgChild1Order", "ASC") - .getMany() - : []; + const orgChild1Data = await this.child1Repository.find({ + where: { + orgRevisionId: In(orgRootIds), + }, + order: { orgChild1Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.next_holder", + "posMasters.next_holder.posLevel", + "posMasters.next_holder.posType", + "posMasters.next_holder.current_holders", + "posMasters.next_holder.current_holders.positions", + "posMasters.next_holder.current_holders.orgRoot", + "posMasters.next_holder.current_holders.orgChild1", + "posMasters.next_holder.current_holders.orgChild2", + "posMasters.next_holder.current_holders.orgChild3", + "posMasters.next_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 = - orgChild1Ids && orgChild1Ids.length > 0 - ? await AppDataSource.getRepository(OrgChild2) - .createQueryBuilder("orgChild2") - .where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids }) - .select([ - "orgChild2.id", - "orgChild2.orgChild2Name", - "orgChild2.orgChild2ShortName", - "orgChild2.orgChild1Id", - "orgChild2.orgChild2Order", - ]) - .orderBy("orgChild2.orgChild2Order", "ASC") - .getMany() - : []; + const orgChild2Data = await this.child2Repository.find({ + where: { + orgRevisionId: In(orgChild1Ids), + }, + order: { orgChild2Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.next_holder", + "posMasters.next_holder.posLevel", + "posMasters.next_holder.posType", + "posMasters.next_holder.current_holders", + "posMasters.next_holder.current_holders.positions", + "posMasters.next_holder.current_holders.orgRoot", + "posMasters.next_holder.current_holders.orgChild1", + "posMasters.next_holder.current_holders.orgChild2", + "posMasters.next_holder.current_holders.orgChild3", + "posMasters.next_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 = - orgChild2Ids && orgChild2Ids.length > 0 - ? await AppDataSource.getRepository(OrgChild3) - .createQueryBuilder("orgChild3") - .where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids }) - .select([ - "orgChild3.id", - "orgChild3.orgChild3Name", - "orgChild3.orgChild3ShortName", - "orgChild3.orgChild2Id", - "orgChild3.orgChild3Order", - ]) - .orderBy("orgChild3.orgChild3Order", "ASC") - .getMany() - : []; + const orgChild3Data = await this.child3Repository.find({ + where: { + orgRevisionId: In(orgChild2Ids), + }, + order: { orgChild3Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.next_holder", + "posMasters.next_holder.posLevel", + "posMasters.next_holder.posType", + "posMasters.next_holder.current_holders", + "posMasters.next_holder.current_holders.positions", + "posMasters.next_holder.current_holders.orgRoot", + "posMasters.next_holder.current_holders.orgChild1", + "posMasters.next_holder.current_holders.orgChild2", + "posMasters.next_holder.current_holders.orgChild3", + "posMasters.next_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 = - orgChild3Ids && orgChild3Ids.length > 0 - ? await AppDataSource.getRepository(OrgChild4) - .createQueryBuilder("orgChild4") - .where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids }) - .select([ - "orgChild4.id", - "orgChild4.orgChild4Name", - "orgChild4.orgChild4ShortName", - "orgChild4.orgChild3Id", - "orgChild4.orgChild4Order", - ]) - .orderBy("orgChild4.orgChild4Order", "ASC") - .getMany() - : []; - - const posMasters = await this.posMasterRepository.find({ + const orgChild4Data = await this.child4Repository.find({ where: { - orgRevisionId: orgRevision.id, + orgRevisionId: In(orgChild3Ids), }, - relations: ["next_holder", "current_holder"], - order: { posMasterOrder: "ASC" }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.next_holder", + "posMasters.next_holder.posLevel", + "posMasters.next_holder.posType", + "posMasters.next_holder.current_holders", + "posMasters.next_holder.current_holders.positions", + "posMasters.next_holder.current_holders.orgRoot", + "posMasters.next_holder.current_holders.orgChild1", + "posMasters.next_holder.current_holders.orgChild2", + "posMasters.next_holder.current_holders.orgChild3", + "posMasters.next_holder.current_holders.orgChild4", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], }); - const positions = await this.positionRepository.find({ - where: { - posMasterId: In(posMasters.map((posMaster: any) => posMaster.id)), - }, - relations: ["posLevel", "posType", "posExecutive"], - }); let orgRevisionActive: any = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, relations: [ @@ -200,27 +209,200 @@ export class ReportController extends Controller { if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0]; } - // const posMasterOlds = await this.posMasterRepository.find({ - // where: { - // orgRevisionId: orgRevisionActive.id, - // }, - // relations: ["next_holder", "current_holder"], - // }); - // const positionOlds = await this.positionRepository.find({ - // where: { - // posMasterId: In(posMasterOlds.map((posMaster: any) => posMaster.id)), - // }, - // relations: ["posLevel", "posType", "posExecutive"], - // }); - let data = new Array(); + let _node: any; for (let orgRoot of orgRootData) { - const node = { - orgRootName: orgRoot.orgRootName, - orgTreeName: orgRoot.orgRootName, - orgTreeShortName: orgRoot.orgRootShortName, - posMasters: await Promise.all( - orgRoot.posMasters + await Promise.all( + orgRoot.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.map((x) => x.posType.posTypeName))]; + const posLevel = [ + ...new Set(posMaster.positions.map((x) => x.posLevel.posLevelName)), + ]; + const posExecutive = [ + ...new Set(posMaster.positions.map((x) => x.posExecutive.posExecutiveName)), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_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) { + 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.map((x: any) => x.posType.posTypeName)), + ]; + profilePosLevel = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.posLevel.posLevelName)), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions.map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.positionName + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posType?.posTypeName + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posExecutive + ?.posExecutiveName + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find((x: any) => (x.isSit = true)) + ?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.next_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.next_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.next_holder == null + ? "- ว่าง -" + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMaster.next_holder == null + ? positionMasterOld == null + ? "" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.next_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.next_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find( + (x: any) => (x.positionIsSelected = true), + )?.posExecutive?.posExecutiveName, + }; + if (_node != null) { + if (node.orgTreeName == _node.orgTreeName) node.orgTreeName = ""; + if (node.orgTreeShortName == _node.orgTreeShortName) node.orgTreeShortName = ""; + if (node.profileOrgName == _node.profileOrgName) node.profileOrgName = ""; + if (node.profileOrgShortName == _node.profileOrgShortName) + node.profileOrgShortName = ""; + if (node.positionName == _node.positionName) node.positionName = ""; + if (node.posType == _node.posType) node.posType = ""; + if (node.posLevel == _node.posLevel) node.posLevel = ""; + if (node.posExecutive == _node.posExecutive) node.posExecutive = ""; + if (node.profilePositionName == _node.profilePositionName) + node.profilePositionName = ""; + if (node.profilePosType == _node.profilePosType) node.profilePosType = ""; + if (node.profilePosLevel == _node.profilePosLevel) node.profilePosLevel = ""; + if (node.profilePosExecutive == _node.profilePosExecutive) + node.profilePosExecutive = ""; + } + _node = node; + data.push(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) => { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; @@ -231,7 +413,6 @@ export class ReportController extends Controller { const posExecutive = [ ...new Set(posMaster.positions.map((x) => x.posExecutive.posExecutiveName)), ]; - const positionField = [...new Set(posMaster.positions.map((x) => x.positionField))]; let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( @@ -244,7 +425,6 @@ export class ReportController extends Controller { let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; - let profilePositionField: any = []; if (posMaster.ancestorDNA != null) { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => @@ -267,53 +447,81 @@ export class ReportController extends Controller { positionMasterOld.positions.map((x: any) => x.posExecutive.posExecutiveName), ), ]; - profilePositionField = [ - ...new Set(positionMasterOld.positions.map((x: any) => x.positionField)), - ]; } - return { - posMasterOrder: posMaster.posMasterOrder, - posMasterNoPrefix: posMaster.posMasterNoPrefix, + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, posMasterNo: posMaster.posMasterNo, - posMasterNoSuffix: posMaster.posMasterNoSuffix, - // positionName: - // posMaster.next_holder == null - // ? positionName.join(" หรือ ") - // : posMaster.positionIsSelected == false - // ? posMaster.positions.find((x: any) => (x.positionIsSelected = true)) - // ?.positionName - // : posMaster.next_holder.position, - // posType: - // posMaster.next_holder == null - // ? posType.join(" หรือ ") - // : posMaster.positionIsSelected == false - // ? posMaster.positions.find((x: any) => (x.positionIsSelected = true)) - // ?.posType?.posTypeName - // : posMaster.next_holder.posType == null - // ? "-" - // : posMaster.next_holder.posType.posTypeName, - // posLevel: - // posMaster.next_holder == null - // ? posLevel.join(" หรือ ") - // : posMaster.positionIsSelected == false - // ? posMaster.positions.find((x: any) => (x.positionIsSelected = true)) - // ?.posLevel?.posLevelName - // : posMaster.next_holder.posLevel == null - // ? "-" - // : posMaster.next_holder.posLevel.posLevelName, - // posExecutive: - // posMaster.next_holder == null - // ? posExecutive.join(" หรือ ") - // : posMaster.positionIsSelected == false - // ? posMaster.positions.find((x: any) => (x.positionIsSelected = true)) - // ?.posExecutive?.posExecutiveName - // : positionMasterProfileOld == null - // ? "-" - // : positionMasterProfileOld.positions.find( - // (x: any) => (x.positionIsSelected = true), - // )?.posExecutive?.posExecutiveName, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.positionName + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posType + ?.posTypeName + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posExecutive + ?.posExecutiveName + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find((x: any) => (x.isSit = true)) + ?.posExecutive?.posExecutiveName, + profileOrgName: + posMaster.next_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.next_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.next_holder == null ? "- ว่าง -" @@ -321,10 +529,10 @@ export class ReportController extends Controller { profilePosMasterNo: posMaster.next_holder == null ? positionMasterOld == null - ? "-" + ? "" : positionMasterOld.posMasterNo : positionMasterProfileOld == null - ? "-" + ? "" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.next_holder == null @@ -338,7 +546,7 @@ export class ReportController extends Controller { ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.next_holder.posType == null - ? "-" + ? "" : posMaster.next_holder.posType.posTypeName, profilePosLevel: posMaster.next_holder == null @@ -346,7 +554,7 @@ export class ReportController extends Controller { ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.next_holder.posLevel == null - ? "-" + ? "" : posMaster.next_holder.posLevel.posLevelName, profilePosExecutive: posMaster.next_holder == null @@ -354,35 +562,642 @@ export class ReportController extends Controller { ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null - ? "-" + ? "" : positionMasterProfileOld.positions.find( (x: any) => (x.positionIsSelected = true), )?.posExecutive?.posExecutiveName, - - // positions: await Promise.all( - // positions - // .filter((position) => position.posMasterId === posMaster.id) - // .map(async (position) => { - // return { - // positionName: position.positionName, - // positionField: position.positionField, - // posType: position.posType == null ? null : position.posType.posTypeName, - // posLevel: - // position.posLevel == null ? null : position.posLevel.posLevelName, - // posExecutive: - // position.posExecutive == null - // ? null - // : position.posExecutive.posExecutiveName, - // }; - // }), - // ), }; + if (_node != null) { + if (node.orgTreeName == _node.orgTreeName) node.orgTreeName = ""; + if (node.orgTreeShortName == _node.orgTreeShortName) node.orgTreeShortName = ""; + if (node.profileOrgName == _node.profileOrgName) node.profileOrgName = ""; + if (node.profileOrgShortName == _node.profileOrgShortName) + node.profileOrgShortName = ""; + if (node.positionName == _node.positionName) node.positionName = ""; + if (node.posType == _node.posType) node.posType = ""; + if (node.posLevel == _node.posLevel) node.posLevel = ""; + if (node.posExecutive == _node.posExecutive) node.posExecutive = ""; + if (node.profilePositionName == _node.profilePositionName) + node.profilePositionName = ""; + if (node.profilePosType == _node.profilePosType) node.profilePosType = ""; + if (node.profilePosLevel == _node.profilePosLevel) node.profilePosLevel = ""; + if (node.profilePosExecutive == _node.profilePosExecutive) + node.profilePosExecutive = ""; + } + _node = node; + data.push(node); }), - ), - }; - data.push(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) => { + const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const posType = [ + ...new Set(posMaster.positions.map((x) => x.posType.posTypeName)), + ]; + const posLevel = [ + ...new Set(posMaster.positions.map((x) => x.posLevel.posLevelName)), + ]; + const posExecutive = [ + ...new Set(posMaster.positions.map((x) => x.posExecutive.posExecutiveName)), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_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) { + 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.map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions.map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions.map( + (x: any) => x.posExecutive.posExecutiveName, + ), + ), + ]; + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.positionName + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posType + ?.posTypeName + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posExecutive + ?.posExecutiveName + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find((x: any) => (x.isSit = true)) + ?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.next_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.next_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.next_holder == null + ? "- ว่าง -" + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMaster.next_holder == null + ? positionMasterOld == null + ? "" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.next_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.next_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find( + (x: any) => (x.positionIsSelected = true), + )?.posExecutive?.posExecutiveName, + }; + if (_node != null) { + if (node.orgTreeName == _node.orgTreeName) node.orgTreeName = ""; + if (node.orgTreeShortName == _node.orgTreeShortName) node.orgTreeShortName = ""; + if (node.profileOrgName == _node.profileOrgName) node.profileOrgName = ""; + if (node.profileOrgShortName == _node.profileOrgShortName) + node.profileOrgShortName = ""; + if (node.positionName == _node.positionName) node.positionName = ""; + if (node.posType == _node.posType) node.posType = ""; + if (node.posLevel == _node.posLevel) node.posLevel = ""; + if (node.posExecutive == _node.posExecutive) node.posExecutive = ""; + if (node.profilePositionName == _node.profilePositionName) + node.profilePositionName = ""; + if (node.profilePosType == _node.profilePosType) node.profilePosType = ""; + if (node.profilePosLevel == _node.profilePosLevel) node.profilePosLevel = ""; + if (node.profilePosExecutive == _node.profilePosExecutive) + node.profilePosExecutive = ""; + } + _node = node; + data.push(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) => { + const positionName = [ + ...new Set(posMaster.positions.map((x) => x.positionName)), + ]; + const posType = [ + ...new Set(posMaster.positions.map((x) => x.posType.posTypeName)), + ]; + const posLevel = [ + ...new Set(posMaster.positions.map((x) => x.posLevel.posLevelName)), + ]; + const posExecutive = [ + ...new Set(posMaster.positions.map((x) => x.posExecutive.posExecutiveName)), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_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) { + 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.map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions.map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions.map( + (x: any) => x.posExecutive.posExecutiveName, + ), + ), + ]; + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.positionName + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posType + ?.posTypeName + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posExecutive + ?.posExecutiveName + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find( + (x: any) => (x.isSit = true), + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.next_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.next_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.next_holder == null + ? "- ว่าง -" + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMaster.next_holder == null + ? positionMasterOld == null + ? "" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.next_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.next_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find( + (x: any) => (x.positionIsSelected = true), + )?.posExecutive?.posExecutiveName, + }; + if (_node != null) { + if (node.orgTreeName == _node.orgTreeName) node.orgTreeName = ""; + if (node.orgTreeShortName == _node.orgTreeShortName) + node.orgTreeShortName = ""; + if (node.profileOrgName == _node.profileOrgName) node.profileOrgName = ""; + if (node.profileOrgShortName == _node.profileOrgShortName) + node.profileOrgShortName = ""; + if (node.positionName == _node.positionName) node.positionName = ""; + if (node.posType == _node.posType) node.posType = ""; + if (node.posLevel == _node.posLevel) node.posLevel = ""; + if (node.posExecutive == _node.posExecutive) node.posExecutive = ""; + if (node.profilePositionName == _node.profilePositionName) + node.profilePositionName = ""; + if (node.profilePosType == _node.profilePosType) node.profilePosType = ""; + if (node.profilePosLevel == _node.profilePosLevel) node.profilePosLevel = ""; + if (node.profilePosExecutive == _node.profilePosExecutive) + node.profilePosExecutive = ""; + } + _node = node; + data.push(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.map((x) => x.posType.posTypeName)), + ]; + const posLevel = [ + ...new Set(posMaster.positions.map((x) => x.posLevel.posLevelName)), + ]; + const posExecutive = [ + ...new Set(posMaster.positions.map((x) => x.posExecutive.posExecutiveName)), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_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) { + 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.map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions.map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions.map( + (x: any) => x.posExecutive.posExecutiveName, + ), + ), + ]; + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.positionName + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posType + ?.posTypeName + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => (x.isSit = true))?.posExecutive + ?.posExecutiveName + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find( + (x: any) => (x.isSit = true), + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.next_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.next_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.next_holder == null + ? "- ว่าง -" + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMaster.next_holder == null + ? positionMasterOld == null + ? "" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.next_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "" + : posMaster.next_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.next_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "" + : positionMasterProfileOld.positions.find( + (x: any) => (x.positionIsSelected = true), + )?.posExecutive?.posExecutiveName, + }; + if (_node != null) { + if (node.orgTreeName == _node.orgTreeName) node.orgTreeName = ""; + if (node.orgTreeShortName == _node.orgTreeShortName) + node.orgTreeShortName = ""; + if (node.profileOrgName == _node.profileOrgName) node.profileOrgName = ""; + if (node.profileOrgShortName == _node.profileOrgShortName) + node.profileOrgShortName = ""; + if (node.positionName == _node.positionName) node.positionName = ""; + if (node.posType == _node.posType) node.posType = ""; + if (node.posLevel == _node.posLevel) node.posLevel = ""; + if (node.posExecutive == _node.posExecutive) node.posExecutive = ""; + if (node.profilePositionName == _node.profilePositionName) + node.profilePositionName = ""; + if (node.profilePosType == _node.profilePosType) node.profilePosType = ""; + if (node.profilePosLevel == _node.profilePosLevel) + node.profilePosLevel = ""; + if (node.profilePosExecutive == _node.profilePosExecutive) + node.profilePosExecutive = ""; + } + _node = node; + data.push(node); + }), + ); + _node = null; + } + } + } + } } - return new HttpSuccess(data); + return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } catch (error) { return error; }