diff --git a/src/controllers/EmployeeTempPositionController.ts b/src/controllers/EmployeeTempPositionController.ts index deb32067..b2e2da56 100644 --- a/src/controllers/EmployeeTempPositionController.ts +++ b/src/controllers/EmployeeTempPositionController.ts @@ -530,85 +530,6 @@ export class EmployeeTempPositionController extends Controller { posMaster.orgRevisionId = orgRoot.orgRevisionId; SName = orgRoot.orgRootShortName; } - - const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgRoot: { orgRootShortName: SName }, - orgChild1Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgRoot"], - }); - // if (chk_SName0 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild1: { orgChild1ShortName: SName }, - orgChild2Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild1"], - }); - // if (chk_SName1 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild2: { orgChild2ShortName: SName }, - orgChild3Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild2"], - }); - // if (chk_SName2 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild3: { orgChild3ShortName: SName }, - orgChild4Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild3"], - }); - // if (chk_SName3 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild4: { orgChild4ShortName: SName }, - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild4"], - }); - // if (chk_SName4 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } const before = null; posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; @@ -652,7 +573,9 @@ export class EmployeeTempPositionController extends Controller { @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "SYS_ORG_TEMP"); - const posMaster = await this.employeeTempPosMasterRepository.findOne({ where: { id: id } }); + const posMaster: any = await this.employeeTempPosMasterRepository.findOne({ + where: { id: id }, + }); if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลอัตรากำลัง"); } @@ -738,90 +661,6 @@ export class EmployeeTempPositionController extends Controller { posMaster.orgRevisionId = orgRoot.orgRevisionId; SName = orgRoot.orgRootShortName; } - - const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgRoot: { orgRootShortName: SName }, - orgChild1Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgRoot"], - }); - // if (chk_SName0 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild1: { orgChild1ShortName: SName }, - orgChild2Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild1"], - }); - // if (chk_SName1 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild2: { orgChild2ShortName: SName }, - orgChild3Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild2"], - }); - // if (chk_SName2 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild3: { orgChild3ShortName: SName }, - orgChild4Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild3"], - }); - // if (chk_SName3 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } - - const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild4: { orgChild4ShortName: SName }, - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild4"], - }); - // if (chk_SName4 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } const before = structuredClone(posMaster); posMaster.createdUserId = request.user.sub; //สงสัยว่าทำให้ bug แก้ไขไม่ได้ posMaster.createdFullName = request.user.name; @@ -853,43 +692,6 @@ export class EmployeeTempPositionController extends Controller { return new HttpSuccess(posMaster.id); } - /** - * API รายละเอียดอัตรากำลัง - * - * @summary ORG_ - รายละเอียดอัตรากำลัง (ADMIN) - * - */ - // @Get("position/{id}") - // async detailEmpPosition(@Path() id: string) { - // const posMaster = await this.employeeTempPosMasterRepository.findOne({ - // where: { id }, - // }); - // if (!posMaster) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - // } - // const positions = await this.employeePositionRepository.find({ - // where: { posMasterTempId: posMaster.id }, - // relations: ["posType", "posLevel"], - // order: { lastUpdatedAt: "ASC" }, - // }); - // const formattedData = { - // id: posMaster.id, - // posMasterNoPrefix: posMaster.posMasterNoPrefix, - // posMasterNo: posMaster.posMasterNo, - // posMasterNoSuffix: posMaster.posMasterNoSuffix, - // positions: positions.map((position) => ({ - // id: position.id, - // positionName: position.positionName, - // posTypeId: position.posTypeId, - // posTypeName: position.posType == null ? null : position.posType.posTypeName, - // posLevelId: position.posLevelId, - // posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, - // positionIsSelected: position.positionIsSelected, - // })), - // }; - // return new HttpSuccess(formattedData); - // } - /** * API ลบอัตรากำลัง * @@ -1551,14 +1353,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgRootId: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgRootShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if ( posMaster.orgRootId == org.id && posMaster.orgChild1Id == null && @@ -1580,14 +1374,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild1Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild1ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if ( posMaster.orgChild1Id == org.id && posMaster.orgChild2Id == null && @@ -1608,14 +1394,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild2Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild2ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if ( posMaster.orgChild2Id == org.id && posMaster.orgChild3Id == null && @@ -1635,14 +1413,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild3Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild3ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if (posMaster.orgChild3Id == org.id && posMaster.orgChild4Id == null) change = false; posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; @@ -1657,14 +1427,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild4Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild4ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if (posMaster.orgChild4Id == org.id) change = false; posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index 59e97ef1..d5b301c7 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -100,24 +100,67 @@ export class ProfileSalaryEmployeeController extends Controller { 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, + days: curr.days_diff ? Number(curr.days_diff) : 0, name: _position[index]?.positionName, })) : []; - const posLevel: any = [ - // { - // year: 3, - // month: 0, - // day: 0, - // name: "ส 1", - // } - ]; + 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); + } + + 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 GetProfileEmployeeSalaryLevel(?)", [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]?.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); + } + + 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: mapPosition, - posLevel: posLevel, + position: groupMapPosition, + posLevel: groupMapPosLevel, }); } @@ -130,24 +173,67 @@ export class ProfileSalaryEmployeeController extends Controller { 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, + days: curr.days_diff ? Number(curr.days_diff) : 0, name: _position[index]?.positionName, })) : []; - const posLevel: any = [ - // { - // year: 3, - // month: 0, - // day: 0, - // name: "ส 1", - // } - ]; + 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); + } + + 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 GetProfileEmployeeSalaryLevel(?)", [profileId]); + 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]?.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); + } + + 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: mapPosition, - posLevel: posLevel, + position: groupMapPosition, + posLevel: groupMapPosLevel, }); } diff --git a/src/entities/EmployeeTempPosMaster.ts b/src/entities/EmployeeTempPosMaster.ts index 82112df1..7486f15b 100644 --- a/src/entities/EmployeeTempPosMaster.ts +++ b/src/entities/EmployeeTempPosMaster.ts @@ -235,13 +235,13 @@ export class EmployeeTempPosMaster extends EntityBase { export class CreateEmployeeTempPosMaster { @Column() - posMasterNoPrefix: string; + posMasterNoPrefix?: string | null; @Column() - posMasterNo: number; + posMasterNo?: number | null; @Column() - posMasterNoSuffix: string; + posMasterNoSuffix?: string | null; @Column("uuid") positions: CreateEmployeePosDict[]; diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index b55d8e72..cfe76e6d 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -184,12 +184,34 @@ class CheckAuth { }); } public async checkOrg(token: any, keycloakId: string) { - const redisClient = await this.redis.createClient({ - host: process.env.REDIS_HOST, - port: process.env.REDIS_PORT, - }); - const getAsync = promisify(redisClient.get).bind(redisClient); try { + // Validate required environment variables + const REDIS_HOST = process.env.REDIS_HOST; + const REDIS_PORT = process.env.REDIS_PORT ? Number(process.env.REDIS_PORT) : 6379; + + if (!REDIS_HOST) { + throw new Error("REDIS_HOST is not set in environment variables"); + } + + console.log(`[REDIS] Connecting to Redis at ${REDIS_HOST}:${REDIS_PORT}`); + + // Create Redis client + const redisClient = this.redis.createClient({ + socket: { + host: REDIS_HOST, + port: REDIS_PORT, + }, + }); + + redisClient.on("error", (err: any) => { + console.error("[REDIS] Connection error:", err.message); + }); + + await redisClient.connect(); + console.log("[REDIS] Connected successfully!"); + + const getAsync = promisify(redisClient.get).bind(redisClient); + let reply = await getAsync("org_" + keycloakId); if (reply != null) { reply = JSON.parse(reply);