From 92cab69503078a11eb3714e9dbcc1c14f75c3fe1 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 13 Mar 2025 17:41:10 +0700 Subject: [PATCH] =?UTF-8?q?=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?=20(USER)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileSalaryController.ts | 124 +++++++++++++++++- .../ProfileSalaryEmployeeController.ts | 34 +++++ 2 files changed, 155 insertions(+), 3 deletions(-) diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 9527a350..7a4e130e 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -319,11 +319,129 @@ export class ProfileSalaryController extends Controller { return new HttpSuccess(record); } + @Get("tenure/user") + public async getPositionTenureUser(@Request() request: { user: Record }) { + // const sql_mode = await AppDataSource.query( + // "SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));", + // ); + const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [profile.id]); + const _position = position.length > 0 ? position[0] : []; + + const mapPosition = + _position.length > 1 + ? _position.slice(1).map((curr: any, index: number) => ({ + days: curr.days_diff ? Number(curr.days_diff) : 0, + name: _position[index]?.positionName, + })) + : []; + + const groupMapPosition = mapPosition.reduce( + (acc: any, curr: any) => { + let existing = acc.find((item: any) => item.name === curr.name); + + if (existing) { + existing.days += curr.days; + } else { + existing = { name: curr.name, days: curr.days }; + acc.push(existing); + } + + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.floor(existing.days % 30.4375); + + return acc; + }, + [] as { name: string; days: number; year: number; month: number; day: number }[], + ); + + const posLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [profile.id]); + const _posLevel = posLevel.length > 0 ? posLevel[0] : []; + const mapPosLevel = + _posLevel.length > 1 + ? _posLevel.slice(1).map((curr: any, index: number) => ({ + days: curr.days_diff ? Number(curr.days_diff) : 0, + name: + !_posLevel[index]?.positionType && _posLevel[index]?.positionCee + ? `ระดับ ${_posLevel[index]?.positionCee.trim()}` + : _posLevel[index]?.positionType == "บริหาร" || + _posLevel[index]?.positionType == "อำนวยการ" + ? `${_posLevel[index]?.positionType}${_posLevel[index]?.positionLevel}` + : _posLevel[index]?.positionLevel, + })) + : []; + + const groupMapPosLevel = mapPosLevel.reduce( + (acc: any, curr: any) => { + let existing = acc.find((item: any) => item.name === curr.name); + + if (existing) { + existing.days += curr.days; + } else { + existing = { name: curr.name, days: curr.days }; + acc.push(existing); + } + + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.floor(existing.days % 30.4375); + + return acc; + }, + [] as { name: string; days: number; year: number; month: number; day: number }[], + ); + + const posExecutive = await AppDataSource.query("CALL GetProfileSalaryExecutive(?)", [ + profile.id, + ]); + const _posExecutive = posExecutive.length > 0 ? posExecutive[0] : []; + const mapPosExecutive = + _posExecutive.length > 1 + ? _posExecutive.slice(1).map((curr: any, index: number) => ({ + days: curr.days_diff ? Number(curr.days_diff) : 0, + name: _posExecutive[index]?.positionExecutive, + })) + : []; + + const groupMapPosExecutive = mapPosExecutive.reduce( + (acc: any, curr: any) => { + let existing = acc.find((item: any) => item.name === curr.name); + + if (existing) { + existing.days += curr.days; + } else { + existing = { name: curr.name, days: curr.days }; + acc.push(existing); + } + + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.floor(existing.days % 30.4375); + + return acc; + }, + [] as { name: string; days: number; year: number; month: number; day: number }[], + ); + + return new HttpSuccess({ + position: groupMapPosition, + posLevel: groupMapPosLevel, + posExecutive: groupMapPosExecutive, + }); + } + @Get("tenure/{profileId}") public async getPositionTenure(@Path() profileId: string, @Request() req: RequestWithUser) { - const sql_mode = await AppDataSource.query( - "SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));", - ); + // 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 _position = position.length > 0 ? position[0] : []; diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index a6e076ec..59e97ef1 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -87,6 +87,40 @@ export class ProfileSalaryEmployeeController extends Controller { return new HttpSuccess(record); } + @Get("tenure/user") + public async getPositionTenureUser(@Request() request: { user: Record }) { + const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [ + profile.id, + ]); + const _position = position.length > 0 ? position[0] : []; + const mapPosition = + _position.length > 1 + ? _position.slice(1).map((curr: any, index: number) => ({ + year: curr.Years ? Math.floor(Number(curr.Years)) : 0, + month: curr.Months ? Math.floor(Number(curr.Months)) : 0, + day: curr.Days ? Math.floor(Number(curr.Days)) : 0, + name: _position[index]?.positionName, + })) + : []; + + const posLevel: any = [ + // { + // year: 3, + // month: 0, + // day: 0, + // name: "ส 1", + // } + ]; + return new HttpSuccess({ + position: mapPosition, + posLevel: posLevel, + }); + } + @Get("tenure/{profileId}") public async getPositionTenure(@Path() profileId: string, @Request() req: RequestWithUser) { const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [