From 632dc300dad5b94189c76a58595beb73eb00eb53 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Mon, 12 Feb 2024 14:43:38 +0700 Subject: [PATCH] report1 report3 --- src/controllers/ReportController.ts | 1320 +++++++++++++++++++++++++++ 1 file changed, 1320 insertions(+) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 933c67f9..3ecd4523 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -43,6 +43,584 @@ export class ReportController extends Controller { private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); + /** + * API Report1 + * + * @summary Report1 + * + */ + @Get("report1") + async findReport1() { + try { + const orgRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + relations: ["orgRoots"], + }); + if (!orgRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const orgRootData = await this.orgRootRepository.find({ + where: { + orgRevisionId: orgRevision.id, + }, + order: { orgRootOrder: "ASC" }, + relations: [ + "posMasters", + "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.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.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.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.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + + 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.orgChild1 == 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 node = { + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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 node = { + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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.orgChild3 == 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 node = { + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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.orgChild3 == 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 node = { + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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 node = { + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(head); + } + no += 1; + _node = node; + }), + ); + _node = null; + } + } + } + } + } + return new HttpSuccess({ template: "report1", reportName: "report1", data: { data } }); + } catch (error) { + return error; + } + } + /** * API Report2 * @@ -1661,4 +2239,746 @@ export class ReportController extends Controller { return error; } } + + /** + * API Report3 + * + * @summary Report3 + * + */ + @Get("report3") + async findReport3() { + try { + const orgRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + relations: ["orgRoots"], + }); + if (!orgRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const orgRootData = await this.orgRootRepository.find({ + where: { + orgRevisionId: orgRevision.id, + }, + order: { orgRootOrder: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "posMasters.next_holder", + "posMasters.next_holder", + "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 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.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 = await this.child2Repository.find({ + where: { + orgChild1: In(orgChild1Ids), + }, + order: { orgChild2Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "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 = await this.child3Repository.find({ + where: { + orgChild2: In(orgChild2Ids), + }, + order: { orgChild3Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "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 = await this.child4Repository.find({ + where: { + orgChild3: In(orgChild3Ids), + }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.orgRoot", + "posMasters.orgChild1", + "posMasters.orgChild2", + "posMasters.orgChild3", + "posMasters.orgChild4", + "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", + ], + }); + + let orgRevisionActive: any = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + if (orgRevisionActive == null) { + const _orgRevisionActive = await this.orgRevisionRepository.find({ + order: { orgRevisionCreatedAt: "DESC" }, + skip: 1, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + if (_orgRevisionActive.length > 0) 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.orgChild1 == null && posMaster.next_holder != null) { + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let node = { + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.next_holder.position, + posType: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType + ?.posTypeName + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + 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, + profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + profileFullname: "", + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: "", + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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 && posMaster.next_holder != null) { + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let node = { + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.next_holder.position, + posType: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + 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, + + profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + profileFullname: "", + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: "", + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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.orgChild3 == null && posMaster.next_holder != null) { + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let node = { + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.next_holder.position, + posType: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + 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, + profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + profileFullname: "", + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: "", + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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.orgChild3 == null && posMaster.next_holder != null) { + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let node = { + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.next_holder.position, + posType: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + 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, + profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + profileFullname: "", + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: "", + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + 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) => { + if (posMaster.next_holder != null) { + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let node = { + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.next_holder.position, + posType: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + 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, + profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + }; + if (_node == null) { + const head = { + posMasterNo: node.orgTreeShortName, + profileFullname: "", + posExecutive: node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + const _head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: "", + posExecutive: + node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + }; + data.push(head); + _node == null; + } + const head = { + no: no, + posMasterNo: node.posMasterNo, + profileFullname: node.profileFullname, + posExecutive: + node.posExecutive == _node.posExecutive ? "" : node.posExecutive, + positionName: node.positionName, + posType: node.posType, + posLevel: node.posLevel, + }; + data.push(head); + } + no += 1; + _node = node; + } + }), + ); + _node = null; + } + } + } + } + } + return new HttpSuccess({ template: "report3", reportName: "report3", data: { data } }); + } catch (error) { + return error; + } + } }