diff --git a/OPTIMIZED_FUNCTION.ts b/OPTIMIZED_FUNCTION.ts deleted file mode 100644 index e6ede546..00000000 --- a/OPTIMIZED_FUNCTION.ts +++ /dev/null @@ -1,349 +0,0 @@ -// This file contains the optimized detailSuperAdmin function -// Replace the function at line 1164 in OrganizationController.ts - - /** - * API รายละเอียดโครงสร้าง - * - * @summary ORG_023 - รายละเอียดโครงสร้าง (ADMIN) #25 - * @optimized ลด N+1 queries จาก 1,000,000+ queries → ~10 queries - */ - @Get("super-admin/{id}") - async detailSuperAdmin(@Path() id: string, @Request() request: RequestWithUser) { - const orgRevision = await this.orgRevisionRepository.findOne({ - where: { id: id }, - }); - if (!orgRevision) return new HttpSuccess([]); - - let rootId: any = null; - if (!request.user.role.includes("SUPER_ADMIN")) { - const profile = await this.profileRepo.findOne({ - where: { - keycloak: request.user.sub, - }, - }); - if (profile == null) return new HttpSuccess([]); - - if (!request.user.role.includes("SUPER_ADMIN")) { - const posMaster = await this.posMasterRepository.findOne({ - where: { - orgRevisionId: id, - current_holderId: profile.id, - }, - }); - if (!posMaster) return new HttpSuccess([]); - - rootId = posMaster.orgRootId; - } - } - - // OPTIMIZED: Get all position counts in ONE query - const { orgRootMap, orgChild1Map, orgChild2Map, orgChild3Map, orgChild4Map, rootPosMap } = - await getPositionCounts(id); - - const orgRootData = await AppDataSource.getRepository(OrgRoot) - .createQueryBuilder("orgRoot") - .where("orgRoot.orgRevisionId = :id", { id }) - .andWhere(rootId != null ? `orgRoot.id = :rootId` : "1=1", { - rootId: rootId, - }) - .orderBy("orgRoot.orgRootOrder", "ASC") - .getMany(); - - 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 }) - .orderBy("orgChild1.orgChild1Order", "ASC") - .getMany() - : []; - - 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 }) - .orderBy("orgChild2.orgChild2Order", "ASC") - .getMany() - : []; - - 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 }) - .orderBy("orgChild3.orgChild3Order", "ASC") - .getMany() - : []; - - 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 }) - .orderBy("orgChild4.orgChild4Order", "ASC") - .getMany() - : []; - - // OPTIMIZED: Build formatted data using pre-calculated counts (no nested queries!) - const formattedData = orgRootData.map((orgRoot) => { - const rootCounts = getCounts(orgRootMap, orgRoot.id); - const rootPosCounts = getRootCounts(rootPosMap, orgRoot.id); - - return { - orgTreeId: orgRoot.id, - orgLevel: 0, - orgName: orgRoot.orgRootName, - orgTreeName: orgRoot.orgRootName, - orgTreeShortName: orgRoot.orgRootShortName, - orgTreeCode: orgRoot.orgRootCode, - orgCode: orgRoot.orgRootCode + "00", - orgTreeRank: orgRoot.orgRootRank, - orgTreeRankSub: orgRoot.orgRootRankSub, - orgRootDnaId: orgRoot.ancestorDNA, - DEPARTMENT_CODE: orgRoot.DEPARTMENT_CODE, - DIVISION_CODE: orgRoot.DIVISION_CODE, - SECTION_CODE: orgRoot.SECTION_CODE, - JOB_CODE: orgRoot.JOB_CODE, - orgTreeOrder: orgRoot.orgRootOrder, - orgTreePhoneEx: orgRoot.orgRootPhoneEx, - orgTreePhoneIn: orgRoot.orgRootPhoneIn, - orgTreeFax: orgRoot.orgRootFax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - isDeputy: orgRoot.isDeputy, - isCommission: orgRoot.isCommission, - responsibility: orgRoot.responsibility, - labelName: - orgRoot.orgRootName + " " + orgRoot.orgRootCode + "00" + " " + orgRoot.orgRootShortName, - totalPosition: rootCounts.totalPosition, - totalPositionCurrentUse: rootCounts.totalPositionCurrentUse, - totalPositionCurrentVacant: rootCounts.totalPositionCurrentVacant, - totalPositionNextUse: rootCounts.totalPositionNextUse, - totalPositionNextVacant: rootCounts.totalPositionNextVacant, - totalRootPosition: rootPosCounts.totalRootPosition, - totalRootPositionCurrentUse: rootPosCounts.totalRootPositionCurrentUse, - totalRootPositionCurrentVacant: rootPosCounts.totalRootPositionCurrentVacant, - totalRootPositionNextUse: rootPosCounts.totalRootPositionNextUse, - totalRootPositionNextVacant: rootPosCounts.totalRootPositionNextVacant, - children: orgChild1Data - .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) - .map((orgChild1) => { - const child1Counts = getCounts(orgChild1Map, orgChild1.id); - const child1PosKey = `${orgRoot.id}-${orgChild1.id}`; - const child1PosCounts = getRootCounts(rootPosMap, child1PosKey); - - return { - orgTreeId: orgChild1.id, - orgRootId: orgRoot.id, - orgLevel: 1, - orgName: `${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild1.orgChild1Name, - orgTreeShortName: orgChild1.orgChild1ShortName, - orgTreeCode: orgChild1.orgChild1Code, - orgCode: orgRoot.orgRootCode + orgChild1.orgChild1Code, - orgTreeRank: orgChild1.orgChild1Rank, - orgTreeRankSub: orgChild1.orgChild1RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - DEPARTMENT_CODE: orgChild1.DEPARTMENT_CODE, - DIVISION_CODE: orgChild1.DIVISION_CODE, - SECTION_CODE: orgChild1.SECTION_CODE, - JOB_CODE: orgChild1.JOB_CODE, - orgTreeOrder: orgChild1.orgChild1Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild1.orgChild1PhoneEx, - orgTreePhoneIn: orgChild1.orgChild1PhoneIn, - orgTreeFax: orgChild1.orgChild1Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild1.responsibility, - isOfficer: orgChild1.isOfficer, - isInformation: orgChild1.isInformation, - labelName: - orgChild1.orgChild1Name + - " " + - orgRoot.orgRootCode + - orgChild1.orgChild1Code + - " " + - orgChild1.orgChild1ShortName, - totalPosition: child1Counts.totalPosition, - totalPositionCurrentUse: child1Counts.totalPositionCurrentUse, - totalPositionCurrentVacant: child1Counts.totalPositionCurrentVacant, - totalPositionNextUse: child1Counts.totalPositionNextUse, - totalPositionNextVacant: child1Counts.totalPositionNextVacant, - totalRootPosition: child1PosCounts.totalRootPosition, - totalRootPositionCurrentUse: child1PosCounts.totalRootPositionCurrentUse, - totalRootPositionCurrentVacant: child1PosCounts.totalRootPositionCurrentVacant, - totalRootPositionNextUse: child1PosCounts.totalRootPositionNextUse, - totalRootPositionNextVacant: child1PosCounts.totalRootPositionNextVacant, - children: orgChild2Data - .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) - .map((orgChild2) => { - const child2Counts = getCounts(orgChild2Map, orgChild2.id); - const child2PosKey = `${orgRoot.id}-${orgChild1.id}-${orgChild2.id}`; - const child2PosCounts = getRootCounts(rootPosMap, child2PosKey); - - return { - orgTreeId: orgChild2.id, - orgRootId: orgChild1.id, - orgLevel: 2, - orgName: `${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild2.orgChild2Name, - orgTreeShortName: orgChild2.orgChild2ShortName, - orgTreeCode: orgChild2.orgChild2Code, - orgCode: orgRoot.orgRootCode + orgChild2.orgChild2Code, - orgTreeRank: orgChild2.orgChild2Rank, - orgTreeRankSub: orgChild2.orgChild2RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - orgChild2DnaId: orgChild2.ancestorDNA, - DEPARTMENT_CODE: orgChild2.DEPARTMENT_CODE, - DIVISION_CODE: orgChild2.DIVISION_CODE, - SECTION_CODE: orgChild2.SECTION_CODE, - JOB_CODE: orgChild2.JOB_CODE, - orgTreeOrder: orgChild2.orgChild2Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild2.orgChild2PhoneEx, - orgTreePhoneIn: orgChild2.orgChild2PhoneIn, - orgTreeFax: orgChild2.orgChild2Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild2.responsibility, - labelName: - orgChild2.orgChild2Name + - " " + - orgRoot.orgRootCode + - orgChild2.orgChild2Code + - " " + - orgChild2.orgChild2ShortName, - totalPosition: child2Counts.totalPosition, - totalPositionCurrentUse: child2Counts.totalPositionCurrentUse, - totalPositionCurrentVacant: child2Counts.totalPositionCurrentVacant, - totalPositionNextUse: child2Counts.totalPositionNextUse, - totalPositionNextVacant: child2Counts.totalPositionNextVacant, - totalRootPosition: child2PosCounts.totalRootPosition, - totalRootPositionCurrentUse: child2PosCounts.totalRootPositionCurrentUse, - totalRootPositionCurrentVacant: child2PosCounts.totalRootPositionCurrentVacant, - totalRootPositionNextUse: child2PosCounts.totalRootPositionNextUse, - totalRootPositionNextVacant: child2PosCounts.totalRootPositionNextVacant, - children: orgChild3Data - .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) - .map((orgChild3) => { - const child3Counts = getCounts(orgChild3Map, orgChild3.id); - const child3PosKey = `${orgRoot.id}-${orgChild1.id}-${orgChild2.id}-${orgChild3.id}`; - const child3PosCounts = getRootCounts(rootPosMap, child3PosKey); - - return { - orgTreeId: orgChild3.id, - orgRootId: orgChild2.id, - orgLevel: 3, - orgName: `${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild3.orgChild3Name, - orgTreeShortName: orgChild3.orgChild3ShortName, - orgTreeCode: orgChild3.orgChild3Code, - orgCode: orgRoot.orgRootCode + orgChild3.orgChild3Code, - orgTreeRank: orgChild3.orgChild3Rank, - orgTreeRankSub: orgChild3.orgChild3RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - orgChild2DnaId: orgChild2.ancestorDNA, - orgChild3DnaId: orgChild3.ancestorDNA, - DEPARTMENT_CODE: orgChild3.DEPARTMENT_CODE, - DIVISION_CODE: orgChild3.DIVISION_CODE, - SECTION_CODE: orgChild3.SECTION_CODE, - JOB_CODE: orgChild3.JOB_CODE, - orgTreeOrder: orgChild3.orgChild3Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild3.orgChild3PhoneEx, - orgTreePhoneIn: orgChild3.orgChild3PhoneIn, - orgTreeFax: orgChild3.orgChild3Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild3.responsibility, - labelName: - orgChild3.orgChild3Name + - " " + - orgRoot.orgRootCode + - orgChild3.orgChild3Code + - " " + - orgChild3.orgChild3ShortName, - totalPosition: child3Counts.totalPosition, - totalPositionCurrentUse: child3Counts.totalPositionCurrentUse, - totalPositionCurrentVacant: child3Counts.totalPositionCurrentVacant, - totalPositionNextUse: child3Counts.totalPositionNextUse, - totalPositionNextVacant: child3Counts.totalPositionNextVacant, - totalRootPosition: child3PosCounts.totalRootPosition, - totalRootPositionCurrentUse: child3PosCounts.totalRootPositionCurrentUse, - totalRootPositionCurrentVacant: child3PosCounts.totalRootPositionCurrentVacant, - totalRootPositionNextUse: child3PosCounts.totalRootPositionNextUse, - totalRootPositionNextVacant: child3PosCounts.totalRootPositionNextVacant, - children: orgChild4Data - .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) - .map((orgChild4) => { - const child4Counts = getCounts(orgChild4Map, orgChild4.id); - const child4PosKey = `${orgRoot.id}-${orgChild1.id}-${orgChild2.id}-${orgChild3.id}-${orgChild4.id}`; - const child4PosCounts = getRootCounts(rootPosMap, child4PosKey); - - return { - orgTreeId: orgChild4.id, - orgRootId: orgChild3.id, - orgLevel: 4, - orgName: `${orgChild4.orgChild4Name}/${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild4.orgChild4Name, - orgTreeShortName: orgChild4.orgChild4ShortName, - orgTreeCode: orgChild4.orgChild4Code, - orgCode: orgRoot.orgRootCode + orgChild4.orgChild4Code, - orgTreeRank: orgChild4.orgChild4Rank, - orgTreeRankSub: orgChild4.orgChild4RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - orgChild2DnaId: orgChild2.ancestorDNA, - orgChild3DnaId: orgChild3.ancestorDNA, - orgChild4DnaId: orgChild4.ancestorDNA, - DEPARTMENT_CODE: orgChild4.DEPARTMENT_CODE, - DIVISION_CODE: orgChild4.DIVISION_CODE, - SECTION_CODE: orgChild4.SECTION_CODE, - JOB_CODE: orgChild4.JOB_CODE, - orgTreeOrder: orgChild4.orgChild4Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild4.orgChild4PhoneEx, - orgTreePhoneIn: orgChild4.orgChild4PhoneIn, - orgTreeFax: orgChild4.orgChild4Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild4.responsibility, - labelName: - orgChild4.orgChild4Name + - " " + - orgRoot.orgRootCode + - orgChild4.orgChild4Code + - " " + - orgChild4.orgChild4ShortName, - totalPosition: child4Counts.totalPosition, - totalPositionCurrentUse: child4Counts.totalPositionCurrentUse, - totalPositionCurrentVacant: child4Counts.totalPositionCurrentVacant, - totalPositionNextUse: child4Counts.totalPositionNextUse, - totalPositionNextVacant: child4Counts.totalPositionNextVacant, - totalRootPosition: child4PosCounts.totalRootPosition, - totalRootPositionCurrentUse: child4PosCounts.totalRootPositionCurrentUse, - totalRootPositionCurrentVacant: child4PosCounts.totalRootPositionCurrentVacant, - totalRootPositionNextUse: child4PosCounts.totalRootPositionNextUse, - totalRootPositionNextVacant: child4PosCounts.totalRootPositionNextVacant, - children: [], - }; - }), - }; - }), - }; - }), - }); - }, - ); - - return new HttpSuccess(formattedData); - } diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 9730e280..db33f1ad 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -46,7 +46,7 @@ import { getRoles, addUserRoles, } from "../keycloak"; -import { getPositionCounts, getCounts, getRootCounts } from "./OrganizationController-optimized"; +// import { getPositionCounts, getCounts, getRootCounts } from "../services/OrganizationService"; import { CreatePosMasterHistoryEmployee, CreatePosMasterHistoryOfficer, @@ -1166,7 +1166,6 @@ export class OrganizationController extends Controller { async detailSuperAdmin(@Path() id: string, @Request() request: RequestWithUser) { const orgRevision = await this.orgRevisionRepository.findOne({ where: { id: id }, - relations: ["posMasters"], }); if (!orgRevision) return new HttpSuccess([]); @@ -1176,80 +1175,46 @@ export class OrganizationController extends Controller { where: { keycloak: request.user.sub, }, + select: ["id"], }); if (profile == null) return new HttpSuccess([]); - if (!request.user.role.includes("SUPER_ADMIN")) { - if (orgRevision.orgRevisionIsDraft == true && orgRevision.orgRevisionIsCurrent == false) { - rootId = - orgRevision?.posMasters?.filter((x) => x.next_holderId == profile.id)[0]?.orgRootId || - null; - if (!rootId) return new HttpSuccess([]); - } else { - rootId = - orgRevision?.posMasters?.filter((x) => x.current_holderId == profile.id)[0] - ?.orgRootId || null; - if (!rootId) return new HttpSuccess([]); - } - } + const posMaster = await this.posMasterRepository.findOne({ + where: + orgRevision.orgRevisionIsCurrent && !orgRevision.orgRevisionIsDraft + ? { + orgRevisionId: id, + current_holderId: profile.id, + } + : { + orgRevisionId: id, + next_holderId: profile.id, + }, + }); + if (!posMaster) return new HttpSuccess([]); + + rootId = posMaster.orgRootId; } + // OPTIMIZED: Get all position counts in ONE query (closed) + // const { orgRootMap, orgChild1Map, orgChild2Map, orgChild3Map, orgChild4Map, rootPosMap } = + // await getPositionCounts(id); + const orgRootData = await AppDataSource.getRepository(OrgRoot) .createQueryBuilder("orgRoot") .where("orgRoot.orgRevisionId = :id", { id }) .andWhere(rootId != null ? `orgRoot.id = :rootId` : "1=1", { rootId: rootId, }) - .select([ - "orgRoot.id", - "orgRoot.isDeputy", - "orgRoot.isCommission", - "orgRoot.orgRootName", - "orgRoot.orgRootShortName", - "orgRoot.orgRootCode", - "orgRoot.orgRootOrder", - "orgRoot.orgRootPhoneEx", - "orgRoot.orgRootPhoneIn", - "orgRoot.orgRootFax", - "orgRoot.orgRevisionId", - "orgRoot.orgRootRank", - "orgRoot.orgRootRankSub", - "orgRoot.DEPARTMENT_CODE", - "orgRoot.DIVISION_CODE", - "orgRoot.SECTION_CODE", - "orgRoot.JOB_CODE", - "orgRoot.responsibility", - "orgRoot.ancestorDNA", - ]) .orderBy("orgRoot.orgRootOrder", "ASC") .getMany(); + 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.isOfficer", - "orgChild1.isInformation", - "orgChild1.orgChild1Name", - "orgChild1.orgChild1ShortName", - "orgChild1.orgChild1Code", - "orgChild1.orgChild1Order", - "orgChild1.orgChild1PhoneEx", - "orgChild1.orgChild1PhoneIn", - "orgChild1.orgChild1Fax", - "orgChild1.orgRootId", - "orgChild1.orgChild1Rank", - "orgChild1.orgChild1RankSub", - "orgChild1.DEPARTMENT_CODE", - "orgChild1.DIVISION_CODE", - "orgChild1.SECTION_CODE", - "orgChild1.JOB_CODE", - "orgChild1.responsibility", - "orgChild1.ancestorDNA", - ]) .orderBy("orgChild1.orgChild1Order", "ASC") .getMany() : []; @@ -1260,26 +1225,6 @@ export class OrganizationController extends Controller { ? await AppDataSource.getRepository(OrgChild2) .createQueryBuilder("orgChild2") .where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids }) - .select([ - "orgChild2.id", - "orgChild2.orgChild2Name", - "orgChild2.orgChild2ShortName", - "orgChild2.orgChild2Code", - "orgChild2.orgChild2Order", - "orgChild2.orgChild2PhoneEx", - "orgChild2.orgChild2PhoneIn", - "orgChild2.orgChild2Fax", - "orgChild2.orgRootId", - "orgChild2.orgChild2Rank", - "orgChild2.orgChild2RankSub", - "orgChild2.DEPARTMENT_CODE", - "orgChild2.DIVISION_CODE", - "orgChild2.SECTION_CODE", - "orgChild2.JOB_CODE", - "orgChild2.orgChild1Id", - "orgChild2.responsibility", - "orgChild2.ancestorDNA", - ]) .orderBy("orgChild2.orgChild2Order", "ASC") .getMany() : []; @@ -1290,26 +1235,6 @@ export class OrganizationController extends Controller { ? await AppDataSource.getRepository(OrgChild3) .createQueryBuilder("orgChild3") .where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids }) - .select([ - "orgChild3.id", - "orgChild3.orgChild3Name", - "orgChild3.orgChild3ShortName", - "orgChild3.orgChild3Code", - "orgChild3.orgChild3Order", - "orgChild3.orgChild3PhoneEx", - "orgChild3.orgChild3PhoneIn", - "orgChild3.orgChild3Fax", - "orgChild3.orgRootId", - "orgChild3.orgChild3Rank", - "orgChild3.orgChild3RankSub", - "orgChild3.DEPARTMENT_CODE", - "orgChild3.DIVISION_CODE", - "orgChild3.SECTION_CODE", - "orgChild3.JOB_CODE", - "orgChild3.orgChild2Id", - "orgChild3.responsibility", - "orgChild3.ancestorDNA", - ]) .orderBy("orgChild3.orgChild3Order", "ASC") .getMany() : []; @@ -1320,661 +1245,270 @@ export class OrganizationController extends Controller { ? await AppDataSource.getRepository(OrgChild4) .createQueryBuilder("orgChild4") .where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids }) - .select([ - "orgChild4.id", - "orgChild4.orgChild4Name", - "orgChild4.orgChild4ShortName", - "orgChild4.orgChild4Code", - "orgChild4.orgChild4Order", - "orgChild4.orgChild4PhoneEx", - "orgChild4.orgChild4PhoneIn", - "orgChild4.orgChild4Fax", - "orgChild4.orgRootId", - "orgChild4.orgChild4Rank", - "orgChild4.orgChild4RankSub", - "orgChild4.DEPARTMENT_CODE", - "orgChild4.DIVISION_CODE", - "orgChild4.SECTION_CODE", - "orgChild4.JOB_CODE", - "orgChild4.orgChild3Id", - "orgChild4.responsibility", - "orgChild4.ancestorDNA", - ]) .orderBy("orgChild4.orgChild4Order", "ASC") .getMany() : []; - // const formattedData = orgRootData.map((orgRoot) => { - const formattedData = await Promise.all( - orgRootData.map(async (orgRoot) => { - return { - orgTreeId: orgRoot.id, - orgLevel: 0, - orgName: orgRoot.orgRootName, - orgTreeName: orgRoot.orgRootName, - orgTreeShortName: orgRoot.orgRootShortName, - orgTreeCode: orgRoot.orgRootCode, - orgCode: orgRoot.orgRootCode + "00", - orgTreeRank: orgRoot.orgRootRank, - orgTreeRankSub: orgRoot.orgRootRankSub, - orgRootDnaId: orgRoot.ancestorDNA, - DEPARTMENT_CODE: orgRoot.DEPARTMENT_CODE, - DIVISION_CODE: orgRoot.DIVISION_CODE, - SECTION_CODE: orgRoot.SECTION_CODE, - JOB_CODE: orgRoot.JOB_CODE, - orgTreeOrder: orgRoot.orgRootOrder, - orgTreePhoneEx: orgRoot.orgRootPhoneEx, - orgTreePhoneIn: orgRoot.orgRootPhoneIn, - orgTreeFax: orgRoot.orgRootFax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - isDeputy: orgRoot.isDeputy, - isCommission: orgRoot.isCommission, - responsibility: orgRoot.responsibility, - labelName: - orgRoot.orgRootName + " " + orgRoot.orgRootCode + "00" + " " + orgRoot.orgRootShortName, - totalPosition: await this.posMasterRepository.count({ - where: { orgRevisionId: orgRoot.orgRevisionId, orgRootId: orgRoot.id }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), + // OPTIMIZED: Build formatted data using pre-calculated counts (no nested queries!) + const formattedData = orgRootData.map((orgRoot) => { + // const rootCounts = getCounts(orgRootMap, orgRoot.id); + // const rootPosCounts = getRootCounts(rootPosMap, orgRoot.id); - children: await Promise.all( - orgChild1Data - .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) - .map(async (orgChild1) => ({ - orgTreeId: orgChild1.id, - orgRootId: orgRoot.id, - orgLevel: 1, - orgName: `${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild1.orgChild1Name, - orgTreeShortName: orgChild1.orgChild1ShortName, - orgTreeCode: orgChild1.orgChild1Code, - orgCode: orgRoot.orgRootCode + orgChild1.orgChild1Code, - orgTreeRank: orgChild1.orgChild1Rank, - orgTreeRankSub: orgChild1.orgChild1RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - DEPARTMENT_CODE: orgChild1.DEPARTMENT_CODE, - DIVISION_CODE: orgChild1.DIVISION_CODE, - SECTION_CODE: orgChild1.SECTION_CODE, - JOB_CODE: orgChild1.JOB_CODE, - orgTreeOrder: orgChild1.orgChild1Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild1.orgChild1PhoneEx, - orgTreePhoneIn: orgChild1.orgChild1PhoneIn, - orgTreeFax: orgChild1.orgChild1Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild1.responsibility, - isOfficer: orgChild1.isOfficer, - isInformation: orgChild1.isInformation, - labelName: - orgChild1.orgChild1Name + - " " + - orgRoot.orgRootCode + - orgChild1.orgChild1Code + - " " + - orgChild1.orgChild1ShortName, - totalPosition: await this.posMasterRepository.count({ - where: { orgRevisionId: orgRoot.orgRevisionId, orgChild1Id: orgChild1.id }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild1Id: orgChild1.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild1Id: orgChild1.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild1Id: orgChild1.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild1Id: orgChild1.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), + return { + orgTreeId: orgRoot.id, + orgLevel: 0, + orgName: orgRoot.orgRootName, + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + orgTreeCode: orgRoot.orgRootCode, + orgCode: orgRoot.orgRootCode + "00", + orgTreeRank: orgRoot.orgRootRank, + orgTreeRankSub: orgRoot.orgRootRankSub, + orgRootDnaId: orgRoot.ancestorDNA, + DEPARTMENT_CODE: orgRoot.DEPARTMENT_CODE, + DIVISION_CODE: orgRoot.DIVISION_CODE, + SECTION_CODE: orgRoot.SECTION_CODE, + JOB_CODE: orgRoot.JOB_CODE, + orgTreeOrder: orgRoot.orgRootOrder, + orgTreePhoneEx: orgRoot.orgRootPhoneEx, + orgTreePhoneIn: orgRoot.orgRootPhoneIn, + orgTreeFax: orgRoot.orgRootFax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + isDeputy: orgRoot.isDeputy, + isCommission: orgRoot.isCommission, + responsibility: orgRoot.responsibility, + labelName: + orgRoot.orgRootName + " " + orgRoot.orgRootCode + "00" + " " + orgRoot.orgRootShortName, + // totalPosition: rootCounts.totalPosition, + // totalPositionCurrentUse: rootCounts.totalPositionCurrentUse, + // totalPositionCurrentVacant: rootCounts.totalPositionCurrentVacant, + // totalPositionNextUse: rootCounts.totalPositionNextUse, + // totalPositionNextVacant: rootCounts.totalPositionNextVacant, + // totalRootPosition: rootPosCounts.totalRootPosition, + // totalRootPositionCurrentUse: rootPosCounts.totalRootPositionCurrentUse, + // totalRootPositionCurrentVacant: rootPosCounts.totalRootPositionCurrentVacant, + // totalRootPositionNextUse: rootPosCounts.totalRootPositionNextUse, + // totalRootPositionNextVacant: rootPosCounts.totalRootPositionNextVacant, + children: orgChild1Data + .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) + .map((orgChild1) => { + // const child1Counts = getCounts(orgChild1Map, orgChild1.id); + // const child1PosKey = `${orgRoot.id}-${orgChild1.id}`; + // const child1PosCounts = getRootCounts(rootPosMap, child1PosKey); - children: await Promise.all( - orgChild2Data - .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) - .map(async (orgChild2) => ({ - orgTreeId: orgChild2.id, - orgRootId: orgChild1.id, - orgLevel: 2, - orgName: `${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild2.orgChild2Name, - orgTreeShortName: orgChild2.orgChild2ShortName, - orgTreeCode: orgChild2.orgChild2Code, - orgCode: orgRoot.orgRootCode + orgChild2.orgChild2Code, - orgTreeRank: orgChild2.orgChild2Rank, - orgTreeRankSub: orgChild2.orgChild2RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - orgChild2DnaId: orgChild2.ancestorDNA, - DEPARTMENT_CODE: orgChild2.DEPARTMENT_CODE, - DIVISION_CODE: orgChild2.DIVISION_CODE, - SECTION_CODE: orgChild2.SECTION_CODE, - JOB_CODE: orgChild2.JOB_CODE, - orgTreeOrder: orgChild2.orgChild2Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild2.orgChild2PhoneEx, - orgTreePhoneIn: orgChild2.orgChild2PhoneIn, - orgTreeFax: orgChild2.orgChild2Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild2.responsibility, - labelName: - orgChild2.orgChild2Name + - " " + - orgRoot.orgRootCode + - orgChild2.orgChild2Code + - " " + - orgChild2.orgChild2ShortName, - totalPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), + return { + orgTreeId: orgChild1.id, + orgRootId: orgRoot.id, + orgLevel: 1, + orgName: `${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + orgTreeCode: orgChild1.orgChild1Code, + orgCode: orgRoot.orgRootCode + orgChild1.orgChild1Code, + orgTreeRank: orgChild1.orgChild1Rank, + orgTreeRankSub: orgChild1.orgChild1RankSub, + orgRootDnaId: orgRoot.ancestorDNA, + orgChild1DnaId: orgChild1.ancestorDNA, + DEPARTMENT_CODE: orgChild1.DEPARTMENT_CODE, + DIVISION_CODE: orgChild1.DIVISION_CODE, + SECTION_CODE: orgChild1.SECTION_CODE, + JOB_CODE: orgChild1.JOB_CODE, + orgTreeOrder: orgChild1.orgChild1Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild1.orgChild1PhoneEx, + orgTreePhoneIn: orgChild1.orgChild1PhoneIn, + orgTreeFax: orgChild1.orgChild1Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild1.responsibility, + isOfficer: orgChild1.isOfficer, + isInformation: orgChild1.isInformation, + labelName: + orgChild1.orgChild1Name + + " " + + orgRoot.orgRootCode + + orgChild1.orgChild1Code + + " " + + orgChild1.orgChild1ShortName, + // totalPosition: child1Counts.totalPosition, + // totalPositionCurrentUse: child1Counts.totalPositionCurrentUse, + // totalPositionCurrentVacant: child1Counts.totalPositionCurrentVacant, + // totalPositionNextUse: child1Counts.totalPositionNextUse, + // totalPositionNextVacant: child1Counts.totalPositionNextVacant, + // totalRootPosition: child1PosCounts.totalRootPosition, + // totalRootPositionCurrentUse: child1PosCounts.totalRootPositionCurrentUse, + // totalRootPositionCurrentVacant: child1PosCounts.totalRootPositionCurrentVacant, + // totalRootPositionNextUse: child1PosCounts.totalRootPositionNextUse, + // totalRootPositionNextVacant: child1PosCounts.totalRootPositionNextVacant, + children: orgChild2Data + .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) + .map((orgChild2) => { + // const child2Counts = getCounts(orgChild2Map, orgChild2.id); + // const child2PosKey = `${orgRoot.id}-${orgChild1.id}-${orgChild2.id}`; + // const child2PosCounts = getRootCounts(rootPosMap, child2PosKey); - children: await Promise.all( - orgChild3Data - .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) - .map(async (orgChild3) => ({ - orgTreeId: orgChild3.id, - orgRootId: orgChild2.id, - orgLevel: 3, - orgName: `${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild3.orgChild3Name, - orgTreeShortName: orgChild3.orgChild3ShortName, - orgTreeCode: orgChild3.orgChild3Code, - orgCode: orgRoot.orgRootCode + orgChild3.orgChild3Code, - orgTreeRank: orgChild3.orgChild3Rank, - orgTreeRankSub: orgChild3.orgChild3RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - orgChild2DnaId: orgChild2.ancestorDNA, - orgChild3DnaId: orgChild3.ancestorDNA, - DEPARTMENT_CODE: orgChild3.DEPARTMENT_CODE, - DIVISION_CODE: orgChild3.DIVISION_CODE, - SECTION_CODE: orgChild3.SECTION_CODE, - JOB_CODE: orgChild3.JOB_CODE, - orgTreeOrder: orgChild3.orgChild3Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild3.orgChild3PhoneEx, - orgTreePhoneIn: orgChild3.orgChild3PhoneIn, - orgTreeFax: orgChild3.orgChild3Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild3.responsibility, - labelName: - orgChild3.orgChild3Name + - " " + - orgRoot.orgRootCode + - orgChild3.orgChild3Code + - " " + - orgChild3.orgChild3ShortName, - totalPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), + return { + orgTreeId: orgChild2.id, + orgRootId: orgChild1.id, + orgLevel: 2, + orgName: `${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + orgTreeCode: orgChild2.orgChild2Code, + orgCode: orgRoot.orgRootCode + orgChild2.orgChild2Code, + orgTreeRank: orgChild2.orgChild2Rank, + orgTreeRankSub: orgChild2.orgChild2RankSub, + orgRootDnaId: orgRoot.ancestorDNA, + orgChild1DnaId: orgChild1.ancestorDNA, + orgChild2DnaId: orgChild2.ancestorDNA, + DEPARTMENT_CODE: orgChild2.DEPARTMENT_CODE, + DIVISION_CODE: orgChild2.DIVISION_CODE, + SECTION_CODE: orgChild2.SECTION_CODE, + JOB_CODE: orgChild2.JOB_CODE, + orgTreeOrder: orgChild2.orgChild2Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild2.orgChild2PhoneEx, + orgTreePhoneIn: orgChild2.orgChild2PhoneIn, + orgTreeFax: orgChild2.orgChild2Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild2.responsibility, + labelName: + orgChild2.orgChild2Name + + " " + + orgRoot.orgRootCode + + orgChild2.orgChild2Code + + " " + + orgChild2.orgChild2ShortName, + // totalPosition: child2Counts.totalPosition, + // totalPositionCurrentUse: child2Counts.totalPositionCurrentUse, + // totalPositionCurrentVacant: child2Counts.totalPositionCurrentVacant, + // totalPositionNextUse: child2Counts.totalPositionNextUse, + // totalPositionNextVacant: child2Counts.totalPositionNextVacant, + // totalRootPosition: child2PosCounts.totalRootPosition, + // totalRootPositionCurrentUse: child2PosCounts.totalRootPositionCurrentUse, + // totalRootPositionCurrentVacant: child2PosCounts.totalRootPositionCurrentVacant, + // totalRootPositionNextUse: child2PosCounts.totalRootPositionNextUse, + // totalRootPositionNextVacant: child2PosCounts.totalRootPositionNextVacant, + children: orgChild3Data + .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) + .map((orgChild3) => { + // const child3Counts = getCounts(orgChild3Map, orgChild3.id); + // const child3PosKey = `${orgRoot.id}-${orgChild1.id}-${orgChild2.id}-${orgChild3.id}`; + // const child3PosCounts = getRootCounts(rootPosMap, child3PosKey); - children: await Promise.all( - orgChild4Data - .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) - .map(async (orgChild4) => ({ - orgTreeId: orgChild4.id, - orgRootId: orgChild3.id, - orgLevel: 4, - orgName: `${orgChild4.orgChild4Name}/${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, - orgTreeName: orgChild4.orgChild4Name, - orgTreeShortName: orgChild4.orgChild4ShortName, - orgTreeCode: orgChild4.orgChild4Code, - orgCode: orgRoot.orgRootCode + orgChild4.orgChild4Code, - orgTreeRank: orgChild4.orgChild4Rank, - orgTreeRankSub: orgChild4.orgChild4RankSub, - orgRootDnaId: orgRoot.ancestorDNA, - orgChild1DnaId: orgChild1.ancestorDNA, - orgChild2DnaId: orgChild2.ancestorDNA, - orgChild3DnaId: orgChild3.ancestorDNA, - orgChild4DnaId: orgChild4.ancestorDNA, - DEPARTMENT_CODE: orgChild4.DEPARTMENT_CODE, - DIVISION_CODE: orgChild4.DIVISION_CODE, - SECTION_CODE: orgChild4.SECTION_CODE, - JOB_CODE: orgChild4.JOB_CODE, - orgTreeOrder: orgChild4.orgChild4Order, - orgRootCode: orgRoot.orgRootCode, - orgTreePhoneEx: orgChild4.orgChild4PhoneEx, - orgTreePhoneIn: orgChild4.orgChild4PhoneIn, - orgTreeFax: orgChild4.orgChild4Fax, - orgRevisionId: orgRoot.orgRevisionId, - orgRootName: orgRoot.orgRootName, - responsibility: orgChild4.responsibility, - labelName: - orgChild4.orgChild4Name + - " " + - orgRoot.orgRootCode + - orgChild4.orgChild4Code + - " " + - orgChild4.orgChild4ShortName, - totalPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild4Id: orgChild4.id, - }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild4Id: orgChild4.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild4Id: orgChild4.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild4Id: orgChild4.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count( - { - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }, - ), - totalRootPositionCurrentVacant: - await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count( - { - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - next_holderId: IsNull() || "", - }, - }, - ), - })), - ), - })), - ), - })), - ), - })), - ), - }; - }), - ); + return { + orgTreeId: orgChild3.id, + orgRootId: orgChild2.id, + orgLevel: 3, + orgName: `${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + orgTreeCode: orgChild3.orgChild3Code, + orgCode: orgRoot.orgRootCode + orgChild3.orgChild3Code, + orgTreeRank: orgChild3.orgChild3Rank, + orgTreeRankSub: orgChild3.orgChild3RankSub, + orgRootDnaId: orgRoot.ancestorDNA, + orgChild1DnaId: orgChild1.ancestorDNA, + orgChild2DnaId: orgChild2.ancestorDNA, + orgChild3DnaId: orgChild3.ancestorDNA, + DEPARTMENT_CODE: orgChild3.DEPARTMENT_CODE, + DIVISION_CODE: orgChild3.DIVISION_CODE, + SECTION_CODE: orgChild3.SECTION_CODE, + JOB_CODE: orgChild3.JOB_CODE, + orgTreeOrder: orgChild3.orgChild3Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild3.orgChild3PhoneEx, + orgTreePhoneIn: orgChild3.orgChild3PhoneIn, + orgTreeFax: orgChild3.orgChild3Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild3.responsibility, + labelName: + orgChild3.orgChild3Name + + " " + + orgRoot.orgRootCode + + orgChild3.orgChild3Code + + " " + + orgChild3.orgChild3ShortName, + // totalPosition: child3Counts.totalPosition, + // totalPositionCurrentUse: child3Counts.totalPositionCurrentUse, + // totalPositionCurrentVacant: child3Counts.totalPositionCurrentVacant, + // totalPositionNextUse: child3Counts.totalPositionNextUse, + // totalPositionNextVacant: child3Counts.totalPositionNextVacant, + // totalRootPosition: child3PosCounts.totalRootPosition, + // totalRootPositionCurrentUse: child3PosCounts.totalRootPositionCurrentUse, + // totalRootPositionCurrentVacant: + // child3PosCounts.totalRootPositionCurrentVacant, + // totalRootPositionNextUse: child3PosCounts.totalRootPositionNextUse, + // totalRootPositionNextVacant: child3PosCounts.totalRootPositionNextVacant, + children: orgChild4Data + .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) + .map((orgChild4) => { + // const child4Counts = getCounts(orgChild4Map, orgChild4.id); + // const child4PosKey = `${orgRoot.id}-${orgChild1.id}-${orgChild2.id}-${orgChild3.id}-${orgChild4.id}`; + // const child4PosCounts = getRootCounts(rootPosMap, child4PosKey); + + return { + orgTreeId: orgChild4.id, + orgRootId: orgChild3.id, + orgLevel: 4, + orgName: `${orgChild4.orgChild4Name}/${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + orgTreeCode: orgChild4.orgChild4Code, + orgCode: orgRoot.orgRootCode + orgChild4.orgChild4Code, + orgTreeRank: orgChild4.orgChild4Rank, + orgTreeRankSub: orgChild4.orgChild4RankSub, + orgRootDnaId: orgRoot.ancestorDNA, + orgChild1DnaId: orgChild1.ancestorDNA, + orgChild2DnaId: orgChild2.ancestorDNA, + orgChild3DnaId: orgChild3.ancestorDNA, + orgChild4DnaId: orgChild4.ancestorDNA, + DEPARTMENT_CODE: orgChild4.DEPARTMENT_CODE, + DIVISION_CODE: orgChild4.DIVISION_CODE, + SECTION_CODE: orgChild4.SECTION_CODE, + JOB_CODE: orgChild4.JOB_CODE, + orgTreeOrder: orgChild4.orgChild4Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild4.orgChild4PhoneEx, + orgTreePhoneIn: orgChild4.orgChild4PhoneIn, + orgTreeFax: orgChild4.orgChild4Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild4.responsibility, + labelName: + orgChild4.orgChild4Name + + " " + + orgRoot.orgRootCode + + orgChild4.orgChild4Code + + " " + + orgChild4.orgChild4ShortName, + // totalPosition: child4Counts.totalPosition, + // totalPositionCurrentUse: child4Counts.totalPositionCurrentUse, + // totalPositionCurrentVacant: child4Counts.totalPositionCurrentVacant, + // totalPositionNextUse: child4Counts.totalPositionNextUse, + // totalPositionNextVacant: child4Counts.totalPositionNextVacant, + // totalRootPosition: child4PosCounts.totalRootPosition, + // totalRootPositionCurrentUse: + // child4PosCounts.totalRootPositionCurrentUse, + // totalRootPositionCurrentVacant: + // child4PosCounts.totalRootPositionCurrentVacant, + // totalRootPositionNextUse: child4PosCounts.totalRootPositionNextUse, + // totalRootPositionNextVacant: + // child4PosCounts.totalRootPositionNextVacant, + children: [], + }; + }), + }; + }), + }; + }), + }; + }), + }; + }); return new HttpSuccess(formattedData); } diff --git a/src/controllers/OrganizationController-optimized.ts b/src/services/OrganizationService.ts similarity index 92% rename from src/controllers/OrganizationController-optimized.ts rename to src/services/OrganizationService.ts index a5de09b6..a9b2b796 100644 --- a/src/controllers/OrganizationController-optimized.ts +++ b/src/services/OrganizationService.ts @@ -12,6 +12,8 @@ export async function getPositionCounts(orgRevisionId: string) { "pos.orgChild2Id", "pos.orgChild3Id", "pos.orgChild4Id", + "pos.current_holderId", + "pos.next_holderId", ]) .where("pos.orgRevisionId = :orgRevisionId", { orgRevisionId }) .getMany(); @@ -27,7 +29,7 @@ export async function getPositionCounts(orgRevisionId: string) { const orgChild4Map = new Map(); // Nested maps for root positions (positions at specific levels) - const rootPosMap = new Map(); // key: "rootId", "rootId-child1Id", "rootId-child1Id-child2Id", etc. + const rootPosMap = new Map(); for (const pos of rawData) { const orgRootId = pos.orgRootId || "NULL"; @@ -57,8 +59,8 @@ export async function getPositionCounts(orgRevisionId: string) { // Level 1 (orgChild1) counts if (!isNull(pos.orgChild1Id)) { - if (!orgChild1Map.has(pos.orgChild1Id)) { - orgChild1Map.set(pos.orgChild1Id, { + if (!orgChild1Map.has(orgChild1Id)) { + orgChild1Map.set(orgChild1Id, { totalPosition: 0, totalPositionCurrentUse: 0, totalPositionCurrentVacant: 0, @@ -66,7 +68,7 @@ export async function getPositionCounts(orgRevisionId: string) { totalPositionNextVacant: 0, }); } - const child1Counts = orgChild1Map.get(pos.orgChild1Id); + const child1Counts = orgChild1Map.get(orgChild1Id); child1Counts.totalPosition++; if (!isNull(pos.current_holderId)) child1Counts.totalPositionCurrentUse++; else child1Counts.totalPositionCurrentVacant++; @@ -76,8 +78,8 @@ export async function getPositionCounts(orgRevisionId: string) { // Level 2 (orgChild2) counts if (!isNull(pos.orgChild2Id)) { - if (!orgChild2Map.has(pos.orgChild2Id)) { - orgChild2Map.set(pos.orgChild2Id, { + if (!orgChild2Map.has(orgChild2Id)) { + orgChild2Map.set(orgChild2Id, { totalPosition: 0, totalPositionCurrentUse: 0, totalPositionCurrentVacant: 0, @@ -85,7 +87,7 @@ export async function getPositionCounts(orgRevisionId: string) { totalPositionNextVacant: 0, }); } - const child2Counts = orgChild2Map.get(pos.orgChild2Id); + const child2Counts = orgChild2Map.get(orgChild2Id); child2Counts.totalPosition++; if (!isNull(pos.current_holderId)) child2Counts.totalPositionCurrentUse++; else child2Counts.totalPositionCurrentVacant++; @@ -95,8 +97,8 @@ export async function getPositionCounts(orgRevisionId: string) { // Level 3 (orgChild3) counts if (!isNull(pos.orgChild3Id)) { - if (!orgChild3Map.has(pos.orgChild3Id)) { - orgChild3Map.set(pos.orgChild3Id, { + if (!orgChild3Map.has(orgChild3Id)) { + orgChild3Map.set(orgChild3Id, { totalPosition: 0, totalPositionCurrentUse: 0, totalPositionCurrentVacant: 0, @@ -104,7 +106,7 @@ export async function getPositionCounts(orgRevisionId: string) { totalPositionNextVacant: 0, }); } - const child3Counts = orgChild3Map.get(pos.orgChild3Id); + const child3Counts = orgChild3Map.get(orgChild3Id); child3Counts.totalPosition++; if (!isNull(pos.current_holderId)) child3Counts.totalPositionCurrentUse++; else child3Counts.totalPositionCurrentVacant++; @@ -114,8 +116,8 @@ export async function getPositionCounts(orgRevisionId: string) { // Level 4 (orgChild4) counts if (!isNull(pos.orgChild4Id)) { - if (!orgChild4Map.has(pos.orgChild4Id)) { - orgChild4Map.set(pos.orgChild4Id, { + if (!orgChild4Map.has(orgChild4Id)) { + orgChild4Map.set(orgChild4Id, { totalPosition: 0, totalPositionCurrentUse: 0, totalPositionCurrentVacant: 0, @@ -123,7 +125,7 @@ export async function getPositionCounts(orgRevisionId: string) { totalPositionNextVacant: 0, }); } - const child4Counts = orgChild4Map.get(pos.orgChild4Id); + const child4Counts = orgChild4Map.get(orgChild4Id); child4Counts.totalPosition++; if (!isNull(pos.current_holderId)) child4Counts.totalPositionCurrentUse++; else child4Counts.totalPositionCurrentVacant++; @@ -198,7 +200,7 @@ export async function getPositionCounts(orgRevisionId: string) { // For orgChild3 level if (!isNull(pos.orgChild3Id)) { - const child3LevelKey = `${orgRootId}-${pos.orgChild1Id}-${pos.orgChild2Id}-${pos.orgChild3Id}`; + const child3LevelKey = `${orgRootId}-${pos.orgChild1Id}-${pos.orgChild2Id}-${orgChild3Id}`; if (!rootPosMap.has(child3LevelKey)) { rootPosMap.set(child3LevelKey, { totalRootPosition: 0, @@ -220,7 +222,7 @@ export async function getPositionCounts(orgRevisionId: string) { // For orgChild4 level if (!isNull(pos.orgChild4Id)) { - const child4LevelKey = `${orgRootId}-${pos.orgChild1Id}-${pos.orgChild2Id}-${pos.orgChild3Id}-${pos.orgChild4Id}`; + const child4LevelKey = `${orgRootId}-${pos.orgChild1Id}-${pos.orgChild2Id}-${orgChild3Id}-${pos.orgChild4Id}`; if (!rootPosMap.has(child4LevelKey)) { rootPosMap.set(child4LevelKey, { totalRootPosition: 0,