diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index cef27dcb..2bc89eb7 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -86,27 +86,26 @@ export class PositionController extends Controller { } const checkPosTypeId = await this.posTypeRepository.findOne({ - where: { id: requestBody.posTypeId }, + where: { id: posDict.posTypeId }, }); if (!checkPosTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosTypeId"); } const checkPosLevelId = await this.posLevelRepository.findOne({ - where: { id: requestBody.posLevelId }, + where: { id: posDict.posLevelId }, }); if (!checkPosLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); } - if (requestBody.posExecutiveId == "") { - requestBody.posExecutiveId = null; + if (posDict.posExecutiveId == "") { posDict.posExecutiveId = null; } - if (requestBody.posExecutiveId != null) { + if (posDict.posExecutiveId != null) { const checkPosExecutiveId = await this.posExecutiveRepository.findOne({ - where: { id: requestBody.posExecutiveId }, + where: { id: posDict.posExecutiveId }, }); if (!checkPosExecutiveId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId"); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 98a33072..7e5c072f 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -176,11 +176,23 @@ export class ReportController extends Controller { }); let orgRevisionActive: any = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + ], }); if (orgRevisionActive == null) { const _orgRevisionActive = await this.orgRevisionRepository.find({ order: { orgRevisionCreatedAt: "DESC" }, skip: 1, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + ], }); if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0]; } @@ -205,114 +217,141 @@ export class ReportController extends Controller { orgTreeName: orgRoot.orgRootName, orgTreeShortName: orgRoot.orgRootShortName, posMasters: await Promise.all( - orgRoot.posMasters.map(async (posMaster) => { - // let posMasterOld: any = null; - // if (posMaster.next_holder != null) { - // // posMasterOld = posMasterOlds.find((posMasOld) => - // // posMasOld.current_holder == null - // // ? false - // // : posMasOld.current_holder.id === posMaster.next_holder.id, - // // ); - // } - // let positionOld: any = null; - // if (posMasterOld != null) - // positionOld = positionOlds.find( - // (positionold) => positionold.posMasterId === posMasterOld.id, - // ); - // const positionNow = await Promise.all( - // positions - // .filter((position) => position.posMasterId === posMaster.id) - // .map(async (position) => { - // return { - // positionName: position.positionName, - // positionField: position.positionField, - // posType: position.posType == null ? null : position.posType.posTypeName, - // posLevel: position.posLevel == null ? null : position.posLevel.posLevelName, - // posExecutive: - // position.posExecutive == null - // ? null - // : position.posExecutive.posExecutiveName, - // }; - // }), - // ); - const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; - const posType = [...new Set(posMaster.positions.map((x) => x.posType.posTypeName))]; - const posLevel = [ - ...new Set(posMaster.positions.map((x) => x.posLevel.posLevelName)), - ]; - const positionField = [...new Set(posMaster.positions.map((x) => x.positionField))]; - const positionMasterOld = posMaster.next_holder.current_holders.find( - (x) => x == orgRevisionActive, - ); - console.log(orgRevisionActive); - console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); - return { - posMasterNoPrefix: posMaster.posMasterNoPrefix, - posMasterNo: posMaster.posMasterNo, - posMasterNoSuffix: posMaster.posMasterNoSuffix, - profileFullname: - posMaster.next_holder == null - ? "- ว่าง -" - : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + orgRoot.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.map((x) => x.posType.posTypeName))]; + const posLevel = [ + ...new Set(posMaster.positions.map((x) => x.posLevel.posLevelName)), + ]; + const positionField = [...new Set(posMaster.positions.map((x) => x.positionField))]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } - // profilePosMasterNo: - // posMaster.next_holder == null - // ? posMaster.posMasterNo - // : positionMasterOld == null - // ? null - // : positionMasterOld.posMasterNo, - profilePositionName: - posMaster.next_holder == null - ? positionName.join(" หรือ ") - : posMaster.next_holder.position, - profilePosType: - posMaster.next_holder == null - ? posType.join(" หรือ ") - : posMaster.next_holder.posType == null - ? null - : posMaster.next_holder.posType.posTypeName, - profilePosLevel: - posMaster.next_holder == null - ? posLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null - ? null - : posMaster.next_holder.posLevel.posLevelName, - // profilePositionField: - // posMaster.next_holder == null - // ? positionField.join(" หรือ ") - // : positionMasterOld == null - // ? null - // : positionMasterOld.positions.find((x) => (x.positionIsSelected = true)) - // ?.positionField, + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePositionField: any = []; + if (posMaster.ancestorDNA != null) { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.posType.posTypeName)), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions.map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePositionField = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionField)), + ]; + } - // profilePosMasterNo: (posMasterOld == null ? null : posMasterOld.posMasterNo), - // profilePositionName: (positionOld == null ? null : positionOld.positionName), - // profilePosType: (positionOld == null ? null : positionOld.posType.posTypeName), - // profilePosLevel: (positionOld == null ? null : positionOld.posLevel.posLevelName), - // profilePositionField: (positionOld == null ? null : positionOld.positionField), - PositionName: positionName.join(" หรือ "), - PosType: posType.join(" หรือ "), - PosLevel: posLevel.join(" หรือ "), - PositionField: positionField.join(" หรือ "), - // positions: await Promise.all( - // positions - // .filter((position) => position.posMasterId === posMaster.id) - // .map(async (position) => { - // return { - // positionName: position.positionName, - // positionField: position.positionField, - // posType: position.posType == null ? null : position.posType.posTypeName, - // posLevel: - // position.posLevel == null ? null : position.posLevel.posLevelName, - // posExecutive: - // position.posExecutive == null - // ? null - // : position.posExecutive.posExecutiveName, - // }; - // }), - // ), - }; - }), + return { + posMasterOrder: posMaster.posMasterOrder, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + profileFullname: + posMaster.next_holder == null + ? "- ว่าง -" + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + positionMasterProfileOld == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld.posMasterNo, + + profilePositionName: + posMaster.next_holder == null + ? profilePositionName.length > 0 + ? profilePositionName.join(" หรือ ") + : positionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? profilePosType.length > 0 + ? profilePosType.join(" หรือ ") + : posType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? profilePosLevel.length > 0 + ? profilePosLevel.join(" หรือ ") + : posLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + profilePositionField: + posMaster.next_holder == null + ? profilePositionField.length > 0 + ? profilePositionField.join(" หรือ ") + : positionField.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => (x.positionIsSelected = true), + )?.positionField, + + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + positionField: + posMaster.next_holder == null + ? positionField.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => (x.positionIsSelected = true), + )?.positionField, + // positions: await Promise.all( + // positions + // .filter((position) => position.posMasterId === posMaster.id) + // .map(async (position) => { + // return { + // positionName: position.positionName, + // positionField: position.positionField, + // posType: position.posType == null ? null : position.posType.posTypeName, + // posLevel: + // position.posLevel == null ? null : position.posLevel.posLevelName, + // posExecutive: + // position.posExecutive == null + // ? null + // : position.posExecutive.posExecutiveName, + // }; + // }), + // ), + }; + }), ), }; data.push(node);