fix ลบคนออกจากอัตรากำลัง

This commit is contained in:
AdisakKanthawilang 2024-07-19 15:51:08 +07:00
parent 85c6093970
commit ac01837c69
3 changed files with 78 additions and 38 deletions

View file

@ -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();
}