From d553c1406c6520b19d64affae3b979d65c40bdee Mon Sep 17 00:00:00 2001 From: harid Date: Wed, 1 Apr 2026 17:50:38 +0700 Subject: [PATCH 1/2] =?UTF-8?q?[Fix]=20=E0=B8=81=E0=B8=A3=E0=B8=93?= =?UTF-8?q?=E0=B8=B5=E0=B8=9E=E0=B9=89=E0=B8=99=E0=B8=A3=E0=B8=B2=E0=B8=8A?= =?UTF-8?q?=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84?= =?UTF-8?q?=E0=B8=82=E0=B9=80=E0=B8=9B=E0=B9=87=E0=B8=99=E0=B9=84=E0=B8=A1?= =?UTF-8?q?=E0=B9=88=E0=B8=95=E0=B9=89=E0=B8=AD=E0=B8=87=20clear=20?= =?UTF-8?q?=E0=B8=9F=E0=B8=B4=E0=B8=A5=E0=B8=94=E0=B9=8C=20keycloak=20#228?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 30 ++++++++++++------- src/controllers/ProfileController.ts | 9 +++--- src/controllers/ProfileEmployeeController.ts | 9 +++--- .../ProfileEmployeeTempController.ts | 15 +++++----- src/controllers/UserController.ts | 16 ++++++---- 5 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 016b4768..2993f480 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 = []; } } @@ -4128,7 +4130,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; } @@ -4532,7 +4535,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; } @@ -4752,7 +4756,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; } @@ -5248,7 +5253,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; } @@ -5431,7 +5437,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; } @@ -5769,7 +5776,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; } @@ -6206,7 +6214,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; } @@ -6503,7 +6512,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; // } 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" }) From b69f8a6c085ab7cd0b8aa723c58f15759a4c9a64 Mon Sep 17 00:00:00 2001 From: harid Date: Thu, 2 Apr 2026 11:27:39 +0700 Subject: [PATCH 2/2] =?UTF-8?q?fix=20=E0=B8=AA=E0=B9=88=E0=B8=87=E0=B8=AD?= =?UTF-8?q?=E0=B8=AD=E0=B8=81=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88?= =?UTF-8?q?=E0=B8=87=E0=B8=A3=E0=B8=B2=E0=B8=A2=E0=B8=8A=E0=B8=B7=E0=B9=88?= =?UTF-8?q?=E0=B8=AD=E0=B8=9C=E0=B8=B9=E0=B9=89=E0=B8=AA=E0=B8=AD=E0=B8=9A?= =?UTF-8?q?=E0=B8=9C=E0=B9=88=E0=B8=B2=E0=B8=99=20=E0=B8=A3=E0=B8=B2?= =?UTF-8?q?=E0=B8=A2=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B9=84=E0=B8=A1?= =?UTF-8?q?=E0=B9=88=E0=B9=81=E0=B8=AA=E0=B8=94=E0=B8=87=E0=B9=83=E0=B8=99?= =?UTF-8?q?=E0=B9=82=E0=B8=84=E0=B8=A3=E0=B8=87=E0=B8=AA=E0=B8=A3=E0=B9=89?= =?UTF-8?q?=E0=B8=B2=E0=B8=87=20#2402?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 37 +++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 2993f480..6e7cb637 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -6844,12 +6844,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, @@ -6857,10 +6881,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, @@ -6872,9 +6898,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, }, }); @@ -6886,6 +6913,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; @@ -6896,10 +6924,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 ตัวใหม่ }, }); if (positionNew != null) {