diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 4e5f898e..68bd0588 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1478,7 +1478,7 @@ export class CommandController extends Controller { return new HttpSuccess(); } - async cronjobUpdateRetirementStatus() { + async cronjobUpdateRetirementStatus(/*@Request() request: RequestWithUser*/) { let body = { client_id: "gettoken", client_secret: process.env.AUTH_ACCOUNT_SECRET, @@ -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,144 @@ 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) => { + response_.data.result.profiles.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, + 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; + if (type == "OFFICER") { + data.profileId = profileId; + data.profileEmployeeId = null; + } + else if (type == "EMPLOYEE"){ + data.profileEmployeeId = profileId; + data.profileId = null; + } + 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(); diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index bc02a392..e6f4c1e7 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -5888,7 +5888,7 @@ export class ProfileController extends Controller { ) // .andWhere("profile.leaveCommandId Is NOT NULL") .andWhere( - "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileId = profile.id and profileSalary.positionName != 'เกษียณอายุราชการ')", + "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileId = profile.id and ps.positionName != 'เกษียณอายุราชการ')", ) .andWhere( diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index c1f1db7a..b217b3b3 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -2786,7 +2786,7 @@ export class ProfileEmployeeController extends Controller { ) // .andWhere("profileEmployee.leaveCommandId Is NOT NULL") .andWhere( - "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileEmployeeId = profileEmployee.id and profileSalary.positionName != 'เกษียณอายุราชการ')", + "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileEmployeeId = profileEmployee.id and ps.positionName != 'เกษียณอายุราชการ')", ) // .andWhere( diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts index 8abe6617..f2acbaba 100644 --- a/src/controllers/ProfileGovernmentController.ts +++ b/src/controllers/ProfileGovernmentController.ts @@ -249,9 +249,13 @@ export class ProfileGovernmentHistoryController extends Controller { if (record.leaveType == "RETIRE") { _profileSalary = record?.profileSalary.length > 1 ? record?.profileSalary[1] - : null; + : record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; } else { - _profileSalary = record?.profileSalary[0]; + _profileSalary = record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; } if (_profileSalary) { _OrgLeave = [ @@ -404,9 +408,13 @@ export class ProfileGovernmentHistoryController extends Controller { if (record.leaveType == "RETIRE") { _profileSalary = record?.profileSalary.length > 1 ? record?.profileSalary[1] - : null; + : record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; } else { - _profileSalary = record?.profileSalary[0]; + _profileSalary = record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; } if (_profileSalary) { _OrgLeave = [ diff --git a/src/controllers/ProfileGovernmentEmployeeController.ts b/src/controllers/ProfileGovernmentEmployeeController.ts index 9191eb2e..ce170d17 100644 --- a/src/controllers/ProfileGovernmentEmployeeController.ts +++ b/src/controllers/ProfileGovernmentEmployeeController.ts @@ -264,11 +264,15 @@ export class ProfileGovernmentEmployeeController extends Controller { // profileSalary.length > 0 && profileSalary[0].orgRoot ? profileSalary[0].orgRoot : null, // ]; if (record.leaveType == "RETIRE") { - _profileSalary = record?.profileSalary.length > 1 - ? record?.profileSalary[1] - : null; + _profileSalary = profileSalary.length > 1 + ? profileSalary[1] + : profileSalary.length > 0 + ? profileSalary[0] + : null; } else { - _profileSalary = record?.profileSalary[0]; + _profileSalary = profileSalary.length > 0 + ? profileSalary[0] + : null } if (_profileSalary) { _OrgLeave = [ @@ -435,11 +439,15 @@ export class ProfileGovernmentEmployeeController extends Controller { // profileSalary.length > 0 && profileSalary[0].orgRoot ? profileSalary[0].orgRoot : null, // ]; if (record.leaveType == "RETIRE") { - _profileSalary = record?.profileSalary.length > 1 - ? record?.profileSalary[1] - : null; + _profileSalary = profileSalary.length > 1 + ? profileSalary[1] + : profileSalary.length > 0 + ? profileSalary[0] + : null; } else { - _profileSalary = record?.profileSalary[0]; + _profileSalary = profileSalary.length > 0 + ? profileSalary[0] + : null; } if (_profileSalary) { _OrgLeave = [