diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index cf86f369..78c01910 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -259,7 +259,13 @@ export async function removeProfileInOrganize(profileId: string, type: string) { .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - if (!currentRevision) { + const draftRevision = await AppDataSource.getRepository(OrgRevision) + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = true") + .andWhere("orgRevision.orgRevisionIsCurrent = false") + .getOne(); + + if (!currentRevision && !draftRevision) { return; } if (type === "OFFICER") { @@ -276,7 +282,20 @@ export async function removeProfileInOrganize(profileId: string, type: string) { .where("id = :id", { id: findProfileInposMaster?.id }) .execute(); - if (!findProfileInposMaster) { + const findProfileInposMasterDraft = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: draftRevision?.id }) + .andWhere("posMaster.current_holderId = :profileId", { profileId }) + .getOne(); + + await AppDataSource.getRepository(PosMaster) + .createQueryBuilder() + .update(PosMaster) + .set({ next_holderId: null }) + .where("id = :id", { id: findProfileInposMasterDraft?.id }) + .execute(); + + if (!findProfileInposMaster && !findProfileInposMasterDraft) { return; } const findPosition = await AppDataSource.getRepository(Position)