diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 29f6b19c..63cc937b 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1657,7 +1657,8 @@ export class CommandController extends Controller { // console.log("4. disable keycloak/authen") const delUserKeycloak = await deleteUser(_profile.keycloak, adminToken); if (delUserKeycloak) { - _profile.keycloak = ""; + // Task #228 + // _profile.keycloak = ""; _profile.roleKeycloaks = []; } } @@ -1713,7 +1714,8 @@ export class CommandController extends Controller { // disable keycloak/authen const delUserKeycloak = await deleteUser(_profileEmp.keycloak, adminToken); if (delUserKeycloak) { - _profileEmp.keycloak = ""; + // Task #228 + // _profileEmp.keycloak = ""; _profileEmp.roleKeycloaks = []; } } @@ -4131,7 +4133,8 @@ export class CommandController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { - profile.keycloak = _null; + // Task #228 + // profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } @@ -4535,7 +4538,8 @@ export class CommandController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { - profile.keycloak = _null; + // Task #228 + // profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } @@ -4755,7 +4759,8 @@ export class CommandController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { - profile.keycloak = _null; + // Task #228 + // profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } @@ -5251,7 +5256,8 @@ export class CommandController extends Controller { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { - _profile.keycloak = _null; + // Task #228 + // _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } @@ -5434,7 +5440,8 @@ export class CommandController extends Controller { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { - _profile.keycloak = _null; + // Task #228 + // _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } @@ -5772,7 +5779,8 @@ export class CommandController extends Controller { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { - _profile.keycloak = _null; + // Task #228 + // _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } @@ -6209,7 +6217,8 @@ export class CommandController extends Controller { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { - _profile.keycloak = _null; + // Task #228 + // _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } @@ -6506,7 +6515,8 @@ export class CommandController extends Controller { if (profileEmployee.keycloak != null) { // const delUserKeycloak = await deleteUser(profileEmployee.keycloak); // if (delUserKeycloak) { - profileEmployee.keycloak = _null; + // Task #228 + // profileEmployee.keycloak = _null; profileEmployee.roleKeycloaks = []; profileEmployee.isActive = false; // } @@ -6837,12 +6847,36 @@ export class CommandController extends Controller { } //Position if (item.bodyPosition && item.bodyPosition != null) { - const posMaster = await this.posMasterRepository.findOne({ - where: { id: item.bodyPosition.posmasterId }, + // STEP 1: หา posMaster ที่จะใช้งานตาม id ที่ส่งมา (อาจเป็นตำแหน่งเก่าหรือใหม่ก็ได้) + let posMaster = await this.posMasterRepository.findOne({ + where: { + id: item.bodyPosition.posmasterId, + }, + relations: { orgRevision: true } }); + + // เช็คว่า posMaster ที่หามาอยู่ในโครงสร้างปัจจุบันหรือไม่ + const isCurrent = posMaster?.orgRevision?.orgRevisionIsCurrent === true && + posMaster?.orgRevision?.orgRevisionIsDraft === false; + + // ถ้าไม่อยู่ในโครงสร้างปัจจุบัน ให้หาตัวใหม่จาก ancestorDNA + if (!isCurrent && posMaster?.ancestorDNA) { + posMaster = await this.posMasterRepository.findOne({ + where: { + ancestorDNA: posMaster.ancestorDNA, + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false + } + }, + relations: { orgRevision: true } + }); + } + if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + // STEP 2: เคลียร์ข้อมูลตำแหน่งเก่าที่ครองอยู่ ในโครงสร้างปัจจุบัน const posMasterOld = await this.posMasterRepository.findOne({ where: { current_holderId: profile.id, @@ -6850,10 +6884,12 @@ export class CommandController extends Controller { }, }); if (posMasterOld != null) { + // เคลียร์คนครองเก่าออกจากตำแหน่งเดิม posMasterOld.current_holderId = null; posMasterOld.lastUpdatedAt = new Date(); } + // หา position เก่าที่เลือกไว้ แล้วเคลียร์การเลือก const positionOld = await this.positionRepository.findOne({ where: { posMasterId: posMasterOld?.id, @@ -6865,9 +6901,10 @@ export class CommandController extends Controller { await this.positionRepository.save(positionOld); } + // STEP 3: เคลียร์ position ที่เลือกไว้อื่นๆ ใน posMaster ตัวใหม่ const checkPosition = await this.positionRepository.find({ where: { - posMasterId: item.bodyPosition.posmasterId, + posMasterId: posMaster.id, positionIsSelected: true, }, }); @@ -6879,6 +6916,7 @@ export class CommandController extends Controller { await this.positionRepository.save(clearPosition); } + // STEP 4: กำหนดคนครองใหม่ให้กับ posMaster posMaster.current_holderId = profile.id; posMaster.lastUpdatedAt = new Date(); // posMaster.conditionReason = _null; @@ -6889,10 +6927,11 @@ export class CommandController extends Controller { } await this.posMasterRepository.save(posMaster); + // STEP 5: กำหนด position ใหม่ const positionNew = await this.positionRepository.findOne({ where: { id: item.bodyPosition.positionId, - posMasterId: item.bodyPosition.posmasterId, + posMasterId: posMaster.id, // ใช้ id ของ posMaster ตัวใหม่ }, }); // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index b49581d0..18e3f96c 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -11133,9 +11133,9 @@ export class ProfileController extends Controller { } /** - * API อัพเดทเกษียณ + * API อัพเดทถึงแก่กรรม * - * @summary อัพเดทเกษียณ (ADMIN) + * @summary อัพเดทถึงแก่กรรม (ADMIN) * * @param {string} id Id ทะเบียนประวัติ */ @@ -11263,7 +11263,8 @@ export class ProfileController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { - profile.keycloak = _null; + // Task #228 + // profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } @@ -11371,7 +11372,7 @@ export class ProfileController extends Controller { .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") - .where("profile.keycloak IS NULL") + .where("profile.isActive = :isActive", { isActive: false }) .andWhere( new Brackets((qb) => { qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index a1ac02af..ffdb2085 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -5560,9 +5560,9 @@ export class ProfileEmployeeController extends Controller { } /** - * API อัพเดทเกษียณ + * API อัพเดทถึงแก่กรรม * - * @summary อัพเดทเกษียณ (ADMIN) + * @summary อัพเดทถึงแก่กรรม (ADMIN) * * @param {string} id Id ทะเบียนประวัติ */ @@ -5687,7 +5687,8 @@ export class ProfileEmployeeController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { - profile.keycloak = _null; + // Task #228 + // profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } @@ -6161,7 +6162,7 @@ export class ProfileEmployeeController extends Controller { .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") - .where("profile.keycloak IS NULL") + .where("profile.isActive = :isActive", { isActive: false }) .andWhere( new Brackets((qb) => { qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index 7930b872..a864710d 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -3459,9 +3459,9 @@ export class ProfileEmployeeTempController extends Controller { } /** - * API อัพเดทเกษียณ + * API อัพเดทถึงแก่กรรม * - * @summary อัพเดทเกษียณ (ADMIN) + * @summary อัพเดทถึงแก่กรรม (ADMIN) * * @param {string} id Id ทะเบียนประวัติ */ @@ -3586,7 +3586,8 @@ export class ProfileEmployeeTempController extends Controller { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { - profile.keycloak = _null; + // Task #228 + // profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } @@ -3987,7 +3988,7 @@ export class ProfileEmployeeTempController extends Controller { case "citizenId": [findProfile, total] = await this.profileRepo.findAndCount({ where: { - keycloak: IsNull(), + isActive: false, citizenId: Like(`%${body.keyword}%`), }, relations: ["posType", "posLevel", "current_holders"], @@ -3999,7 +4000,7 @@ export class ProfileEmployeeTempController extends Controller { case "firstname": [findProfile, total] = await this.profileRepo.findAndCount({ where: { - keycloak: IsNull(), + isActive: false, firstName: Like(`%${body.keyword}%`), }, relations: ["posType", "posLevel", "current_holders"], @@ -4011,7 +4012,7 @@ export class ProfileEmployeeTempController extends Controller { case "lastname": [findProfile, total] = await this.profileRepo.findAndCount({ where: { - keycloak: IsNull(), + isActive: false, lastName: Like(`%${body.keyword}%`), }, relations: ["posType", "posLevel", "current_holders"], @@ -4023,7 +4024,7 @@ export class ProfileEmployeeTempController extends Controller { default: [findProfile, total] = await this.profileRepo.findAndCount({ where: { - keycloak: IsNull(), + isActive: false, }, relations: ["posType", "posLevel", "current_holders"], skip, diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 9889bd9b..52b97622 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -274,14 +274,16 @@ export class KeycloakController extends Controller { }); if (!profileEmp) { } else { - const _null: any = null; - profileEmp.keycloak = _null; + // Task #228 + // const _null: any = null; + // profileEmp.keycloak = _null; profileEmp.roleKeycloaks = []; await this.profileEmpRepo.save(profileEmp); } } else { - const _null: any = null; - profile.keycloak = _null; + // Task #228 + // const _null: any = null; + // profile.keycloak = _null; profile.roleKeycloaks = []; await this.profileRepo.save(profile); return new HttpSuccess(); @@ -566,7 +568,8 @@ export class KeycloakController extends Controller { .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") - .where("profile.keycloak IS NOT NULL AND profile.keycloak != ''") + // .where("profile.keycloak IS NOT NULL AND profile.keycloak != ''") + .where("profile.isActive = :isActive", { isActive: true }) .andWhere(checkChildFromRole) .andWhere(conditions) .andWhere( @@ -609,7 +612,8 @@ export class KeycloakController extends Controller { .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") - .where("profileEmployee.keycloak IS NOT NULL AND profileEmployee.keycloak != ''") + // .where("profileEmployee.keycloak IS NOT NULL AND profileEmployee.keycloak != ''") + .where("profileEmployee.isActive = :isActive", { isActive: true }) .andWhere(checkChildFromRole) .andWhere(conditions) .andWhere({ employeeClass: "PERM" })