diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 83b407a6..e6ceb790 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -1,3 +1,5 @@ +import { ProfileSalaryHistory } from "./../entities/ProfileSalaryHistory"; +import { ProfileSalary } from "./../entities/ProfileSalary"; import { Controller, Get, @@ -31,6 +33,7 @@ import { sendToQueueOrg, sendToQueueOrgDraft } from "../services/rabbitmq"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { PermissionOrg } from "../entities/PermissionOrg"; +import { deleteUser } from "../keycloak"; @Route("api/v1/org") @Tags("Organization") @@ -51,6 +54,9 @@ export class OrganizationController extends Controller { private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); private permissionOrgRepository = AppDataSource.getRepository(PermissionOrg); + private profileSalaryRepository = AppDataSource.getRepository(ProfileSalary); + private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory); + private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); /** * API ล้างข้อมูล @@ -7942,4 +7948,123 @@ export class OrganizationController extends Controller { return new HttpSuccess(); } + + /** + * API บันทึกลงประวัติตำแหน่ง + * + * @summary - แก้ไขเหตุผลการลาออก และปลดจาก keycloak (ADMIN) + * + */ + @Get("save/profile/position-history") + async saveRetireToPositionHistory(request: RequestWithUser) { + const profileLeave = await this.profileRepo.find({ + where: { + isLeave: true, + leaveType: "RETIRE", + }, + relations: [ + "posType", + "posLevel", + "current_holders", + "current_holders.orgRoot", + "current_holders.orgChild1", + "current_holders.orgChild2", + "current_holders.orgChild3", + "current_holders.orgChild4", + "current_holders.positions", + "current_holders.positions.posExecutive", + ], + }); + + const batchSize = 1000; + for (let i = 0; i < profileLeave.length; i += batchSize) { + const batch = profileLeave.slice(i, i + batchSize); + await Promise.all( + batch.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: "-", + positionExecutive: "-", + positionType: "-", + positionLevel: "-", + amountSpecial: null, + orgRoot: null, + orgChild1: null, + orgChild2: null, + orgChild3: null, + orgChild4: null, + commandYear: new Date().getFullYear() + 543, + commandDateAffect: profile.dateLeave, + commandCode: "16", + commandName: "พ้นจากราชการ", + posNoAbb: "-", + 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, + }; + + delete data.id; + + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + data.dateGovernment = profile.dateLeave; + await this.profileSalaryRepository.save(data); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history); + }), + ); + } + + return new HttpSuccess(); + } + + /** + * API แก้ไขเหตุผลการลาออก และปลดจาก keycloak + * + * @summary - แก้ไขเหตุผลการลาออก และปลดจาก keycloak (ADMIN) + * + */ + @Get("update/profile/leave-reason") + async updateRetireReason() { + const profileLeave = await this.profileRepo.find({ + where: { + isLeave: true, + leaveType: "RETIRE", + }, + }); + + const batchSize = 1000; + for (let i = 0; i < profileLeave.length; i += batchSize) { + const batch = profileLeave.slice(i, i + batchSize); + await Promise.all( + batch.map(async (profile) => { + const delUserKeycloak = await deleteUser(profile.keycloak); + if (delUserKeycloak) { + profile.leaveReason = "เกษียณอายุราชการ"; + profile.keycloak = ""; + profile.isActive = false; + profile.roleKeycloaks = []; + await this.profileRepo.save(profile); + } + }), + ); + } + + return new HttpSuccess(); + } }