diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 99b90bc5..1d4ef30a 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -1912,6 +1912,346 @@ export class OrganizationDotnetController extends Controller { return new HttpSuccess(mapProfile); } + // เพิ่มที่อยู่ปัจจุบัน + ตำแหน่งหัวหน้า + @Get("by-keycloak2/{keycloakId}") + async NewGetProfileByKeycloak2IdAsync(@Path() keycloakId: string) { + /* ========================= + * 1. Load profile + * ========================= */ + const profile = await this.profileRepo.findOne({ + where: { keycloak: keycloakId }, + relations: { + posLevel: true, + posType: true, + currentProvince: true, + currentDistrict: true, + currentSubDistrict: true, + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }, + }); + let commanderFullname = ""; + let commanderPositionName = ""; + let commanderId = ""; + // Employee + if (!profile) { + const profile = await this.profileEmpRepo.findOne({ + where: { keycloak: keycloakId }, + relations: { + posLevel: true, + posType: true, + currentProvince: true, + currentDistrict: true, + currentSubDistrict: true, + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }, + }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const currentHolder = profile.current_holders?.find( + x => + x.orgRevision?.orgRevisionIsDraft === false && + x.orgRevision?.orgRevisionIsCurrent === true, + ); + + const pos = await this.empPosMasterRepository + .createQueryBuilder("pos") + .leftJoinAndSelect("pos.current_holder", "holder") + .leftJoin("pos.orgRevision", "rev") + .where("rev.orgRevisionIsCurrent = true") + .andWhere("rev.orgRevisionIsDraft = false") + .andWhere("pos.isDirector = true") + .andWhere("pos.current_holderId IS NOT NULL") + .andWhere("pos.orgRootId = :root", { root: currentHolder?.orgRootId }) + .andWhere( + `(pos.orgChild1Id = :c1 OR pos.orgChild1Id IS NULL) + AND (pos.orgChild2Id = :c2 OR pos.orgChild2Id IS NULL) + AND (pos.orgChild3Id = :c3 OR pos.orgChild3Id IS NULL) + AND (pos.orgChild4Id = :c4 OR pos.orgChild4Id IS NULL)`, + { + c1: currentHolder?.orgChild1Id, + c2: currentHolder?.orgChild2Id, + c3: currentHolder?.orgChild3Id, + c4: currentHolder?.orgChild4Id, + }, + ) + .orderBy( + `(CASE WHEN pos.orgChild4Id IS NULL THEN 1 ELSE 0 END) + + (CASE WHEN pos.orgChild3Id IS NULL THEN 1 ELSE 0 END) + + (CASE WHEN pos.orgChild2Id IS NULL THEN 1 ELSE 0 END) + + (CASE WHEN pos.orgChild1Id IS NULL THEN 1 ELSE 0 END)`, + "ASC", + ) + .getOne(); + + if (pos?.current_holder) { + commanderFullname = + `${pos.current_holder.prefix}${pos.current_holder.firstName} ${pos.current_holder.lastName}`; + commanderPositionName = pos.current_holder.position; + commanderId = pos.current_holder.id; + } + + let oc = ""; + if (currentHolder) { + if (!currentHolder.orgChild1Id) { + oc = currentHolder.orgRoot?.orgRootName; + } else if (!currentHolder.orgChild2Id) { + oc = `${currentHolder.orgChild1?.orgChild1Name} ${currentHolder.orgRoot?.orgRootName}`; + } else if (!currentHolder.orgChild3Id) { + oc = `${currentHolder.orgChild2?.orgChild2Name} ${currentHolder.orgChild1?.orgChild1Name} ${currentHolder.orgRoot?.orgRootName}`; + } else if (!currentHolder.orgChild4Id) { + oc = `${currentHolder.orgChild3?.orgChild3Name} ${currentHolder.orgChild2?.orgChild2Name} ${currentHolder.orgChild1?.orgChild1Name} ${currentHolder.orgRoot?.orgRootName}`; + } else { + oc = currentHolder.orgChild4?.orgChild4Name; + } + } + + const mapProfile = { + profileType: "EMPLOYEE", + id: profile.id, + keycloak: profile.keycloak, + prefix: profile.prefix, + firstName: profile.firstName, + lastName: profile.lastName, + citizenId: profile.citizenId, + position: profile.position, + email: profile.email, + phone: profile.phone, + isProbation: profile.isProbation, + isLeave: profile.isLeave, + dateRetire: profile.dateRetire, + dateAppoint: profile.dateAppoint, + dateRetireLaw: profile.dateRetireLaw, + dateStart: profile.dateStart, + govAgeAbsent: profile.govAgeAbsent, + govAgePlus: profile.govAgePlus, + birthDate: profile.birthDate ?? new Date(), + reasonSameDate: profile.reasonSameDate, + telephoneNumber: profile.phone, + nationality: profile.nationality, + gender: profile.gender, + relationship: profile.relationship, + religion: profile.religion, + bloodGroup: profile.bloodGroup, + dutyTimeId: profile.dutyTimeId, + dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate, + amount: profile.amount, + positionSalaryAmount: profile.positionSalaryAmount, + mouthSalaryAmount: profile.mouthSalaryAmount, + + posType: profile.posType?.posTypeName ?? null, + posLevel: profile.posType?.posTypeShortName == null && profile.posLevel?.posLevelName == null + ? null + : `${profile.posType?.posTypeShortName} ${profile.posLevel?.posLevelName}`, + oc, + + currentAddress: profile.currentAddress, + currentSubDistrict: profile.currentSubDistrict?.name ?? null, + currentDistrict: profile.currentDistrict?.name ?? null, + currentProvince: profile.currentProvince?.name ?? null, + currentZipCode: profile.currentZipCode, + + commander: commanderFullname, + commanderPositionName, + commanderId, + + root: currentHolder?.orgRoot?.orgRootName ?? null, + rootId: currentHolder?.orgRootId ?? null, + rootDnaId: currentHolder?.orgRoot?.ancestorDNA ?? null, + child1: currentHolder?.orgChild1?.orgChild1Name ?? null, + child1Id: currentHolder?.orgChild1Id ?? null, + child1DnaId: currentHolder?.orgChild1?.ancestorDNA ?? null, + child2: currentHolder?.orgChild2?.orgChild2Name ?? null, + child2Id: currentHolder?.orgChild2Id ?? null, + child2DnaId: currentHolder?.orgChild2?.ancestorDNA ?? null, + child3: currentHolder?.orgChild3?.orgChild3Name ?? null, + child3Id: currentHolder?.orgChild3Id ?? null, + child3DnaId: currentHolder?.orgChild3?.ancestorDNA ?? null, + child4: currentHolder?.orgChild4?.orgChild4Name ?? null, + child4Id: currentHolder?.orgChild4Id ?? null, + child4DnaId: currentHolder?.orgChild4?.ancestorDNA ?? null, + }; + + return new HttpSuccess(mapProfile); + } + + /* ========================================= + * 2. current holder + * ========================================= */ + const currentHolder = profile.current_holders?.find( + x => + x.orgRevision?.orgRevisionIsDraft === false && + x.orgRevision?.orgRevisionIsCurrent === true, + ); + + /* ================================================= + * 3. หา commander + * ================================================= */ + const pos = await this.posMasterRepository + .createQueryBuilder("pos") + .leftJoinAndSelect("pos.current_holder", "holder") + .leftJoin("pos.orgRevision", "rev") + .where("rev.orgRevisionIsCurrent = true") + .andWhere("rev.orgRevisionIsDraft = false") + .andWhere("pos.isDirector = true") + .andWhere("pos.current_holderId IS NOT NULL") + .andWhere("pos.orgRootId = :root", { root: currentHolder?.orgRootId }) + .andWhere( + `(pos.orgChild1Id = :c1 OR pos.orgChild1Id IS NULL) + AND (pos.orgChild2Id = :c2 OR pos.orgChild2Id IS NULL) + AND (pos.orgChild3Id = :c3 OR pos.orgChild3Id IS NULL) + AND (pos.orgChild4Id = :c4 OR pos.orgChild4Id IS NULL)`, + { + c1: currentHolder?.orgChild1Id, + c2: currentHolder?.orgChild2Id, + c3: currentHolder?.orgChild3Id, + c4: currentHolder?.orgChild4Id, + }, + ) + .orderBy( + `(CASE WHEN pos.orgChild4Id IS NULL THEN 1 ELSE 0 END) + + (CASE WHEN pos.orgChild3Id IS NULL THEN 1 ELSE 0 END) + + (CASE WHEN pos.orgChild2Id IS NULL THEN 1 ELSE 0 END) + + (CASE WHEN pos.orgChild1Id IS NULL THEN 1 ELSE 0 END)`, + "ASC", + ) + .getOne(); + + if (pos?.current_holder) { + commanderFullname = + `${pos.current_holder.prefix}${pos.current_holder.firstName} ${pos.current_holder.lastName}`; + commanderPositionName = pos.current_holder.position; + commanderId = pos.current_holder.id; + } + + /* ========================================= + * 5. position executive + * ========================================= */ + const position = await this.positionRepository.findOne({ + where: { + positionIsSelected: true, + posMaster: { + orgRevisionId: currentHolder?.orgRevisionId, + current_holderId: profile.id, + }, + }, + order: { createdAt: "DESC" }, + relations: { posExecutive: true }, + }); + + /* ========================================= + * 6. OC name + * ========================================= */ + let oc = ""; + if (currentHolder) { + if (!currentHolder.orgChild1Id) { + oc = currentHolder.orgRoot?.orgRootName; + } else if (!currentHolder.orgChild2Id) { + oc = `${currentHolder.orgChild1?.orgChild1Name} ${currentHolder.orgRoot?.orgRootName}`; + } else if (!currentHolder.orgChild3Id) { + oc = `${currentHolder.orgChild2?.orgChild2Name} ${currentHolder.orgChild1?.orgChild1Name} ${currentHolder.orgRoot?.orgRootName}`; + } else if (!currentHolder.orgChild4Id) { + oc = `${currentHolder.orgChild3?.orgChild3Name} ${currentHolder.orgChild2?.orgChild2Name} ${currentHolder.orgChild1?.orgChild1Name} ${currentHolder.orgRoot?.orgRootName}`; + } else { + oc = currentHolder.orgChild4?.orgChild4Name; + } + } + + /* ========================================= + * 7. position level name + * ========================================= */ + const positionLeaveName = + profile.posType && + profile.posLevel && + (profile.posType.posTypeName === "บริหาร" || + profile.posType.posTypeName === "อำนวยการ") + ? `${profile.posType.posTypeName}${profile.posLevel.posLevelName}` + : profile.posLevel?.posLevelName ?? null; + + /* ========================================= + * 8. map response + * ========================================= */ + const mapProfile = { + profileType: "OFFICER", + id: profile.id, + keycloak: profile.keycloak, + prefix: profile.prefix, + firstName: profile.firstName, + lastName: profile.lastName, + citizenId: profile.citizenId, + position: profile.position, + email: profile.email, + phone: profile.phone, + isProbation: profile.isProbation, + isLeave: profile.isLeave, + dateRetire: profile.dateRetire, + dateAppoint: profile.dateAppoint, + dateRetireLaw: profile.dateRetireLaw, + dateStart: profile.dateStart, + govAgeAbsent: profile.govAgeAbsent, + govAgePlus: profile.govAgePlus, + birthDate: profile.birthDate ?? new Date(), + reasonSameDate: profile.reasonSameDate, + telephoneNumber: profile.phone, + nationality: profile.nationality, + gender: profile.gender, + relationship: profile.relationship, + religion: profile.religion, + bloodGroup: profile.bloodGroup, + dutyTimeId: profile.dutyTimeId, + dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate, + amount: profile.amount, + positionSalaryAmount: profile.positionSalaryAmount, + mouthSalaryAmount: profile.mouthSalaryAmount, + + posLevel: profile.posLevel?.posLevelName ?? null, + posType: profile.posType?.posTypeName ?? null, + posExecutiveName: position?.posExecutive?.posExecutiveName ?? null, + positionLeaveName, + oc, + + currentAddress: profile.currentAddress, + currentSubDistrict: profile.currentSubDistrict?.name ?? null, + currentDistrict: profile.currentDistrict?.name ?? null, + currentProvince: profile.currentProvince?.name ?? null, + currentZipCode: profile.currentZipCode, + + commander: commanderFullname, + commanderPositionName, + commanderId, + + root: currentHolder?.orgRoot?.orgRootName ?? null, + rootId: currentHolder?.orgRootId ?? null, + rootDnaId: currentHolder?.orgRoot?.ancestorDNA ?? null, + child1: currentHolder?.orgChild1?.orgChild1Name ?? null, + child1Id: currentHolder?.orgChild1Id ?? null, + child1DnaId: currentHolder?.orgChild1?.ancestorDNA ?? null, + child2: currentHolder?.orgChild2?.orgChild2Name ?? null, + child2Id: currentHolder?.orgChild2Id ?? null, + child2DnaId: currentHolder?.orgChild2?.ancestorDNA ?? null, + child3: currentHolder?.orgChild3?.orgChild3Name ?? null, + child3Id: currentHolder?.orgChild3Id ?? null, + child3DnaId: currentHolder?.orgChild3?.ancestorDNA ?? null, + child4: currentHolder?.orgChild4?.orgChild4Name ?? null, + child4Id: currentHolder?.orgChild4Id ?? null, + child4DnaId: currentHolder?.orgChild4?.ancestorDNA ?? null, + }; + + return new HttpSuccess(mapProfile); + } + /** * API Get Profile For Logs *