From aadcec440eee783adb7742108fd2e589a1b29756 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 3 Oct 2025 17:04:05 +0700 Subject: [PATCH] fix sort registry and add script update retire emp / position select --- src/controllers/OrganizationController.ts | 190 ++++++++++++++++++++-- src/controllers/ProfileController.ts | 2 +- 2 files changed, 179 insertions(+), 13 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index c12b1a72..3cb257c7 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -1,3 +1,7 @@ +import { ProfileEmployee } from "./../entities/ProfileEmployee"; +import { EmployeePosition } from "./../entities/EmployeePosition"; +import { EmployeePosMaster } from "./../entities/EmployeePosMaster"; +import { Position } from "./../entities/Position"; import { ProfileSalaryHistory } from "./../entities/ProfileSalaryHistory"; import { ProfileSalary } from "./../entities/ProfileSalary"; import { @@ -34,7 +38,10 @@ import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { PermissionOrg } from "../entities/PermissionOrg"; import { deleteUser } from "../keycloak"; - +import { + CreatePosMasterHistoryEmployee, + CreatePosMasterHistoryOfficer, +} from "../services/PositionService"; @Route("api/v1/org") @Tags("Organization") @Security("bearerAuth") @@ -56,7 +63,11 @@ export class OrganizationController extends Controller { private permissionOrgRepository = AppDataSource.getRepository(PermissionOrg); private profileSalaryRepository = AppDataSource.getRepository(ProfileSalary); private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory); + private positionRepository = AppDataSource.getRepository(Position); + private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); + private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); + private employeePositionRepository = AppDataSource.getRepository(EmployeePosition); /** * API ล้างข้อมูล * @@ -7929,38 +7940,124 @@ export class OrganizationController extends Controller { */ @Get("delete/profile/org/{orgRevisionId}") async deleteRetireInOrg(@Path() orgRevisionId: string, @Request() request: RequestWithUser) { - const posMasters = await this.posMasterRepository.find({ + // const posMasters = await this.posMasterRepository.find({ + // where: { + // orgRevisionId: orgRevisionId, + // current_holder: { + // isLeave: true, + // }, + // }, + // }); + + // let check = 0; + // await Promise.all( + // posMasters.map(async (posMaster) => { + // posMaster.current_holderId = null; + // await this.posMasterRepository.save(posMaster); + // check += 1; + // }), + // ); + + const posMastersEmployee = await this.employeePosMasterRepository.find({ where: { orgRevisionId: orgRevisionId, current_holder: { isLeave: true, + leaveType: "RETIRE", }, + positions: { positionIsSelected: true }, }, }); let check = 0; await Promise.all( - posMasters.map(async (posMaster) => { + posMastersEmployee.map(async (posMaster) => { posMaster.current_holderId = null; - await this.posMasterRepository.save(posMaster); + if (posMaster.positions) { + for (const position of posMaster.positions) { + position.positionIsSelected = false; + await this.employeePositionRepository.save(position); + check += 1; + } + } + await this.employeePosMasterRepository.save(posMaster); + await CreatePosMasterHistoryEmployee(posMaster.id, null); check += 1; }), ); - // จำนวนคนที่ถูกลบออกจากโครงสร้าง - const total = posMasters.length; + const total = posMastersEmployee.length; return new HttpSuccess({ total, successAmount: check }); } /** * API บันทึกลงประวัติตำแหน่ง * - * @summary - แก้ไขเหตุผลการลาออก และปลดจาก keycloak (ADMIN) + * @summary - บันทึกลงประวัติตำแหน่ง (ADMIN) * */ @Get("save/profile/position-history") async saveRetireToPositionHistory(@Request() request: RequestWithUser) { - const profileLeave = await this.profileRepo.find({ + // const profileLeave = await this.profileRepo.find({ + // where: { + // isLeave: true, + // leaveType: "RETIRE", + // }, + // }); + + // let check: number = 0; + // await Promise.all( + // profileLeave.map(async (profile: any) => { + // const dest_item = await this.profileSalaryRepository.findOne({ + // where: { profileId: profile.id }, + // order: { order: "DESC" }, + // }); + // const data: any = { + // order: dest_item == null ? 1 : dest_item.order + 1, + // amount: null, + // positionSalaryAmount: null, + // mouthSalaryAmount: null, + // profileId: profile.id, + // posNo: null, + // positionExecutive: null, + // positionType: null, + // positionLevel: null, + // amountSpecial: null, + // orgRoot: null, + // orgChild1: null, + // orgChild2: null, + // orgChild3: null, + // orgChild4: null, + // commandYear: new Date().getFullYear() + 543, + // commandDateAffect: profile.dateLeave, + // commandCode: "16", + // commandName: "พ้นจากราชการ", + // posNoAbb: null, + // isEntry: false, + // positionName: "เกษียณอายุราชการ", + // createdUserId: request.user.sub, + // createdFullName: request.user.name, + // lastUpdateUserId: request.user.sub, + // lastUpdateFullName: request.user.name, + // createdAt: new Date(), + // lastUpdatedAt: new Date(), + // remark: "ประกาศคณะอนุกรรมการสามัญข้าราชการกรุงเทพมหานครสามัญ ลว. 31 มี.ค. 68", // script เกษียณจริง ๆ ให้เอา “วันที่ประกาศเกษียณฉบับแรก” มาลงในเอกสารอ้างอิง + // isGovernment: false, + // }; + + // const history = new ProfileSalaryHistory(); + // Object.assign(history, { ...data, id: undefined }); + // data.dateGovernment = profile.dateLeave; + // const savedData = await this.profileSalaryRepository.save(data); + + // history.profileSalaryId = savedData.id; + // await this.salaryHistoryRepo.save(history); + + // check += 1; + // }), + // ); + + const profileLeave = await this.profileEmployeeRepo.find({ where: { isLeave: true, leaveType: "RETIRE", @@ -7979,7 +8076,7 @@ export class OrganizationController extends Controller { amount: null, positionSalaryAmount: null, mouthSalaryAmount: null, - profileId: profile.id, + profileEmployeeId: profile.id, posNo: null, positionExecutive: null, positionType: null, @@ -8018,7 +8115,6 @@ export class OrganizationController extends Controller { check += 1; }), ); - // จำนวนคนที่บันทึกลงประวัติตำแหน่ง const total = profileLeave.length; // จำนวนคนที่ถูกบันทึกลงประวัติตำแหน่งสำเร็จ @@ -8033,7 +8129,37 @@ export class OrganizationController extends Controller { */ @Get("update/profile/leave-reason") async updateRetireReason() { - const profileLeave = await this.profileRepo.find({ + // const profileLeave = await this.profileRepo.find({ + // where: { + // isLeave: true, + // leaveType: "RETIRE", + // }, + // }); + + // let check: number = 0; + // let notDelete: string[] = []; + // await Promise.all( + // profileLeave.map(async (profile) => { + // profile.leaveReason = "เกษียณอายุราชการ"; + // profile.isActive = false; + + // if (profile.keycloak != null && profile.keycloak != "") { + // const delUserKeycloak = await deleteUser(profile.keycloak); + // if (delUserKeycloak) { + // profile.keycloak = ""; + // profile.roleKeycloaks = []; + // check += 1; + // } else { + // // push array not delete + // notDelete.push(profile.keycloak); + // } + // } + + // await this.profileRepo.save(profile); + // }), + // ); + + const profileLeave = await this.profileEmployeeRepo.find({ where: { isLeave: true, leaveType: "RETIRE", @@ -8059,7 +8185,7 @@ export class OrganizationController extends Controller { } } - await this.profileRepo.save(profile); + await this.profileEmployeeRepo.save(profile); }), ); @@ -8067,4 +8193,44 @@ export class OrganizationController extends Controller { const total = profileLeave.length; return new HttpSuccess({ total, successAmount: check, notDelete }); } + + /** + * API ลบตำแหน่งที่ครองอยู่ของข้าราชการที่รันเกษียณไปแล้ว + * + * @summary - ลบตำแหน่งที่ครองอยู่ของข้าราชการที่รันเกษียณไปแล้ว (ADMIN) + * + */ + @Get("update/org/position/remove-select/{orgRevisionId}") + async updatePositionSelectOrg( + @Path() orgRevisionId: string, + @Request() request: RequestWithUser, + ) { + const posMasters = await this.posMasterRepository.find({ + where: { + orgRevisionId: orgRevisionId, + current_holderId: IsNull(), + positions: { positionIsSelected: true }, + }, + relations: ["positions"], + }); + + // update position positionIsSelected = 0 + let check = 0; + await Promise.all( + posMasters.map(async (posMaster) => { + if (posMaster.positions && posMaster.positions.length > 0) { + for (const position of posMaster.positions) { + position.positionIsSelected = false; + await this.positionRepository.save(position); + check += 1; + } + await CreatePosMasterHistoryOfficer(posMaster.id, null); + } + }), + ); + + // จำนวนคนที่ถูกลบออกจากโครงสร้าง + const total = posMasters.length; + return new HttpSuccess({ total, successAmount: check }); + } } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index a7ace8f7..783b568d 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -6587,7 +6587,7 @@ export class ProfileController extends Controller { }), ) .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") - .orderBy("sort_order", "ASC") + .orderBy(`${sortBy ? sortBy : "sort_order"}`, `${sort}`) .addOrderBy("orgRoot.orgRootOrder", sort) .addOrderBy("orgChild1.orgChild1Order", sort) .addOrderBy("orgChild2.orgChild2Order", sort)