From 1ade81a0481562475693824854344a73ab134c94 Mon Sep 17 00:00:00 2001 From: harid Date: Mon, 26 Jan 2026 15:42:56 +0700 Subject: [PATCH] =?UTF-8?q?Fix=20=E0=B9=80=E0=B8=A5=E0=B8=82=E0=B8=97?= =?UTF-8?q?=E0=B8=B5=E0=B9=88=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99?= =?UTF-8?q?=E0=B9=88=E0=B8=87=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B9=81=E0=B8=AA?= =?UTF-8?q?=E0=B8=94=E0=B8=87=20#2230=20=20+=20Add=20api=20=E0=B8=AA?= =?UTF-8?q?=E0=B8=B3=E0=B8=AB=E0=B8=A3=E0=B8=B1=E0=B8=9A=E0=B9=83=E0=B8=9A?= =?UTF-8?q?=E0=B8=A5=E0=B8=B2=20#2233?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrganizationDotnetController.ts | 306 +++++++++++++++++- 1 file changed, 305 insertions(+), 1 deletion(-) diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 688a593c..448bc6cb 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -39,6 +39,7 @@ import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; import { PosMasterAssign } from "../entities/PosMasterAssign"; import { Assign } from "../entities/Assign"; import { ProfileSalary } from "../entities/ProfileSalary"; +import { ProfileEducation } from "../entities/ProfileEducation"; @Route("api/v1/org/dotnet") @Tags("Dotnet") @Security("bearerAuth") @@ -67,6 +68,7 @@ export class OrganizationDotnetController extends Controller { private posMasterAssignRepo = AppDataSource.getRepository(PosMasterAssign); private assignRepository = AppDataSource.getRepository(Assign); private salaryRepo = AppDataSource.getRepository(ProfileSalary); + private educationRepo = AppDataSource.getRepository(ProfileEducation); /** * ทำไว้ให้ service อื่นๆ ภายในระบบ call มาตรวจสอบเลขบัตรประจำตัวประชาชน @@ -6788,6 +6790,308 @@ export class OrganizationDotnetController extends Controller { return new HttpSuccess(mapProfile); } + @Post("profile-leave/keycloak") + async GetProfileLeaveReportByKeycloakIdAsync( + @Body() body: { + keycloakId: string, + report?: string + } + ) { + const profile = await this.profileRepo.findOne({ + relations: { + currentProvince: true, + currentDistrict: true, + currentSubDistrict: true, + posLevel: true, + posType: true, + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }, + where: { + keycloak: body.keycloakId, + }, + }); + + // ลูกจ้างประจำ + if (!profile) { + const profile = await this.profileEmpRepo.findOne({ + relations: { + currentProvince: true, + currentDistrict: true, + currentSubDistrict: true, + posLevel: true, + posType: true, + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }, + where: { + keycloak: body.keycloakId, + } + }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + /* ========================================= + * current holder + * ========================================= */ + const currentHolder = profile.current_holders?.find( + x => + x.orgRevision?.orgRevisionIsDraft === false && + x.orgRevision?.orgRevisionIsCurrent === true, + ); + + 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; + } + } + let _positions: any[] = []; + let _educations: any[] = []; + if (body.report && ["LEAVE16"].includes(body.report.trim().toUpperCase())) { + + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; + if (profile && profile?.isLeave) { + _currentDate = + profile && profile.leaveDate ? Extension.toDateOnlyString(profile.leaveDate) : _currentDate; + } + const positions = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?, ?)", [ + profile!.id, + _currentDate, + ]); + _positions = positions[0].length > 0 + ? _positions.slice(0, -1).map((x: any, idx: number) => ({ + positionName: x.positionName, + dateStart: x.commandDateAffect ?? null, + dateEnd: _positions[idx + 1]?.commandDateAffect ?? null, + })) + : []; + + const profileEducations = await this.educationRepo.find({ + where: { profileEmployeeId: profile!.id }, + order: { level: "ASC" }, + }); + _educations = profileEducations.length > 0 + ? profileEducations.map((x) => ({ + educationLevel: x.educationLevel, + institute: x.institute ?? "-", + country: x.country ?? "-", + finishDate: x.finishDate, + })) + : []; + } + + const mapProfile = { + prefix: profile.prefix, + firstName: profile.firstName, + lastName: profile.lastName, + citizenId: profile.citizenId, + birthDate: profile.birthDate, + retireDate: profile.birthDate + ? calculateRetireLaw(profile.birthDate) + : null, + govAge: profile.dateAppoint + ? `${Extension.CalculateGovAge(profile.dateAppoint, 0, 0)} ปี` + : null, + age: profile.birthDate + ? Extension.CalculateAgeStrV2(profile.birthDate, 0, 0, "GET") + : null, + dateAppoint: profile.dateAppoint, + dateCurrent: new Date(), + amount: profile.amount, + telephoneNumber: profile.telephoneNumber, + currentAddress: + profile && profile.currentAddress + ? profile.currentAddress + + (profile.currentSubDistrict && profile.currentSubDistrict.name + ? " ตำบล/แขวง " + profile.currentSubDistrict.name + : "") + + (profile.currentDistrict && profile.currentDistrict.name + ? " อำเภอ/เขต " + profile.currentDistrict.name + : "") + + (profile.currentProvince && profile.currentProvince.name + ? " จังหวัด " + profile.currentProvince.name + " " + : "") + + profile.currentZipCode + : "-", + position: profile.position, + posType: profile.posType?.posTypeName, + posLevel: `${profile.posType?.posTypeShortName} ${profile.posLevel?.posLevelName}`, + positionLeaveName: null, + posExecutiveName: null, + + isCommission: currentHolder?.orgRoot?.isCommission ?? false, + root: currentHolder?.orgRoot?.orgRootName ?? null, + child1: currentHolder?.orgChild1?.orgChild1Name ?? null, + child2: currentHolder?.orgChild2?.orgChild2Name ?? null, + child3: currentHolder?.orgChild3?.orgChild3Name ?? null, + child4: currentHolder?.orgChild4?.orgChild4Name ?? null, + oc: oc, + + positions: _positions, + educations: _educations, + }; + return new HttpSuccess(mapProfile); + } + + /* ========================================= + * current holder + * ========================================= */ + const currentHolder = profile.current_holders?.find( + x => + x.orgRevision?.orgRevisionIsDraft === false && + x.orgRevision?.orgRevisionIsCurrent === true, + ); + + 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; + } + } + + /* ========================================= + * posType + posLevel + * ========================================= */ + const positionLeaveName = + profile.posType && + profile.posLevel && + (profile.posType.posTypeName === "บริหาร" || + profile.posType.posTypeName === "อำนวยการ") + ? `${profile.posType.posTypeName}${profile.posLevel.posLevelName}` + : profile.posLevel?.posLevelName ?? null; + + /* ========================================= + * position executive + * ========================================= */ + const _posExec = await this.positionRepository.findOne({ + where: { + positionIsSelected: true, + posMaster: { + orgRevisionId: currentHolder?.orgRevisionId, + current_holderId: profile.id, + }, + }, + order: { createdAt: "DESC" }, + relations: { posExecutive: true }, + }); + + let _positions: any[] = []; + let _educations: any[] = []; + if (body.report && ["LEAVE16"].includes(body.report.trim().toUpperCase())) { + + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; + if (profile && profile?.isLeave) { + _currentDate = + profile && profile.leaveDate ? Extension.toDateOnlyString(profile.leaveDate) : _currentDate; + } + const positions = await AppDataSource.query("CALL GetProfileSalaryPosition(?, ?)", [ + profile!.id, + _currentDate, + ]); + _positions = positions[0].length > 0 + ? _positions.slice(0, -1).map((x: any, idx: number) => ({ + positionName: x.positionName, + dateStart: x.commandDateAffect ?? null, + dateEnd: _positions[idx + 1]?.commandDateAffect ?? null, + })) + : []; + + const profileEducations = await this.educationRepo.find({ + where: { profileId: profile!.id }, + order: { level: "ASC" }, + }); + _educations = profileEducations.length > 0 + ? profileEducations.map((x) => ({ + educationLevel: x.educationLevel, + institute: x.institute ?? "-", + country: x.country ?? "-", + finishDate: x.finishDate, + })) + : []; + } + + const mapProfile = { + prefix: profile.prefix, + firstName: profile.firstName, + lastName: profile.lastName, + citizenId: profile.citizenId, + birthDate: profile.birthDate, + retireDate: profile.birthDate + ? calculateRetireLaw(profile.birthDate) + : null, + govAge: profile.dateAppoint + ? `${Extension.CalculateGovAge(profile.dateAppoint, 0, 0)} ปี` + : null, + age: profile.birthDate + ? Extension.CalculateAgeStrV2(profile.birthDate, 0, 0, "GET") + : null, + dateAppoint: profile.dateAppoint, + dateCurrent: new Date(), + amount: profile.amount, + telephoneNumber: profile.telephoneNumber, + currentAddress: + profile && profile.currentAddress + ? profile.currentAddress + + (profile.currentSubDistrict && profile.currentSubDistrict.name + ? " ตำบล/แขวง " + profile.currentSubDistrict.name + : "") + + (profile.currentDistrict && profile.currentDistrict.name + ? " อำเภอ/เขต " + profile.currentDistrict.name + : "") + + (profile.currentProvince && profile.currentProvince.name + ? " จังหวัด " + profile.currentProvince.name + " " + : "") + + profile.currentZipCode + : "-", + position: profile.position ?? "-", + posLevel: profile.posLevel?.posLevelName ?? "-", + posType: profile.posType?.posTypeName ?? "-", + positionLeaveName, + posExecutiveName: _posExec?.posExecutive?.posExecutiveName ?? null, + + isCommission: currentHolder?.orgRoot?.isCommission ?? false, + root: currentHolder?.orgRoot?.orgRootName ?? null, + child1: currentHolder?.orgChild1?.orgChild1Name ?? null, + child2: currentHolder?.orgChild2?.orgChild2Name ?? null, + child3: currentHolder?.orgChild3?.orgChild3Name ?? null, + child4: currentHolder?.orgChild4?.orgChild4Name ?? null, + oc: oc, + + positions: _positions, + educations: _educations, + }; + return new HttpSuccess(mapProfile); + } + /** * API Get Profile ใช้อัพเดทสถานะ Mark ในระบบเครื่องราช * @@ -7723,7 +8027,7 @@ export class OrganizationDotnetController extends Controller { dateStart: profile?.dateStart ?? null, dateAppoint: profile?.dateAppoint ?? null, keycloak: profile?.keycloak ?? null, - posNo: item.shortName, + posNo: `${item.shortName} ${item.posMasterNo}`, position: item.position, positionLevel: item.posLevel, positionType: item.posType,