From 7c70229579fbcf5b43bd9ded9c1f8925c8019305 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Wed, 28 Jan 2026 17:48:28 +0700 Subject: [PATCH] fix: query use Promise all --- src/controllers/OrganizationController.ts | 65 ++++++++++++++--------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index db33f1ad..8b027cdb 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -1200,6 +1200,7 @@ export class OrganizationController extends Controller { // const { orgRootMap, orgChild1Map, orgChild2Map, orgChild3Map, orgChild4Map, rootPosMap } = // await getPositionCounts(id); + // OPTIMIZED: Fetch orgRoot first, then fetch all child levels in parallel const orgRootData = await AppDataSource.getRepository(OrgRoot) .createQueryBuilder("orgRoot") .where("orgRoot.orgRevisionId = :id", { id }) @@ -1209,44 +1210,60 @@ export class OrganizationController extends Controller { .orderBy("orgRoot.orgRootOrder", "ASC") .getMany(); - const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; + const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id); + + // OPTIMIZED: Fetch all child levels in parallel using orgRevisionId + // This is faster than sequential queries that depend on parent IDs + const [orgChild1AllData, orgChild2AllData, orgChild3AllData, orgChild4AllData] = + await Promise.all([ + AppDataSource.getRepository(OrgChild1) + .createQueryBuilder("orgChild1") + .where("orgChild1.orgRevisionId = :id", { id }) + .orderBy("orgChild1.orgChild1Order", "ASC") + .getMany(), + + AppDataSource.getRepository(OrgChild2) + .createQueryBuilder("orgChild2") + .where("orgChild2.orgRevisionId = :id", { id }) + .orderBy("orgChild2.orgChild2Order", "ASC") + .getMany(), + + AppDataSource.getRepository(OrgChild3) + .createQueryBuilder("orgChild3") + .where("orgChild3.orgRevisionId = :id", { id }) + .orderBy("orgChild3.orgChild3Order", "ASC") + .getMany(), + + AppDataSource.getRepository(OrgChild4) + .createQueryBuilder("orgChild4") + .where("orgChild4.orgRevisionId = :id", { id }) + .orderBy("orgChild4.orgChild4Order", "ASC") + .getMany(), + ]); + + // Filter child1 data by orgRootIds (maintains backward compatibility) const orgChild1Data = orgRootIds && orgRootIds.length > 0 - ? await AppDataSource.getRepository(OrgChild1) - .createQueryBuilder("orgChild1") - .where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds }) - .orderBy("orgChild1.orgChild1Order", "ASC") - .getMany() + ? orgChild1AllData.filter((orgChild1) => orgRootIds.includes(orgChild1.orgRootId)) : []; - const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; + // Build maps for efficient filtering of deeper levels + const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id); const orgChild2Data = orgChild1Ids && orgChild1Ids.length > 0 - ? await AppDataSource.getRepository(OrgChild2) - .createQueryBuilder("orgChild2") - .where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids }) - .orderBy("orgChild2.orgChild2Order", "ASC") - .getMany() + ? orgChild2AllData.filter((orgChild2) => orgChild1Ids.includes(orgChild2.orgChild1Id)) : []; - const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; + const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id); const orgChild3Data = orgChild2Ids && orgChild2Ids.length > 0 - ? await AppDataSource.getRepository(OrgChild3) - .createQueryBuilder("orgChild3") - .where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids }) - .orderBy("orgChild3.orgChild3Order", "ASC") - .getMany() + ? orgChild3AllData.filter((orgChild3) => orgChild2Ids.includes(orgChild3.orgChild2Id)) : []; - const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; + const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id); const orgChild4Data = orgChild3Ids && orgChild3Ids.length > 0 - ? await AppDataSource.getRepository(OrgChild4) - .createQueryBuilder("orgChild4") - .where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids }) - .orderBy("orgChild4.orgChild4Order", "ASC") - .getMany() + ? orgChild4AllData.filter((orgChild4) => orgChild3Ids.includes(orgChild4.orgChild3Id)) : []; // OPTIMIZED: Build formatted data using pre-calculated counts (no nested queries!)