ฟังกชั่นกลางดึงคนออกจากโครงสร้าง

This commit is contained in:
AdisakKanthawilang 2024-07-18 21:21:24 +07:00
parent 1718176911
commit 51e6655252

View file

@ -1,3 +1,7 @@
import { OrgRevision } from "../entities/OrgRevision";
import { AppDataSource } from "../database/data-source";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
export function calculateAge(start: Date, end = new Date()) {
if (start.getTime() > end.getTime()) return null;
@ -86,3 +90,45 @@ export function calculateRetireYear(birthDate: Date) {
return yy + 61;
}
export async function removeProfileInOrganize(profileId: string) {
const currentRevision = await AppDataSource.getRepository(OrgRevision)
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
if (!currentRevision) {
throw new Error("Not Found");
}
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");
}
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();
}