diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index d2255fa0..bec9eb16 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -1452,7 +1452,7 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); } let dateLeave_: any = body.date; - await removeProfileInOrganize(profile.id); + await removeProfileInOrganize(profile.id,"OFFICER"); profile.isLeave = true; profile.leaveReason = "คำสั่งให้ข้าราชการออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐานที่กำหนด"; @@ -1525,7 +1525,7 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); } let dateLeave_: any = body.date; - await removeProfileInOrganize(profile.id); + await removeProfileInOrganize(profile.id,"OFFICER"); profile.isLeave = true; profile.leaveReason = "ได้รับโทษทางวินัย ปลดออกจากราชการ"; profile.dateLeave = dateLeave_; @@ -1597,7 +1597,7 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); } let dateLeave_: any = body.date; - await removeProfileInOrganize(profile.id); + await removeProfileInOrganize(profile.id,"OFFICER"); profile.isLeave = true; profile.leaveReason = "ได้รับโทษทางวินัย ไล่ออกจากราชการ"; profile.dateLeave = dateLeave_; @@ -1683,7 +1683,7 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); } let dateLeave_: any = body.date; - await removeProfileInOrganize(profile.id); + await removeProfileInOrganize(profile.id,"OFFICER"); profile.isLeave = true; profile.leaveReason = "ได้รับโทษทางวินัย พักจากราชการ"; profile.dateLeave = dateLeave_; @@ -1755,7 +1755,7 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); } let dateLeave_: any = body.date; - await removeProfileInOrganize(profile.id); + await removeProfileInOrganize(profile.id, "OFFICER"); profile.isLeave = true; profile.leaveReason = "ได้รับโทษทางวินัย ให้ออกจากราชการไว้ก่อน"; profile.dateLeave = dateLeave_; @@ -4886,7 +4886,7 @@ export class ProfileController extends Controller { const _null: any = null; profile.isLeave = requestBody.isLeave; if (requestBody.isLeave == true) { - await removeProfileInOrganize(profile.id); + await removeProfileInOrganize(profile.id, "OFFICER"); } if (requestBody.leaveReason != undefined && requestBody.leaveReason != null) { profile.leaveReason = requestBody.leaveReason; diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 5b914e16..e5fae7ab 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -2760,7 +2760,7 @@ export class ProfileEmployeeController extends Controller { profile.leaveReason = requestBody.leaveReason; profile.dateLeave = requestBody.dateLeave; if (requestBody.isLeave == true) { - await removeProfileInOrganize(profile.id); + await removeProfileInOrganize(profile.id,"EMPLOYEE"); } await this.profileRepo.save(profile); diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index 4562e6d5..4b58fdd4 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -2,6 +2,9 @@ import { OrgRevision } from "../entities/OrgRevision"; import { AppDataSource } from "../database/data-source"; import { PosMaster } from "../entities/PosMaster"; import { Position } from "../entities/Position"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { EmployeePosition } from "../entities/EmployeePosition"; +import { In } from "typeorm"; export function calculateAge(start: Date, end = new Date()) { if (start.getTime() > end.getTime()) return null; @@ -90,7 +93,7 @@ export function calculateRetireYear(birthDate: Date) { return yy + 61; } -export async function removeProfileInOrganize(profileId: string) { +export async function removeProfileInOrganize(profileId: string, type:string) { const currentRevision = await AppDataSource.getRepository(OrgRevision) .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = false") @@ -98,37 +101,74 @@ export async function removeProfileInOrganize(profileId: string) { .getOne(); if (!currentRevision) { - throw new Error("Not Found"); + throw new Error("ไม่พบข้อมูลโครงสร้างอัตรากำลังที่เผยแพร่อยู่"); } + if (type === "OFFICER") { + const findProfileInposMaster = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id }) + .andWhere("posMaster.current_holderId = :profileId", { profileId }) + .getOne(); + + await AppDataSource.getRepository(PosMaster) + .createQueryBuilder() + .update(PosMaster) + .set({ current_holderId: null }) + .where("id = :id", { id: findProfileInposMaster?.id }) + .execute(); - const findProfileInposMaster = await AppDataSource.getRepository(PosMaster) - .createQueryBuilder("posMaster") - .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id }) - .andWhere("posMaster.current_holderId = :profileId", { profileId }) - .getOne(); - await AppDataSource.getRepository(PosMaster) - .createQueryBuilder() - .update(PosMaster) - .set({ current_holderId: null }) - .where("id = :id", { id: findProfileInposMaster?.id }) - .execute(); - - if (!findProfileInposMaster) { - throw new Error("Not Found"); + if (!findProfileInposMaster) { + throw new Error("ไม่พบข้อมูลบุคคลนี้ภายในโครงสร้างอัตรากำลัง"); + } + const findPosition = await AppDataSource.getRepository(Position) + .createQueryBuilder("position") + .where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id }) + .getMany(); + + if (!findPosition) { + throw new Error("ไม่พบข้อมูลตำแหน่งของบุคคลนี้ภายในโครงสร้างอัตรากำลัง"); + } + await AppDataSource.getRepository(Position) + .createQueryBuilder() + .update(Position) + .set({ positionIsSelected: false }) + .where("id IN (:...ids)", { ids: findPosition.map((item) => item.id) }) + .execute(); + } + if (type === "EMPLOYEE") { + const findProfileInEmpPosMaster = await AppDataSource.getRepository(EmployeePosMaster) + .createQueryBuilder("employeePosMaster") + .where("employeePosMaster.orgRevisionId = :orgRevisionId", { + orgRevisionId: currentRevision?.id, + }) + .andWhere("employeePosMaster.current_holderId = :profileId", { profileId }) + .getOne(); + await AppDataSource.getRepository(EmployeePosMaster) + .createQueryBuilder() + .update(EmployeePosMaster) + .set({ current_holderId: null }) + .where("id = :id", { id: findProfileInEmpPosMaster?.id }) + .execute(); + + if (!findProfileInEmpPosMaster) { + throw new Error("ไม่พบข้อมูลบุคคลนี้ภายในโครงสร้างอัตรากำลัง"); + } + const findEmpPosition = await AppDataSource.getRepository(EmployeePosition) + .createQueryBuilder("employeePosition") + .where("employeePosition.posMasterId = :posMasterId", { + posMasterId: findProfileInEmpPosMaster?.id, + }) + .getMany(); + + if (!findEmpPosition) { + throw new Error("ไม่พบข้อมูลตำแหน่งของบุคคลนี้ภายในโครงสร้างอัตรากำลัง"); + } + + await AppDataSource.getRepository(EmployeePosition) + .createQueryBuilder() + .update(EmployeePosition) + .set({ positionIsSelected: false }) + .where("id IN (:...ids)", { ids: findEmpPosition.map((item) => item.id) }) + .execute(); } - const findPosition = await AppDataSource.getRepository(Position) - .createQueryBuilder("position") - .where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id }) - .getOne(); - - if (!findPosition) { - throw new Error("Not Found"); - } - - await AppDataSource.getRepository(Position) - .createQueryBuilder() - .update(Position) - .set({ positionIsSelected: false }) - .where("id = :id", { id: findPosition?.id }) - .execute(); }