diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index f12117b6..addc33db 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -8593,13 +8593,29 @@ export class OrganizationDotnetController extends Controller { where: { ...typeCondition, createdAt: LessThanOrEqual(date), - // firstName: Not("") && Not(IsNull()), - // lastName: Not("") && Not(IsNull()), }, + select: [ + "profileId", + "prefix", + "firstName", + "lastName", + "shortName", + "posMasterNo", + "position", + "posType", + "posLevel", + "ancestorDNA", + "rootDnaId", + "child1DnaId", + "child2DnaId", + "child3DnaId", + "child4DnaId", + "createdAt", + ], order: { firstName: "ASC", lastName: "ASC", - createdAt: "DESC", // ให้ createdAt ล่าสุดอยู่ข้างบน + createdAt: "DESC", }, }); @@ -8646,36 +8662,41 @@ export class OrganizationDotnetController extends Controller { } } - const profile_ = await Promise.all( - Array.from(grouped3.values()) - .filter((x) => x.profileId != null) - .map(async (item: PosMasterHistory) => { - let profile = await this.profileRepo.findOne({ - where: { id: item.profileId }, - }); + const profileIds = Array.from(grouped3.values()) + .filter((x) => x.profileId != null) + .map((x) => x.profileId); - return { - id: item.profileId, - prefix: item.prefix, - firstName: item.firstName, - lastName: item.lastName, - citizenId: profile?.citizenId ?? null, - dateStart: profile?.dateStart ?? null, - dateAppoint: profile?.dateAppoint ?? null, - keycloak: profile?.keycloak ?? null, - posNo: `${item.shortName} ${item.posMasterNo}`, - position: item.position, - positionLevel: item.posLevel, - positionType: item.posType, - // oc: Oc, - orgRootId: item.rootDnaId, - orgChild1Id: item.child1DnaId, - orgChild2Id: item.child2DnaId, - orgChild3Id: item.child3DnaId, - orgChild4Id: item.child4DnaId, - }; - }), - ); + const profiles = await this.profileRepo.find({ + where: { id: In(profileIds) }, + select: ["id", "citizenId", "dateStart", "dateAppoint", "keycloak"], + }); + + const profileMap = new Map(profiles.map((p) => [p.id, p])); + + const profile_ = Array.from(grouped3.values()) + .filter((x) => x.profileId != null) + .map((item: PosMasterHistory) => { + const profile = profileMap.get(item.profileId); + return { + id: item.profileId, + prefix: item.prefix, + firstName: item.firstName, + lastName: item.lastName, + citizenId: profile?.citizenId ?? null, + dateStart: profile?.dateStart ?? null, + dateAppoint: profile?.dateAppoint ?? null, + keycloak: profile?.keycloak ?? null, + posNo: `${item.shortName} ${item.posMasterNo}`, + position: item.position, + positionLevel: item.posLevel, + positionType: item.posType, + orgRootId: item.rootDnaId, + orgChild1Id: item.child1DnaId, + orgChild2Id: item.child2DnaId, + orgChild3Id: item.child3DnaId, + orgChild4Id: item.child4DnaId, + }; + }); return new HttpSuccess( (profile_ ?? []).sort((a, b) => a.posNo.localeCompare(b.posNo, undefined, { numeric: true })),