From 21813ffd07d09ef463c0285e7d1af42287b137a7 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 30 Jan 2025 14:40:08 +0700 Subject: [PATCH 1/2] =?UTF-8?q?cronjob=20=E0=B8=AD=E0=B8=B1=E0=B8=9E?= =?UTF-8?q?=E0=B9=80=E0=B8=94=E0=B8=97=E0=B8=AA=E0=B8=96=E0=B8=B2=E0=B8=99?= =?UTF-8?q?=E0=B8=B0=E0=B9=80=E0=B8=81=E0=B8=A9=E0=B8=B5=E0=B8=A2=E0=B8=93?= =?UTF-8?q?=E0=B8=AD=E0=B8=B2=E0=B8=A2=E0=B8=B8=E0=B8=A3=E0=B8=B2=E0=B8=8A?= =?UTF-8?q?=E0=B8=81=E0=B8=B2=E0=B8=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 10 ++ src/controllers/CommandController.ts | 87 ++++++++++++++ .../OrganizationUnauthorizeController.ts | 108 +++++++++--------- 3 files changed, 151 insertions(+), 54 deletions(-) diff --git a/src/app.ts b/src/app.ts index 1ed0839f..36df20ea 100644 --- a/src/app.ts +++ b/src/app.ts @@ -56,6 +56,16 @@ async function main() { console.error("Error executing function from controller:", error); } }); + + const cronTime_Oct = "0 0 1 10 *"; + cron.schedule(cronTime_Oct, async () => { + try { + const commandController = new CommandController(); + await commandController.cronjobUpdateRetirementStatus(); + } catch (error) { + console.error("Error executing function from controller:", error); + } + }); // app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`)); app.listen( diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index a020da83..fd71116e 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1329,6 +1329,93 @@ export class CommandController extends Controller { return new HttpSuccess(); } + async cronjobUpdateRetirementStatus() { + 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()+1}`, + { + 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()+1}`, + { + 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(); + } + /** * API รายละเอียดรายการคำสั่ง tab4 คำสั่ง * diff --git a/src/controllers/OrganizationUnauthorizeController.ts b/src/controllers/OrganizationUnauthorizeController.ts index dca7a4d0..9ecc7d87 100644 --- a/src/controllers/OrganizationUnauthorizeController.ts +++ b/src/controllers/OrganizationUnauthorizeController.ts @@ -1160,59 +1160,59 @@ export class OrganizationUnauthorizeController extends Controller { return new HttpSuccess("Email verified successfully."); } - @Patch("retirement") - public async updateStatusRetirement( - @Body() - body: { - data: { - profileId: string; - }[]; - }, - ) { - let profiles: Profile[] = []; - let _null: any = null; - await Promise.all( - body.data.map(async (item) => { - const _profile = await this.profileRepo.findOneBy({ id: item.profileId }); - if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - _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.profileRepo.save(profiles); - return new HttpSuccess(); - } + // @Patch("retirement") + // public async updateStatusRetirement( + // @Body() + // body: { + // data: { + // profileId: string; + // }[]; + // }, + // ) { + // let profiles: Profile[] = []; + // let _null: any = null; + // await Promise.all( + // body.data.map(async (item) => { + // const _profile = await this.profileRepo.findOneBy({ id: item.profileId }); + // if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + // _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.profileRepo.save(profiles); + // return new HttpSuccess(); + // } - @Patch("retirement-employee") - public async updateStatusRetirementEmp( - @Body() - body: { - data: { - profileId: string; - }[]; - }, - ) { - let profiles: ProfileEmployee[] = []; - let _null: any = null; - await Promise.all( - body.data.map(async (item) => { - const _profile = await this.profileEmpRepo.findOneBy({ id: item.profileId }); - if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - _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.profileEmpRepo.save(profiles); - return new HttpSuccess(); - } + // @Patch("retirement-employee") + // public async updateStatusRetirementEmp( + // @Body() + // body: { + // data: { + // profileId: string; + // }[]; + // }, + // ) { + // let profiles: ProfileEmployee[] = []; + // let _null: any = null; + // await Promise.all( + // body.data.map(async (item) => { + // const _profile = await this.profileEmpRepo.findOneBy({ id: item.profileId }); + // if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + // _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.profileEmpRepo.save(profiles); + // return new HttpSuccess(); + // } } From 9636ee669135ed7359e69b8cd9ba2bad528301af Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 30 Jan 2025 14:40:56 +0700 Subject: [PATCH 2/2] no message --- src/controllers/CommandController.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index fd71116e..746f0bf0 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1352,7 +1352,7 @@ export class CommandController extends Controller { let type: string = "OFFICER" try { const response_ = await axios.get( - process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()+1}`, + process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, { headers: { Authorization: `Bearer ${adminToken}`, @@ -1384,7 +1384,7 @@ export class CommandController extends Controller { type = "EMPLOYEE" try { const response_ = await axios.get( - process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()+1}`, + process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, { headers: { Authorization: `Bearer ${adminToken}`,