From 85121ffae8101bc585c491270912d82444da9ab4 Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 12 Nov 2025 16:35:18 +0700 Subject: [PATCH 1/4] #1948 , #1950 , #1951 --- src/controllers/CommandController.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index e4538538..29b9533c 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -4266,12 +4266,22 @@ export class CommandController extends Controller { body.data.map(async (item) => { const profile: any = await this.profileRepository.findOne({ where: { id: item.profileId }, - relations: ["roleKeycloaks"], + relations: ["roleKeycloaks","current_holders"], }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } + const orgRevision = await this.orgRevisionRepo.findOne({ + where: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + }); + + const orgRevisionRef = + profile?.current_holders?.find((x:any) => x.orgRevisionId == orgRevision?.id) ?? null; + //ลบตำแหน่งที่รักษาการแทน const code = _command?.commandType?.code; if (code && ["C-PM-13"].includes(code)) { @@ -4300,6 +4310,9 @@ export class CommandController extends Controller { lastUpdatedAt: new Date(), }; if (item.isLeave != undefined && item.isLeave == true) { + if(orgRevisionRef){ + await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); + } await removeProfileInOrganize(profile.id, "OFFICER"); } const clearProfile = await checkCommandType(String(item.commandId)); @@ -4628,8 +4641,8 @@ export class CommandController extends Controller { orgRevisionIsDraft: false, }, }); - // const orgRevisionRef = - // profile?.current_holders?.find((x) => x.orgRevisionId == orgRevision?.id) ?? null; + const orgRevisionRef = + profile?.current_holders?.find((x) => x.orgRevisionId == orgRevision?.id) ?? null; // const orgRootRef = orgRevisionRef?.orgRoot ?? null; // const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null; // const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null; @@ -4770,6 +4783,9 @@ export class CommandController extends Controller { _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = exceptClear.LeaveType ?? _null; } else { + if(orgRevisionRef){ + await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); + } await removeProfileInOrganize(_profile.id, "OFFICER"); } } From 043a2d344efdb6a2132439150ddc83bce9dc16e0 Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 12 Nov 2025 17:28:32 +0700 Subject: [PATCH 2/4] fix --- src/controllers/CommandController.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 29b9533c..42aab934 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -4310,9 +4310,9 @@ export class CommandController extends Controller { lastUpdatedAt: new Date(), }; if (item.isLeave != undefined && item.isLeave == true) { - if(orgRevisionRef){ - await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); - } + // if(orgRevisionRef){ + // await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); + // } await removeProfileInOrganize(profile.id, "OFFICER"); } const clearProfile = await checkCommandType(String(item.commandId)); From 3d689ff19c01ff511f67e7a0d7bb9852d61ed752 Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 12 Nov 2025 17:50:42 +0700 Subject: [PATCH 3/4] log --- src/controllers/CommandController.ts | 8 +++++--- src/interfaces/utils.ts | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 42aab934..e7f883d0 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -4310,10 +4310,12 @@ export class CommandController extends Controller { lastUpdatedAt: new Date(), }; if (item.isLeave != undefined && item.isLeave == true) { - // if(orgRevisionRef){ - // await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); - // } + if(orgRevisionRef){ + await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); + } + console.log("A"); await removeProfileInOrganize(profile.id, "OFFICER"); + console.log("Z"); } const clearProfile = await checkCommandType(String(item.commandId)); const _null: any = null; diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index 12636a36..f5c9e608 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -253,65 +253,73 @@ export function calculateRetireYear(birthDate: Date) { return yy + 61; } export async function removeProfileInOrganize(profileId: string, type: string) { + console.log(1); const currentRevision = await AppDataSource.getRepository(OrgRevision) .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - + console.log(2); const draftRevision = await AppDataSource.getRepository(OrgRevision) .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = true") .andWhere("orgRevision.orgRevisionIsCurrent = false") .getOne(); - + console.log(3); if (!currentRevision && !draftRevision) { + console.log(4); return; } if (type === "OFFICER") { + console.log(5); const findProfileInposMaster = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id }) .andWhere("posMaster.current_holderId = :profileId", { profileId }) .getOne(); - + console.log(6); await AppDataSource.getRepository(PosMaster) .createQueryBuilder() .update(PosMaster) .set({ current_holderId: null }) .where("id = :id", { id: findProfileInposMaster?.id }) .execute(); - + console.log(7); const findProfileInposMasterDraft = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: draftRevision?.id }) .andWhere("posMaster.next_holderId = :profileId", { profileId }) .getOne(); - + console.log(8); await AppDataSource.getRepository(PosMaster) .createQueryBuilder() .update(PosMaster) .set({ next_holderId: null }) .where("id = :id", { id: findProfileInposMasterDraft?.id }) .execute(); - + console.log(9); if (!findProfileInposMaster && !findProfileInposMasterDraft) { + console.log(10); return; } + console.log(11); const findPosition = await AppDataSource.getRepository(Position) .createQueryBuilder("position") .where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id }) .getMany(); - + console.log(12); if (!findPosition) { + console.log(13); return; } + console.log(13); await AppDataSource.getRepository(Position) .createQueryBuilder() .update(Position) .set({ positionIsSelected: false }) .where("id IN (:...ids)", { ids: findPosition.map((item) => item.id) }) .execute(); + console.log(14); } if (type === "EMPLOYEE") { const findProfileInEmpPosMaster = await AppDataSource.getRepository(EmployeePosMaster) From 62750576d385ceb7c08523da1f378ae3c9801fcc Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 12 Nov 2025 18:02:28 +0700 Subject: [PATCH 4/4] update --- src/controllers/CommandController.ts | 2 -- src/interfaces/utils.ts | 22 +++++++--------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index e7f883d0..29b9533c 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -4313,9 +4313,7 @@ export class CommandController extends Controller { if(orgRevisionRef){ await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); } - console.log("A"); await removeProfileInOrganize(profile.id, "OFFICER"); - console.log("Z"); } const clearProfile = await checkCommandType(String(item.commandId)); const _null: any = null; diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index f5c9e608..12636a36 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -253,73 +253,65 @@ export function calculateRetireYear(birthDate: Date) { return yy + 61; } export async function removeProfileInOrganize(profileId: string, type: string) { - console.log(1); const currentRevision = await AppDataSource.getRepository(OrgRevision) .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - console.log(2); + const draftRevision = await AppDataSource.getRepository(OrgRevision) .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = true") .andWhere("orgRevision.orgRevisionIsCurrent = false") .getOne(); - console.log(3); + if (!currentRevision && !draftRevision) { - console.log(4); return; } if (type === "OFFICER") { - console.log(5); const findProfileInposMaster = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id }) .andWhere("posMaster.current_holderId = :profileId", { profileId }) .getOne(); - console.log(6); + await AppDataSource.getRepository(PosMaster) .createQueryBuilder() .update(PosMaster) .set({ current_holderId: null }) .where("id = :id", { id: findProfileInposMaster?.id }) .execute(); - console.log(7); + const findProfileInposMasterDraft = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: draftRevision?.id }) .andWhere("posMaster.next_holderId = :profileId", { profileId }) .getOne(); - console.log(8); + await AppDataSource.getRepository(PosMaster) .createQueryBuilder() .update(PosMaster) .set({ next_holderId: null }) .where("id = :id", { id: findProfileInposMasterDraft?.id }) .execute(); - console.log(9); + if (!findProfileInposMaster && !findProfileInposMasterDraft) { - console.log(10); return; } - console.log(11); const findPosition = await AppDataSource.getRepository(Position) .createQueryBuilder("position") .where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id }) .getMany(); - console.log(12); + if (!findPosition) { - console.log(13); return; } - console.log(13); await AppDataSource.getRepository(Position) .createQueryBuilder() .update(Position) .set({ positionIsSelected: false }) .where("id IN (:...ids)", { ids: findPosition.map((item) => item.id) }) .execute(); - console.log(14); } if (type === "EMPLOYEE") { const findProfileInEmpPosMaster = await AppDataSource.getRepository(EmployeePosMaster)