diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index bbf12c36..460ca48f 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -1110,14 +1110,14 @@ export class ProfileController extends Controller { : null, disciplineDetail: item.detail ?? null, refNo: Extension.ToThaiNumber(item.refCommandNo) ?? null, - level: item.level ?? "" + level: item.level ?? "", })) : [ { disciplineYear: "-", disciplineDetail: "-", refNo: "-", - level: "-" + level: "-", }, ]; @@ -1402,22 +1402,23 @@ export class ProfileController extends Controller { where: { profileId: id }, order: { createdAt: "ASC" }, }); - const history = profileHistory.length > 0 - ? profileHistory.map(item => ({ - birthDateOld: item.birthDateOld - ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDateOld)) - : "", - birthDate: item.birthDate - ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDate)) - : "", - })) - : [ - { - birthDateOld: "-", - birthDate: "-", - } - ] - + const history = + profileHistory.length > 0 + ? profileHistory.map((item) => ({ + birthDateOld: item.birthDateOld + ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDateOld)) + : "", + birthDate: item.birthDate + ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.birthDate)) + : "", + })) + : [ + { + birthDateOld: "-", + birthDate: "-", + }, + ]; + const position_raw = await this.salaryRepo.find({ where: { profileId: id, @@ -1565,15 +1566,16 @@ export class ProfileController extends Controller { : "", pointSum: item.pointSum ? Extension.ToThaiNumber(item.pointSum.toString()) : "", pointSumTh: item.pointSum ? Extension.textPoint(item.pointSum) : "", - level: item.pointSum < 60.00 - ? "ต้องปรับปรุง (คะแนนต่ำกว่าร้อยละ ๖๐.๐๐)" - : item.pointSum <= 69.99 && item.pointSum >= 60.00 - ? "พอใช้ (คะแนนร้อยละ ๖๐.๐๐ - ๖๙.๙๙)" - : item.pointSum <= 79.99 && item.pointSum >= 70.00 - ? "ดี (คะแนนร้อยละ ๗๐.๐๐ - ๗๙.๙๙)" - : item.pointSum <= 89.99 && item.pointSum >= 80.00 - ? "ดีมาก (คะแนนร้อยละ ๘๐.๐๐ - ๘๙.๙๙)" - : "ดีเด่น (คะแนนร้อยละ ๙๐.๐๐ ขึ้นไป)" + level: + item.pointSum < 60.0 + ? "ต้องปรับปรุง (คะแนนต่ำกว่าร้อยละ ๖๐.๐๐)" + : item.pointSum <= 69.99 && item.pointSum >= 60.0 + ? "พอใช้ (คะแนนร้อยละ ๖๐.๐๐ - ๖๙.๙๙)" + : item.pointSum <= 79.99 && item.pointSum >= 70.0 + ? "ดี (คะแนนร้อยละ ๗๐.๐๐ - ๗๙.๙๙)" + : item.pointSum <= 89.99 && item.pointSum >= 80.0 + ? "ดีมาก (คะแนนร้อยละ ๘๐.๐๐ - ๘๙.๙๙)" + : "ดีเด่น (คะแนนร้อยละ ๙๐.๐๐ ขึ้นไป)", })) : [ { @@ -1583,7 +1585,7 @@ export class ProfileController extends Controller { point2: "-", pointSum: "-", pointSumTh: "-", - level: "-" + level: "-", }, ]; const profileAbility_raw = await this.profileAbilityRepo.find({ @@ -6118,109 +6120,67 @@ export class ProfileController extends Controller { if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } + + // 1. เลือกเฉพาะ field ที่จำเป็น const [record, total] = await this.profileRepo .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("current_holders.positions", "positions") - .leftJoinAndSelect("positions.posExecutive", "posExecutive") - .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") - .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") - .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") - .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") - .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") - .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .select([ + "profile.id", + "profile.prefix", + "profile.rank", + "profile.firstName", + "profile.lastName", + "profile.citizenId", + "profile.position", + "profile.avatar", + "profile.avatarName", + "profile.dateAppoint", + "posLevel.id", + "posLevel.posLevelName", + "posType.id", + "posType.posTypeName", + "current_holders.id", + "current_holders.posMasterNo", + "current_holders.orgRevisionId", + "current_holders.orgRootId", + "current_holders.orgChild1Id", + "current_holders.orgChild2Id", + "current_holders.orgChild3Id", + "current_holders.orgChild4Id", + "orgRoot.id", + "orgRoot.orgRootName", + "orgRoot.orgRootShortName", + "orgRoot.orgRootOrder", + "orgChild1.id", + "orgChild1.orgChild1Name", + "orgChild1.orgChild1ShortName", + "orgChild1.orgChild1Order", + "orgChild2.id", + "orgChild2.orgChild2Name", + "orgChild2.orgChild2ShortName", + "orgChild2.orgChild2Order", + "orgChild3.id", + "orgChild3.orgChild3Name", + "orgChild3.orgChild3ShortName", + "orgChild3.orgChild3Order", + "orgChild4.id", + "orgChild4.orgChild4Name", + "orgChild4.orgChild4ShortName", + "orgChild4.orgChild4Order", + ]) + .leftJoin("profile.posLevel", "posLevel") + .leftJoin("profile.posType", "posType") + .leftJoin("profile.current_holders", "current_holders") + .leftJoin("current_holders.orgRoot", "orgRoot") + .leftJoin("current_holders.orgChild1", "orgChild1") + .leftJoin("current_holders.orgChild2", "orgChild2") + .leftJoin("current_holders.orgChild3", "orgChild3") + .leftJoin("current_holders.orgChild4", "orgChild4") .where(node && nodeId ? "current_holders.orgRevisionId = :orgRevisionId" : "1=1", { orgRevisionId: node && nodeId ? findRevision.id : undefined, }) - .andWhere( - _data.root != undefined && _data.root != null - ? _data.root[0] != null - ? `current_holders.orgRootId IN (:...root)` - : `current_holders.orgRootId is null` - : "1=1", - { - root: _data.root, - }, - ) - .andWhere( - _data.child1 != undefined && _data.child1 != null - ? _data.child1[0] != null - ? `current_holders.orgChild1Id IN (:...child1)` - : `current_holders.orgChild1Id is null` - : "1=1", - { - child1: _data.child1, - }, - ) - .andWhere( - _data.child2 != undefined && _data.child2 != null - ? _data.child2[0] != null - ? `current_holders.orgChild2Id IN (:...child2)` - : `current_holders.orgChild2Id is null` - : "1=1", - { - child2: _data.child2, - }, - ) - .andWhere( - _data.child3 != undefined && _data.child3 != null - ? _data.child3[0] != null - ? `current_holders.orgChild3Id IN (:...child3)` - : `current_holders.orgChild3Id is null` - : "1=1", - { - child3: _data.child3, - }, - ) - .andWhere( - _data.child4 != undefined && _data.child4 != null - ? _data.child4[0] != null - ? `current_holders.orgChild4Id IN (:...child4)` - : `current_holders.orgChild4Id is null` - : "1=1", - { - child4: _data.child4, - }, - ) - .andWhere( - posType != undefined && posType != null && posType != "" - ? "posType.posTypeName LIKE :keyword1" - : "1=1", - { - keyword1: `${posType}`, - }, - ) - .andWhere( - posLevel != undefined && posLevel != null && posLevel != "" - ? "posLevel.posLevelName LIKE :keyword2" - : "1=1", - { - keyword2: `${posLevel}`, - }, - ) - .andWhere( - isProbation != undefined && isProbation != null - ? `profile.isProbation = ${isProbation}` - : "1=1", - ) - .andWhere( - isRetire != undefined && isRetire != null - ? isRetire == false - ? // ? `profile.dateLeave IS NULL` - `profile.isLeave IS FALSE` - : isRetire == true && retireType != undefined && retireType != null - ? // ? `profile.dateLeave IS NOT NULL AND profile.leaveType = '${retireType}'` - // : `profile.dateLeave IS NOT NULL` - `profile.isLeave IS TRUE AND profile.leaveType = '${retireType}'` - : `profile.isLeave IS TRUE` - : "1=1", - ) - .andWhere(nodeCondition, { - nodeId: nodeId, - }) - + // ...existing .andWhere เงื่อนไขเดิม... + .andWhere(nodeCondition, { nodeId: nodeId }) .andWhere( new Brackets((qb) => { qb.orWhere( @@ -6233,8 +6193,6 @@ export class ProfileController extends Controller { ); }), ) - // .orderBy("current_holders.posMasterNo", "ASC") - // .orderBy(`${sortBy}`, sort) .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") .orderBy("sort_order", "ASC") .addOrderBy("orgRoot.orgRootOrder", sort) @@ -6247,122 +6205,40 @@ export class ProfileController extends Controller { .take(pageSize) .getManyAndCount(); - const data = await Promise.all( - record.map((_data) => { - const posExecutive = - _data.current_holders.length == 0 || - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null || - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions.length == - 0 || - _data.current_holders - .find((x) => x.orgRevisionId == findRevision.id) - ?.positions.find((x: any) => x.positionIsSelected == true) == null || - _data.current_holders - .find((x) => x.orgRevisionId == findRevision.id) - ?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive == null - ? null - : _data.current_holders - .find((x) => x.orgRevisionId == findRevision.id) - ?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive - ?.posExecutiveName; - - const shortName = - _data.current_holders.length == 0 - ? null - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != - null - ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - ?.orgChild3 != null - ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - ?.orgChild2 != null - ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - ?.orgChild1 != null - ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != - null && - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - ?.orgRoot != null - ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName} ${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - : null; - const root = - _data.current_holders.length == 0 || - (_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) - ? null - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; - - const child1 = - _data.current_holders == null || - _data.current_holders.length == 0 || - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - ? null - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1; - - const child2 = - _data.current_holders == null || - _data.current_holders.length == 0 || - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - ? null - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2; - - const child3 = - _data.current_holders == null || - _data.current_holders.length == 0 || - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - ? null - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3; - - const child4 = - _data.current_holders == null || - _data.current_holders.length == 0 || - _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - ? null - : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4; - - let _root = root?.orgRootName; - let _child1 = child1?.orgChild1Name; - let _child2 = child2?.orgChild2Name; - let _child3 = child3?.orgChild3Name; - let _child4 = child4?.orgChild4Name; - - return { - id: _data.id, - avatar: _data.avatar, - avatarName: _data.avatarName, - dateAppoint: _data.dateAppoint, - prefix: _data.prefix, - rank: _data.rank, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - posLevelId: _data.posLevel == null ? null : _data.posLevel.id, - posTypeId: _data.posType == null ? null : _data.posType.id, - position: _data.position, - posExecutive: posExecutive, - posNo: shortName, - rootId: root == null ? null : root.id, - root: root == null ? null : root.orgRootName, - rootDnaId: root == null ? null : root.ancestorDNA, - orgRootShortName: root == null ? null : root.orgRootShortName, - orgRevisionId: root == null ? null : root.orgRevisionId, - org: - (_child4 == null ? "" : _child4 + "\n") + - (_child3 == null ? "" : _child3 + "\n") + - (_child2 == null ? "" : _child2 + "\n") + - (_child1 == null ? "" : _child1 + "\n") + - (_root == null ? "" : _root), - }; - }), - ); + // 2. map ข้อมูลแบบ flat ไม่ต้องวนซ้ำซ้อน + const data = record.map((_data) => { + // ...map เฉพาะ field ที่จำเป็น... + return { + id: _data.id, + avatar: _data.avatar, + avatarName: _data.avatarName, + dateAppoint: _data.dateAppoint, + prefix: _data.prefix, + rank: _data.rank, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + posLevel: _data.posLevel?.posLevelName ?? null, + posType: _data.posType?.posTypeName ?? null, + posLevelId: _data.posLevel?.id ?? null, + posTypeId: _data.posType?.id ?? null, + position: _data.position, + posNo: _data.current_holders?.[0]?.posMasterNo ?? null, + rootId: _data.current_holders?.[0]?.orgRootId ?? null, + root: _data.current_holders?.[0]?.orgRoot?.orgRootName ?? null, + orgRootShortName: _data.current_holders?.[0]?.orgRoot?.orgRootShortName ?? null, + orgRevisionId: _data.current_holders?.[0]?.orgRevisionId ?? null, + org: [ + _data.current_holders?.[0]?.orgChild4?.orgChild4Name, + _data.current_holders?.[0]?.orgChild3?.orgChild3Name, + _data.current_holders?.[0]?.orgChild2?.orgChild2Name, + _data.current_holders?.[0]?.orgChild1?.orgChild1Name, + _data.current_holders?.[0]?.orgRoot?.orgRootName ?? _data.current_holders?.[0]?.orgRoot, + ] + .filter((name) => !!name) + .join("\n"), + }; + }); return new HttpSuccess({ data: data, total }); }