From cd2cd1645be689f460ded633c5ffb965175df106 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Tue, 25 Mar 2025 14:08:17 +0700 Subject: [PATCH] api run corn job retire --- src/controllers/CommandController.ts | 95 ++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 0bb89f28..a3565420 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1249,6 +1249,101 @@ export class CommandController extends Controller { return new HttpSuccess(); } + /** + * API รายละเอียดรายการคำสั่ง tab3 + * + * @summary API รายละเอียดรายการคำสั่ง tab3 + * + * @param {string} id Id คำสั่ง + */ + @Get("run/cronjob/retirement") + async CreateCronjobUpdateRetirementStatus(@Request() request: RequestWithUser) { + let body = { + client_id: "gettoken", + client_secret: process.env.AUTH_ACCOUNT_SECRET, + grant_type: "client_credentials", + }; + const postData = querystring.stringify(body); + const response = await axios.post( + `${process.env.KC_URL}/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`, + postData, + { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + api_key: process.env.API_KEY, + }, + }, + ); + const adminToken = response.data.access_token; + const today = new Date(); + today.setUTCHours(0, 0, 0, 0); + let type: string = "OFFICER"; + try { + const response_ = await axios.get( + process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, + { + headers: { + Authorization: `Bearer ${adminToken}`, + "Content-Type": "application/json", + api_key: process.env.API_KEY, + }, + }, + ); + if (response && response_.data.result.length > 0) { + let profiles: Profile[] = []; + await Promise.all( + response_.data.result.map(async (x: any) => { + const _profile = await this.profileRepository.findOneBy({ id: x.profileId }); + if (_profile) { + _profile.isRetirement = true; + _profile.isLeave = true; + _profile.leaveType = "RETIRE"; + _profile.leaveDate = new Date(); + _profile.dateLeave = new Date(); + _profile.lastUpdatedAt = new Date(); + profiles.push(_profile); + } + }), + ); + await this.profileRepository.save(profiles); + } + } catch {} + + type = "EMPLOYEE"; + try { + const response_ = await axios.get( + process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, + { + headers: { + Authorization: `Bearer ${adminToken}`, + "Content-Type": "application/json", + api_key: process.env.API_KEY, + }, + }, + ); + if (response && response_.data.result.length > 0) { + let profiles: ProfileEmployee[] = []; + await Promise.all( + response_.data.result.map(async (x: any) => { + const _profileEmp = await this.profileEmployeeRepository.findOneBy({ id: x.profileId }); + if (_profileEmp) { + _profileEmp.isRetirement = true; + _profileEmp.isLeave = true; + _profileEmp.leaveType = "RETIRE"; + _profileEmp.leaveDate = new Date(); + _profileEmp.dateLeave = new Date(); + _profileEmp.lastUpdatedAt = new Date(); + profiles.push(_profileEmp); + } + }), + ); + await this.profileEmployeeRepository.save(profiles); + } + } catch {} + + return new HttpSuccess(); + } + async cronjobUpdateRetirementStatus() { let body = { client_id: "gettoken",