From 7ffb698252593136ddee6de15df901697c99c682 Mon Sep 17 00:00:00 2001 From: harid Date: Wed, 1 Oct 2025 22:21:16 +0700 Subject: [PATCH] =?UTF-8?q?test=20job=20=E0=B9=80=E0=B8=81=E0=B8=A9?= =?UTF-8?q?=E0=B8=B5=E0=B8=A2=E0=B8=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 3 +- src/controllers/CommandController.ts | 162 ++++++++++++++++++++++++--- 2 files changed, 149 insertions(+), 16 deletions(-) diff --git a/src/app.ts b/src/app.ts index 236fb105..fb2c4d5b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -62,7 +62,8 @@ async function main() { } }); - const cronTime_Oct = "0 0 1 10 *"; + // const cronTime_Oct = "0 0 1 10 *"; + const cronTime_Oct = "0 0 2 10 *"; // test 2 ตุลาคม cron.schedule(cronTime_Oct, async () => { try { const commandController = new CommandController(); diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 4e5f898e..2f72f709 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1499,6 +1499,7 @@ export class CommandController extends Controller { const today = new Date(); today.setUTCHours(0, 0, 0, 0); let type: string = "OFFICER"; + let _Date = new Date() try { const response_ = await axios.get( process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, @@ -1510,23 +1511,46 @@ export class CommandController extends Controller { }, }, ); - if (response && response_.data.result.length > 0) { - let profiles: Profile[] = []; + if (response_ && response_.data.result) { + let signDate:string = "" + if (response_.data.result.signDate != null) { + signDate = Extension.ToThaiShortDate_noPrefix(new Date(response_.data.result.signDate)) + } + else { + signDate = Extension.ToThaiShortDate_noPrefix(_Date) + } + // let profiles: Profile[] = []; await Promise.all( - response_.data.result.map(async (x: any) => { + response_.data.result.profiles.map(async (x: any) => { const _profile = await this.profileRepository.findOneBy({ id: x.profileId }); if (_profile) { + // console.log("1. บันทึกลงประวัติตำแหน่ง") + await this.profileSalaryRetire(x.profileId, type, signDate, _Date); + // console.log("2. ปลดออกจากโครงสร้าง") + await this.posMasterRetire(x.profileId, type, _Date); + // console.log("3. แก้ไขสถานะในทะเบียนประวัติ") _profile.isRetirement = true; _profile.isLeave = true; + _profile.isActive = false _profile.leaveType = "RETIRE"; - _profile.leaveDate = new Date(); - _profile.dateLeave = new Date(); - _profile.lastUpdatedAt = new Date(); - profiles.push(_profile); + _profile.leaveReason = "เกษียณอายุราชการ"; + _profile.leaveDate = _Date; + _profile.dateLeave = _Date; + _profile.lastUpdatedAt = _Date; + if (_profile.keycloak != null && _profile.keycloak != "") { + // console.log("4. disable keycloak/authen") + const delUserKeycloak = await deleteUser(_profile.keycloak); + if (delUserKeycloak) { + _profile.keycloak = ""; + _profile.roleKeycloaks = []; + } + } + // profiles.push(_profile); + // console.log("5. save profile ",_profile) + await this.profileRepository.save(_profile); } }), ); - await this.profileRepository.save(profiles); } } catch {} @@ -1542,29 +1566,137 @@ export class CommandController extends Controller { }, }, ); - if (response && response_.data.result.length > 0) { - let profiles: ProfileEmployee[] = []; + if (response_ && response_.data.result) { + let signDate:string = "" + if (response_.data.result.signDate != null) { + signDate = Extension.ToThaiShortDate_noPrefix(new Date(response_.data.result.signDate)) + } + else { + signDate = Extension.ToThaiShortDate_noPrefix(_Date) + } + // let profiles: ProfileEmployee[] = []; await Promise.all( response_.data.result.map(async (x: any) => { const _profileEmp = await this.profileEmployeeRepository.findOneBy({ id: x.profileId }); if (_profileEmp) { + // บันทึกลงประวัติตำแหน่ง + await this.profileSalaryRetire(x.profileId, type, signDate, _Date); + // ปลดออกจากโครงสร้าง + await this.posMasterRetire(x.profileId, type, _Date); + // แก้ไขสถานะในทะเบียนประวัติ _profileEmp.isRetirement = true; _profileEmp.isLeave = true; + _profileEmp.isActive = false _profileEmp.leaveType = "RETIRE"; - _profileEmp.leaveDate = new Date(); - _profileEmp.dateLeave = new Date(); - _profileEmp.lastUpdatedAt = new Date(); - profiles.push(_profileEmp); + _profileEmp.leaveReason = "เกษียณอายุราชการ"; + _profileEmp.leaveDate = _Date; + _profileEmp.dateLeave = _Date; + _profileEmp.lastUpdatedAt = _Date; + if (_profileEmp.keycloak != null && _profileEmp.keycloak != "") { + // disable keycloak/authen + const delUserKeycloak = await deleteUser(_profileEmp.keycloak); + if (delUserKeycloak) { + _profileEmp.keycloak = ""; + _profileEmp.roleKeycloaks = []; + } + } + // profiles.push(_profileEmp); + await this.profileEmployeeRepository.save(_profileEmp); } }), ); - await this.profileEmployeeRepository.save(profiles); } } catch {} return new HttpSuccess(); } + async profileSalaryRetire(profileId: string, type: string, signDate: string, _Date: Date) { + const whereKey = type == "OFFICER" + ? { profileId: profileId } + : { profileEmployeeId: profileId }; + const maxOrder = await this.salaryRepo.findOne({ + select: { order: true }, + where: whereKey, + order: { order: "DESC" }, + }); + const data: any = { + order: maxOrder ? maxOrder.order + 1 : 1, + amount: null, + positionSalaryAmount: null, + mouthSalaryAmount: null, + profileId: profileId, + posNo: null, + positionExecutive: null, + positionType: null, + positionLevel: null, + amountSpecial: null, + orgRoot: null, + orgChild1: null, + orgChild2: null, + orgChild3: null, + orgChild4: null, + commandYear: _Date.getFullYear() + 543, + commandDateAffect: _Date, + commandCode: "16", + commandName: "พ้นจากราชการ", + posNoAbb: null, + isEntry: false, + positionName: "เกษียณอายุราชการ", + createdUserId: "", + createdFullName: "System Administrator", + lastUpdateUserId: "", + lastUpdateFullName: "System Administrator", + createdAt: _Date, + lastUpdatedAt: _Date, + remark: `ประกาศคณะอนุกรรมการสามัญข้าราชการกรุงเทพมหานครสามัญ ลว. ${signDate}`, // script เกษียณจริง ๆ ให้เอา “วันที่ประกาศเกษียณฉบับแรก” มาลงในเอกสารอ้างอิง + isGovernment: false, + }; + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + data.dateGovernment = _Date; + const savedData = await this.salaryRepo.save(data); + history.profileSalaryId = savedData.id; + await this.salaryHistoryRepo.save(history); + } + + async posMasterRetire(profileId: string, type: string, _Date: Date) { + const orgRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } + }); + if (orgRevision) { + let _posMaster:any = null; + if (type == "OFFICER") { + _posMaster = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision.id, + current_holderId: profileId, + }, + }); + if (_posMaster) { + _posMaster.current_holderId = null; + _posMaster.lastUpdateFullName = "System Administrator"; + _posMaster.lastUpdatedAt = _Date; + await this.posMasterRepository.save(_posMaster); + } + } + else if (type == "EMPLOYEE") { + _posMaster = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision.id, + current_holderId: profileId, + }, + }); + if (_posMaster) { + _posMaster.current_holderId = null; + _posMaster.lastUpdateFullName = "System Administrator"; + _posMaster.lastUpdatedAt = _Date; + await this.employeePosMasterRepository.save(_posMaster); + } + } + } + } + @Get("cornjob/cronjobUpdateRetirementStatus") async runCronjobUpdateRetirementStatus() { await this.cronjobUpdateRetirementStatus();