From ec6b4a7ac8bc02ea27b59f2a759abfa2ff92ba42 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 24 Apr 2026 12:19:17 +0700 Subject: [PATCH] fix calculate --- src/app.ts | 3 +- src/controllers/ProfileSalaryController.ts | 40 ++++++++++++++-------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/app.ts b/src/app.ts index 06f76548..6ee54a80 100644 --- a/src/app.ts +++ b/src/app.ts @@ -98,7 +98,8 @@ async function main() { }); // Cron job for updating tenure - every day at 04:00:00 - const cronTime_Tenure = "0 0 4 * * *"; + // const cronTime_Tenure = "0 0 4 * * *"; + const cronTime_Tenure = "0 30 12 * * *"; // test 12:30 cron.schedule(cronTime_Tenure, async () => { try { const profileSalaryController = new ProfileSalaryController(); diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 03814161..4e3e9e3b 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -66,10 +66,12 @@ export class ProfileSalaryController extends Controller { await this.positionOfficerRepo.clear(); const profile = await this.profileRepo.find(); const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); - let _currentDate = CURRENT_DATE[0].today; + const baseCurrentDate = CURRENT_DATE[0].today; for await (const x of profile) { - if (x.isLeave) { - _currentDate = x.leaveDate ? Extension.toDateOnlyString(x.leaveDate) : _currentDate; + // Use leave date if available and valid, otherwise use current date + let _currentDate = baseCurrentDate; + if (x.isLeave && x.leaveDate) { + _currentDate = Extension.toDateOnlyString(x.leaveDate); } const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?, ?)", [ x.id, @@ -113,11 +115,13 @@ export class ProfileSalaryController extends Controller { let data: any = []; await this.positionEmployeeRepo.clear(); const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); - let _currentDate = CURRENT_DATE[0].today; + const baseCurrentDate = CURRENT_DATE[0].today; const profile = await this.profileEmployeeRepo.find(); for await (const x of profile) { - if (x?.isLeave) { - _currentDate = x.leaveDate ? Extension.toDateOnlyString(x.leaveDate) : _currentDate; + // Use leave date if available and valid, otherwise use current date + let _currentDate = baseCurrentDate; + if (x?.isLeave && x.leaveDate) { + _currentDate = Extension.toDateOnlyString(x.leaveDate); } const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?, ?)", [ x.id, @@ -162,10 +166,12 @@ export class ProfileSalaryController extends Controller { await this.levelOfficerRepo.clear(); const profile = await this.profileRepo.find({ relations: ["posLevel", "posType"] }); const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); - let _currentDate = CURRENT_DATE[0].today; + const baseCurrentDate = CURRENT_DATE[0].today; for await (const x of profile) { - if (x?.isLeave) { - _currentDate = x.leaveDate ? Extension.toDateOnlyString(x.leaveDate) : _currentDate; + // Use leave date if available and valid, otherwise use current date + let _currentDate = baseCurrentDate; + if (x?.isLeave && x.leaveDate) { + _currentDate = Extension.toDateOnlyString(x.leaveDate); } const positionLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?, ?)", [ x.id, @@ -220,10 +226,12 @@ export class ProfileSalaryController extends Controller { await this.levelEmployeeRepo.clear(); const profile = await this.profileEmployeeRepo.find({ relations: ["posLevel", "posType"] }); const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); - let _currentDate = CURRENT_DATE[0].today; + const baseCurrentDate = CURRENT_DATE[0].today; for await (const x of profile) { - if (x?.isLeave) { - _currentDate = x.leaveDate ? Extension.toDateOnlyString(x.leaveDate) : _currentDate; + // Use leave date if available and valid, otherwise use current date + let _currentDate = baseCurrentDate; + if (x?.isLeave && x.leaveDate) { + _currentDate = Extension.toDateOnlyString(x.leaveDate); } const positionLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?, ?)", [ x.id, @@ -286,10 +294,12 @@ export class ProfileSalaryController extends Controller { }, }); const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); - let _currentDate = CURRENT_DATE[0].today; + const baseCurrentDate = CURRENT_DATE[0].today; for await (const x of profile) { - if (x?.isLeave) { - _currentDate = x.leaveDate ? Extension.toDateOnlyString(x.leaveDate) : _currentDate; + // Use leave date if available and valid, otherwise use current date + let _currentDate = baseCurrentDate; + if (x?.isLeave && x.leaveDate) { + _currentDate = Extension.toDateOnlyString(x.leaveDate); } const position = await this.positionRepo.findOne({ where: {