From 42cc69cbbecf83b306a876be891363796899d97d Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 18 Jul 2025 13:41:52 +0700 Subject: [PATCH] =?UTF-8?q?Fix=20[Bug]=20=E0=B8=84=E0=B8=B3=E0=B8=99?= =?UTF-8?q?=E0=B8=A7=E0=B8=99=E0=B8=A3=E0=B8=B0=E0=B8=A2=E0=B8=B0=E0=B9=80?= =?UTF-8?q?=E0=B8=A7=E0=B8=A5=E0=B8=B2=E0=B8=94=E0=B8=B3=E0=B8=A3=E0=B8=87?= =?UTF-8?q?=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88=E0=B8=87?= =?UTF-8?q?=E0=B9=83=E0=B8=99=E0=B8=AA=E0=B8=B2=E0=B8=A2=E0=B8=87=E0=B8=B2?= =?UTF-8?q?=E0=B8=99,=20=E0=B8=A3=E0=B8=B0=E0=B8=A2=E0=B8=B0=E0=B9=80?= =?UTF-8?q?=E0=B8=A7=E0=B8=A5=E0=B8=B2=E0=B8=94=E0=B8=B3=E0=B8=A3=E0=B8=87?= =?UTF-8?q?=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88=E0=B8=87?= =?UTF-8?q?=E0=B8=95=E0=B8=B2=E0=B8=A1=E0=B8=A3=E0=B8=B0=E0=B8=94=E0=B8=B1?= =?UTF-8?q?=E0=B8=9A=20=E0=B9=81=E0=B8=A5=E0=B8=B0=E0=B8=97=E0=B8=B2?= =?UTF-8?q?=E0=B8=87=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=9A=E0=B8=A3=E0=B8=B4?= =?UTF-8?q?=E0=B8=AB=E0=B8=B2=E0=B8=A3=E0=B8=82=E0=B8=AD=E0=B8=87=E0=B8=84?= =?UTF-8?q?=E0=B8=99=E0=B8=97=E0=B8=B5=E0=B9=88=E0=B8=9E=E0=B9=89=E0=B8=99?= =?UTF-8?q?=E0=B8=88=E0=B8=B2=E0=B8=81=E0=B8=A3=E0=B8=B2=E0=B8=8A=E0=B8=81?= =?UTF-8?q?=E0=B8=B2=E0=B8=A3=20#137?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrganizationDotnetController.ts | 12 ++- src/controllers/PositionController.ts | 17 +++- src/controllers/ProfileSalaryController.ts | 84 ++++++++++++++++--- .../ProfileSalaryEmployeeController.ts | 34 +++++++- src/interfaces/extension.ts | 6 ++ 5 files changed, 131 insertions(+), 22 deletions(-) diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index a2010ddb..5abd6a13 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -5244,6 +5244,8 @@ export class OrganizationDotnetController extends Controller { */ @Get("profile-leave/keycloak/{keycloakId}") async GetProfileLeaveByKeycloakIdAsync(@Path() keycloakId: string) { + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; const profile = await this.profileRepo.findOne({ relations: [ "posLevel", @@ -5317,8 +5319,9 @@ export class OrganizationDotnetController extends Controller { oc = `${_profileCurrent.orgChild4?.orgChild4Name}`; } } - const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [ + const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?, ?)", [ profile.id, + _currentDate ]); const _position = position.length > 0 ? position[0] : []; @@ -5429,7 +5432,12 @@ export class OrganizationDotnetController extends Controller { oc = `${_profileCurrent.orgChild4?.orgChild4Name}`; } } - const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [profile.id]); + if (profile && profile?.isLeave) { + _currentDate = profile && profile.leaveDate + ? Extension.toDateOnlyString(profile.leaveDate) + : _currentDate + } + const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?, ?)", [profile.id, _currentDate]); const _position = position.length > 0 ? position[0] : []; const mapProfile = { diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 47c59547..de64c036 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -5050,10 +5050,19 @@ export class PositionController extends Controller { : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` : "1=1", ) - .andWhere(checkChildConditions) - .andWhere(typeCondition) - .andWhere(revisionCondition) - .andWhere({ current_holderId: IsNull() }); + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition) + .andWhere({ current_holderId: IsNull() }); + }), + ) + .orWhere( + new Brackets((qb) => { + qb.andWhere(`posMaster.conditionReason LIKE '%${body.keyword}%' AND posMaster.conditionReason IS NOT NULL`) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition) + .andWhere({ current_holderId: IsNull() }); }), ) .orderBy("orgRoot.orgRootOrder", "ASC") diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index ecb8c2c7..f99c96bd 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -32,6 +32,8 @@ import { Command } from "../entities/Command"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgRevision } from "../entities/OrgRevision"; import { Position } from "../entities/Position"; +import Extension from "../interfaces/extension"; + @Route("api/v1/org/profile/salary") @Tags("ProfileSalary") @Security("bearerAuth") @@ -55,8 +57,15 @@ export class ProfileSalaryController extends Controller { let data: any = []; 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; for await (const x of profile) { - const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [x.id]); + if (x.isLeave) { + _currentDate = x.leaveDate + ? Extension.toDateOnlyString(x.leaveDate) + : _currentDate + } + const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?, ?)", [x.id, _currentDate]); const _position = position.length > 0 ? position[0] : []; const mapPosition = _position.length > 1 @@ -94,10 +103,18 @@ export class ProfileSalaryController extends Controller { public async cronjobTenurePositionEmployee() { 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 profile = await this.profileEmployeeRepo.find(); for await (const x of profile) { - const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [ + if (x?.isLeave) { + _currentDate = x.leaveDate + ? Extension.toDateOnlyString(x.leaveDate) + : _currentDate + } + const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?, ?)", [ x.id, + _currentDate ]); const _position = position.length > 0 ? position[0] : []; const mapPosition = @@ -137,8 +154,15 @@ export class ProfileSalaryController extends Controller { let data: any = []; 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; for await (const x of profile) { - const positionLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [x.id]); + if (x?.isLeave) { + _currentDate = x.leaveDate + ? Extension.toDateOnlyString(x.leaveDate) + : _currentDate + } + const positionLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?, ?)", [x.id, _currentDate]); const _positionLevel = positionLevel.length > 0 ? positionLevel[0] : []; const mapPositionLevel = _positionLevel.length > 1 @@ -187,9 +211,17 @@ export class ProfileSalaryController extends Controller { let data: any = []; 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; for await (const x of profile) { - const positionLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?)", [ + if (x?.isLeave) { + _currentDate = x.leaveDate + ? Extension.toDateOnlyString(x.leaveDate) + : _currentDate + } + const positionLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?, ?)", [ x.id, + _currentDate ]); const _positionLevel = positionLevel.length > 0 ? positionLevel[0] : []; const mapPositionLevel = @@ -246,7 +278,14 @@ export class ProfileSalaryController extends Controller { orgRevisionIsCurrent: true, }, }); + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; for await (const x of profile) { + if (x?.isLeave) { + _currentDate = x.leaveDate + ? Extension.toDateOnlyString(x.leaveDate) + : _currentDate + } const position = await this.positionRepo.findOne({ where: { positionIsSelected: true, @@ -260,7 +299,7 @@ export class ProfileSalaryController extends Controller { posExecutive: true, }, }); - const positionExecutive = await AppDataSource.query("CALL GetProfileSalaryExecutive(?)", [x.id]); + const positionExecutive = await AppDataSource.query("CALL GetProfileSalaryExecutive(?, ?)", [x.id, _currentDate]); const _position = positionExecutive.length > 0 ? positionExecutive[0] : []; const mapPosition = _position.length > 1 @@ -475,7 +514,14 @@ export class ProfileSalaryController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [profile.id]); + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; + if (profile && profile?.isLeave) { + _currentDate = profile && profile.leaveDate + ? Extension.toDateOnlyString(profile.leaveDate) + : _currentDate + } + const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?, ?)", [profile.id, _currentDate]); const _position = position.length > 0 ? position[0] : []; const mapPosition = @@ -506,8 +552,7 @@ export class ProfileSalaryController extends Controller { }, [] as { name: string; days: number; year: number; month: number; day: number }[], ); - - const posLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [profile.id]); + const posLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?, ?)", [profile.id, _currentDate]); const _posLevel = posLevel.length > 0 ? posLevel[0] : []; const mapPosLevel = _posLevel.length > 1 @@ -544,8 +589,9 @@ export class ProfileSalaryController extends Controller { [] as { name: string; days: number; year: number; month: number; day: number }[], ); - const posExecutive = await AppDataSource.query("CALL GetProfileSalaryExecutive(?)", [ + const posExecutive = await AppDataSource.query("CALL GetProfileSalaryExecutive(?, ?)", [ profile.id, + _currentDate ]); const _posExecutive = posExecutive.length > 0 ? posExecutive[0] : []; const mapPosExecutive = @@ -589,7 +635,20 @@ export class ProfileSalaryController extends Controller { // const sql_mode = await AppDataSource.query( // "SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));", // ); - const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [profileId]); + const _profile = await this.profileRepo.findOne({ + where: { id: profileId } + }) + if (!_profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; + if (_profile && _profile?.isLeave) { + _currentDate = _profile && _profile.leaveDate + ? Extension.toDateOnlyString(_profile.leaveDate) + : _currentDate + } + const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?, ?)", [profileId, _currentDate]); const _position = position.length > 0 ? position[0] : []; const mapPosition = @@ -624,7 +683,7 @@ export class ProfileSalaryController extends Controller { [] as { name: string; days: number; year: number; month: number; day: number }[], ); - const posLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [profileId]); + const posLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?, ?)", [profileId, _currentDate]); const _posLevel = posLevel.length > 0 ? posLevel[0] : []; const mapPosLevel = _posLevel.length > 1 @@ -664,8 +723,9 @@ export class ProfileSalaryController extends Controller { [] as { name: string; days: number; year: number; month: number; day: number }[], ); - const posExecutive = await AppDataSource.query("CALL GetProfileSalaryExecutive(?)", [ + const posExecutive = await AppDataSource.query("CALL GetProfileSalaryExecutive(?, ?)", [ profileId, + _currentDate ]); const _posExecutive = posExecutive.length > 0 ? posExecutive[0] : []; const mapPosExecutive = diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index 9ad2b02c..a9047ae8 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -29,6 +29,8 @@ import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; import { Command } from "../entities/Command"; import { OrgRoot } from "../entities/OrgRoot"; +import Extension from "../interfaces/extension"; + @Route("api/v1/org/profile-employee/salary") @Tags("ProfileSalary") @Security("bearerAuth") @@ -141,8 +143,16 @@ export class ProfileSalaryEmployeeController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [ + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; + if (profile && profile?.isLeave) { + _currentDate = profile && profile.leaveDate + ? Extension.toDateOnlyString(profile.leaveDate) + : _currentDate + } + const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?, ?)", [ profile.id, + _currentDate ]); const _position = position.length > 0 ? position[0] : []; const mapPosition = @@ -173,8 +183,9 @@ export class ProfileSalaryEmployeeController extends Controller { [] as { name: string; days: number; year: number; month: number; day: number }[], ); - const posLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?)", [ + const posLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?, ?)", [ profile.id, + _currentDate ]); const _posLevel = posLevel.length > 0 ? posLevel[0] : []; const mapPosLevel = @@ -216,8 +227,22 @@ export class ProfileSalaryEmployeeController extends Controller { @Get("tenure/{profileId}") public async getPositionTenure(@Path() profileId: string, @Request() req: RequestWithUser) { - const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [ + const _profile = await this.profileRepo.findOne({ + where: { id: profileId } + }) + if (!_profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const CURRENT_DATE = await AppDataSource.query("SELECT CURRENT_DATE() as today"); + let _currentDate = CURRENT_DATE[0].today; + if (_profile && _profile?.isLeave) { + _currentDate = _profile && _profile.leaveDate + ? Extension.toDateOnlyString(_profile.leaveDate) + : _currentDate + } + const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?, ?)", [ profileId, + _currentDate ]); const _position = position.length > 0 ? position[0] : []; const mapPosition = @@ -248,8 +273,9 @@ export class ProfileSalaryEmployeeController extends Controller { [] as { name: string; days: number; year: number; month: number; day: number }[], ); - const posLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?)", [ + const posLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?, ?)", [ profileId, + _currentDate ]); const _posLevel = posLevel.length > 0 ? posLevel[0] : []; const mapPosLevel = diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts index 38985d89..26a12465 100644 --- a/src/interfaces/extension.ts +++ b/src/interfaces/extension.ts @@ -381,6 +381,12 @@ class Extension { if (val >= 90 && val <= 100) return "ดีเด่น"; else return "-"; } + + public static toDateOnlyString(date: Date): string { + return date.getFullYear() + "-" + + String(date.getMonth() + 1).padStart(2, "0") + "-" + + String(date.getDate()).padStart(2, "0"); + } } export default Extension;