From 15830ef2ba97364ced1c0142abb024fcc19e5ab8 Mon Sep 17 00:00:00 2001 From: harid Date: Mon, 18 May 2026 15:02:38 +0700 Subject: [PATCH] =?UTF-8?q?fix=20=E0=B8=A2=E0=B8=A8=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=B8=A3=E0=B8=B0=E0=B8=9A=E0=B8=9A=E0=B8=97=E0=B8=B0=E0=B9=80?= =?UTF-8?q?=E0=B8=9A=E0=B8=B5=E0=B8=A2=E0=B8=99=20#2469=20+=20Error=20Log?= =?UTF-8?q?=20api=20check-keycloak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 21 ++- .../OrganizationDotnetController.ts | 136 +++++++++--------- 2 files changed, 81 insertions(+), 76 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index d50a3431..fcadf77c 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -6594,9 +6594,9 @@ export class CommandController extends Controller { profile.amountSpecial = item.bodyProfile.amountSpecial ?? null; profile.isProbation = item.bodyProfile.isProbation; //เพิ่มใหม่จากรับโอน - profile.rank = item.bodyProfile.rank ?? null; - profile.prefix = item.bodyProfile.prefix ?? null; - profile.prefixMain = item.bodyProfile.prefix ?? null; + profile.rank = item?.bodyProfile?.rank ?? null; + profile.prefix = item?.bodyProfile?.rank ?? item?.bodyProfile?.prefix ?? null; + profile.prefixMain = item?.bodyProfile?.prefix ?? null; profile.firstName = item.bodyProfile.firstName ?? null; profile.lastName = item.bodyProfile.lastName ?? null; profile.birthDate = item.bodyProfile.birthDate ?? null; @@ -6658,9 +6658,9 @@ export class CommandController extends Controller { profile.amount = item.bodyProfile.amount ?? null; profile.amountSpecial = item.bodyProfile.amountSpecial ?? null; profile.isProbation = item.bodyProfile.isProbation; - profile.rank = item.bodyProfile.rank ?? null; - profile.prefix = item.bodyProfile.prefix ?? null; - profile.prefixMain = item.bodyProfile.prefix ?? null; + profile.rank = item?.bodyProfile?.rank ?? null; + profile.prefix = item?.bodyProfile?.rank ?? item?.bodyProfile?.prefix ?? null; + profile.prefixMain = item?.bodyProfile?.prefix ?? null; profile.firstName = item.bodyProfile.firstName ?? null; profile.lastName = item.bodyProfile.lastName ?? null; profile.birthDate = item.bodyProfile.birthDate ?? null; @@ -6717,12 +6717,9 @@ export class CommandController extends Controller { profile.lastUpdateFullName = req.user.name; profile.lastUpdatedAt = new Date(); //เพิ่มใหม่จากรับโอน - profile.rank = item.bodyProfile.rank ?? null; - profile.prefix = - item.bodyProfile.prefix && item.bodyProfile.prefix != "" - ? item.bodyProfile.prefix - : profile.prefix; - profile.prefixMain = item.bodyProfile.prefix ?? null; + profile.rank = item?.bodyProfile?.rank ?? null; + profile.prefix = item?.bodyProfile?.rank ?? item?.bodyProfile?.prefix ?? null; + profile.prefixMain = item?.bodyProfile?.prefix ?? null; profile.firstName = item.bodyProfile.firstName && item.bodyProfile.firstName != "" ? item.bodyProfile.firstName diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 23255fe3..b26cf73c 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -2357,26 +2357,11 @@ export class OrganizationDotnetController extends Controller { */ @Get("check-keycloak/{keycloakId}") async GetProfileForProcessCheckInAsync(@Path() keycloakId: string) { - /* ========================= - * 1. Load profile (Officer) - * ========================= */ - const profile = await this.profileRepo.findOne({ - where: { keycloak: keycloakId }, - relations: { - current_holders: { - orgRevision: true, - orgRoot: true, - orgChild1: true, - orgChild2: true, - orgChild3: true, - orgChild4: true, - }, - }, - }); - - // Employee - if (!profile) { - const profile = await this.profileEmpRepo.findOne({ + try { + /* ========================= + * 1. Load profile (Officer) + * ========================= */ + const profile = await this.profileRepo.findOne({ where: { keycloak: keycloakId }, relations: { current_holders: { @@ -2389,15 +2374,72 @@ export class OrganizationDotnetController extends Controller { }, }, }); - if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + // Employee + if (!profile) { + const empProfile = await this.profileEmpRepo.findOne({ + where: { keycloak: keycloakId }, + relations: { + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }, + }); + if (!empProfile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const currentHolder = empProfile.current_holders?.find( + (x) => + x.orgRevision?.orgRevisionIsDraft === false && + x.orgRevision?.orgRevisionIsCurrent === true, + ); + + const mapProfile = { + profileType: "EMPLOYEE", + id: empProfile.id, + keycloak: empProfile.keycloak, + prefix: empProfile.prefix, + firstName: empProfile.firstName, + lastName: empProfile.lastName, + citizenId: empProfile.citizenId, + gender: empProfile.gender, + + 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 (Officer) + * ========================================= */ const currentHolder = profile.current_holders?.find( (x) => - x.orgRevision?.orgRevisionIsDraft === false && - x.orgRevision?.orgRevisionIsCurrent === true, + x.orgRevision?.orgRevisionIsDraft === false && x.orgRevision?.orgRevisionIsCurrent === true, ); + /* ========================================= + * 3. map response + * ========================================= */ const mapProfile = { - profileType: "EMPLOYEE", + profileType: "OFFICER", id: profile.id, keycloak: profile.keycloak, prefix: profile.prefix, @@ -2424,47 +2466,13 @@ export class OrganizationDotnetController extends Controller { }; return new HttpSuccess(mapProfile); + } catch (error: any) { + // Log เฉพาะ unexpected errors (ไม่ใช่ HttpError) + if (!(error instanceof HttpError)) { + console.error(`[check-keycloak] Unexpected error: keycloakId=${keycloakId}`, error); + } + throw error; } - - /* ========================================= - * 2. current holder (Officer) - * ========================================= */ - const currentHolder = profile.current_holders?.find( - (x) => - x.orgRevision?.orgRevisionIsDraft === false && x.orgRevision?.orgRevisionIsCurrent === true, - ); - - /* ========================================= - * 6. map response - * ========================================= */ - const mapProfile = { - profileType: "OFFICER", - id: profile.id, - keycloak: profile.keycloak, - prefix: profile.prefix, - firstName: profile.firstName, - lastName: profile.lastName, - citizenId: profile.citizenId, - gender: profile.gender, - - 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); } /**