refactor transaction

This commit is contained in:
Warunee Tamkoo 2026-05-05 12:32:21 +07:00
parent 869bb093a3
commit a6d3df9904
2 changed files with 251 additions and 499 deletions

View file

@ -1,4 +1,4 @@
import { In } from "typeorm"; import { EntityManager, In } from "typeorm";
import { SavePosMasterHistory } from "./../interfaces/OrgMapping"; import { SavePosMasterHistory } from "./../interfaces/OrgMapping";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { EmployeePosMaster } from "../entities/EmployeePosMaster";
@ -17,105 +17,118 @@ export async function CreatePosMasterHistoryOfficer(
request: RequestWithUser | null, request: RequestWithUser | null,
type?: string | null, type?: string | null,
positionData?: { positionId?: string } | null, positionData?: { positionId?: string } | null,
manager?: EntityManager,
): Promise<boolean> { ): Promise<boolean> {
try { const execute = async (transactionManager: EntityManager) => {
await AppDataSource.transaction(async (manager) => { const repoPosmaster = transactionManager.getRepository(PosMaster);
const repoPosmaster = manager.getRepository(PosMaster); const repoHistory = transactionManager.getRepository(PosMasterHistory);
const repoHistory = manager.getRepository(PosMasterHistory); const repoOrgRevision = transactionManager.getRepository(OrgRevision);
const repoOrgRevision = manager.getRepository(OrgRevision); const repoPosition = transactionManager.getRepository(Position);
const repoPosition = manager.getRepository(Position);
const pm = await repoPosmaster.findOne({ const pm = await repoPosmaster.findOne({
where: { id: posMasterId }, where: { id: posMasterId },
relations: [ relations: [
"positions", "positions",
"positions.posLevel", "positions.posLevel",
"positions.posType", "positions.posType",
"positions.posExecutive", "positions.posExecutive",
"orgRoot", "orgRoot",
"orgChild1", "orgChild1",
"orgChild2", "orgChild2",
"orgChild3", "orgChild3",
"orgChild4", "orgChild4",
"current_holder", "current_holder",
"next_holder", "next_holder",
], ],
});
if (!pm) return false;
if (!pm.ancestorDNA) return false;
const checkCurrentRevision = await repoOrgRevision.findOne({
where: {
id: pm.orgRevisionId,
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
});
const _null: any = null;
const h = new PosMasterHistory();
// query position โดยตรงจาก positionRepository
let selectedPosition: Position | null = null;
if (positionData?.positionId) {
selectedPosition = await repoPosition.findOne({
where: { id: positionData.positionId },
relations: { posLevel: true, posType: true, posExecutive: true },
});
} else {
// ใช้ logic เดิม หาจาก pm.positions ที่ positionIsSelected = true
selectedPosition =
pm.positions.length > 0
? pm.positions.find((p) => p.positionIsSelected === true) ?? null
: null;
}
h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null;
if (!type || type != "DELETE") {
if (checkCurrentRevision) {
h.prefix = pm.current_holder?.prefix || _null;
h.firstName = pm.current_holder?.firstName || _null;
h.lastName = pm.current_holder?.lastName || _null;
h.profileId = pm.current_holder?.id || _null;
} else {
h.prefix = pm.next_holder?.prefix || _null;
h.firstName = pm.next_holder?.firstName || _null;
h.lastName = pm.next_holder?.lastName || _null;
}
h.position = selectedPosition?.positionName ?? _null;
h.posType = selectedPosition?.posType?.posTypeName ?? _null;
h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null;
}
h.rootDnaId = pm.orgRoot?.ancestorDNA || _null;
h.child1DnaId = pm.orgChild1?.ancestorDNA || _null;
h.child2DnaId = pm.orgChild2?.ancestorDNA || _null;
h.child3DnaId = pm.orgChild3?.ancestorDNA || _null;
h.child4DnaId = pm.orgChild4?.ancestorDNA || _null;
h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null;
h.posMasterNo = pm.posMasterNo ?? _null;
h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null;
h.posExecutive = selectedPosition?.posExecutive?.posExecutiveName ?? _null;
h.shortName =
[
pm.orgChild4?.orgChild4ShortName,
pm.orgChild3?.orgChild3ShortName,
pm.orgChild2?.orgChild2ShortName,
pm.orgChild1?.orgChild1ShortName,
pm.orgRoot?.orgRootShortName,
].find((s) => typeof s === "string" && s.trim().length > 0) ?? _null;
const userId = request?.user?.sub ?? "";
const userName = request?.user?.name ?? "system";
h.createdUserId = userId;
h.createdFullName = userName;
h.lastUpdateUserId = userId;
h.lastUpdateFullName = userName;
h.createdAt = new Date();
h.lastUpdatedAt = new Date();
await repoHistory.save(h);
}); });
if (!pm || !pm.ancestorDNA) {
return;
}
const checkCurrentRevision = await repoOrgRevision.findOne({
where: {
id: pm.orgRevisionId,
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
});
const _null: any = null;
const h = new PosMasterHistory();
// query position โดยตรงจาก positionRepository
let selectedPosition: Position | null = null;
if (positionData?.positionId) {
selectedPosition = await repoPosition.findOne({
where: { id: positionData.positionId },
relations: { posLevel: true, posType: true, posExecutive: true },
});
} else {
// ใช้ logic เดิม หาจาก pm.positions ที่ positionIsSelected = true
selectedPosition =
pm.positions.length > 0
? pm.positions.find((p) => p.positionIsSelected === true) ?? null
: null;
}
h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null;
if (!type || type != "DELETE") {
if (checkCurrentRevision) {
h.prefix = pm.current_holder?.prefix || _null;
h.firstName = pm.current_holder?.firstName || _null;
h.lastName = pm.current_holder?.lastName || _null;
h.profileId = pm.current_holder?.id || _null;
} else {
h.prefix = pm.next_holder?.prefix || _null;
h.firstName = pm.next_holder?.firstName || _null;
h.lastName = pm.next_holder?.lastName || _null;
}
h.position = selectedPosition?.positionName ?? _null;
h.posType = selectedPosition?.posType?.posTypeName ?? _null;
h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null;
}
h.rootDnaId = pm.orgRoot?.ancestorDNA || _null;
h.child1DnaId = pm.orgChild1?.ancestorDNA || _null;
h.child2DnaId = pm.orgChild2?.ancestorDNA || _null;
h.child3DnaId = pm.orgChild3?.ancestorDNA || _null;
h.child4DnaId = pm.orgChild4?.ancestorDNA || _null;
h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null;
h.posMasterNo = pm.posMasterNo ?? _null;
h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null;
h.posExecutive = selectedPosition?.posExecutive?.posExecutiveName ?? _null;
h.shortName =
[
pm.orgChild4?.orgChild4ShortName,
pm.orgChild3?.orgChild3ShortName,
pm.orgChild2?.orgChild2ShortName,
pm.orgChild1?.orgChild1ShortName,
pm.orgRoot?.orgRootShortName,
].find((s) => typeof s === "string" && s.trim().length > 0) ?? _null;
const userId = request?.user?.sub ?? "";
const userName = request?.user?.name ?? "system";
h.createdUserId = userId;
h.createdFullName = userName;
h.lastUpdateUserId = userId;
h.lastUpdateFullName = userName;
h.createdAt = new Date();
h.lastUpdatedAt = new Date();
await repoHistory.save(h);
};
try {
if (manager) {
await execute(manager);
return true;
}
await AppDataSource.transaction(async (transactionManager) => {
await execute(transactionManager);
});
return true; return true;
} catch (err) { } catch (err) {
if (manager) {
throw err;
}
console.error("CreatePosMasterHistoryOfficer transaction error:", err); console.error("CreatePosMasterHistoryOfficer transaction error:", err);
return false; return false;
} }

View file

@ -818,77 +818,138 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
// ===== BATCH EXECUTION: save ทีละ batch ===== // ===== BATCH EXECUTION: save ทีละ batch =====
let shouldSkipPublishInTransaction = false;
await AppDataSource.transaction(async (manager) => {
const repoPosmaster = manager.getRepository(PosMaster);
const posMasterAssignRepository = manager.getRepository(PosMasterAssign);
const posMasterActRepository = manager.getRepository(PosMasterAct);
const permissionProfilesRepository = manager.getRepository(PermissionProfile);
const repoEmployeePosmaster = manager.getRepository(EmployeePosMaster);
const repoEmployeeTempPosmaster = manager.getRepository(EmployeeTempPosMaster);
const repoProfile = manager.getRepository(Profile);
const repoProfileEmployee = manager.getRepository(ProfileEmployee);
const employeePositionRepository = manager.getRepository(EmployeePosition);
const repoOrgRevision = manager.getRepository(OrgRevision);
const orgRootRepository = manager.getRepository(OrgRoot);
const child1Repository = manager.getRepository(OrgChild1);
const child2Repository = manager.getRepository(OrgChild2);
const child3Repository = manager.getRepository(OrgChild3);
const child4Repository = manager.getRepository(OrgChild4);
// 4. Batch save posMasterAssign (chunk 500) const targetOrgRevision = await repoOrgRevision
console.time("[AMQ] batch_save_posMasterAssign"); .createQueryBuilder("orgRevision")
if (posMasterAssignsToSave.length > 0) { .setLock("pessimistic_write")
const chunks = chunkArray(posMasterAssignsToSave, 500); .where("orgRevision.id = :id", { id })
for (const chunk of chunks) { .getOne();
await posMasterAssignRepository.save(chunk);
if (!targetOrgRevision) {
shouldSkipPublishInTransaction = true;
return;
} }
}
console.timeEnd("[AMQ] batch_save_posMasterAssign");
// 5. Batch save profiles (chunk 200) if (targetOrgRevision.orgRevisionIsCurrent && !targetOrgRevision.orgRevisionIsDraft) {
console.time("[AMQ] batch_save_profiles"); shouldSkipPublishInTransaction = true;
if (profilesToSave.length > 0) { return;
const chunks = chunkArray(profilesToSave, 200);
for (const chunk of chunks) {
await repoProfile.save(chunk);
} }
}
console.timeEnd("[AMQ] batch_save_profiles");
// 6. Batch update posMasters if (!targetOrgRevision.orgRevisionIsDraft || targetOrgRevision.orgRevisionIsCurrent) {
console.time("[AMQ] batch_update_posMasters"); shouldSkipPublishInTransaction = true;
for (const update of posMasterUpdates) { return;
await repoPosmaster.update(update.id, { }
current_holderId: update.current_holderId,
next_holderId: null,
lastUpdateUserId,
lastUpdateFullName,
lastUpdatedAt,
});
}
console.timeEnd("[AMQ] batch_update_posMasters");
// 7. Batch create history const orgRevisionPublish = await repoOrgRevision
console.time("[AMQ] batch_create_history"); .createQueryBuilder("orgRevision")
for (const id of historyCreateIds) { .setLock("pessimistic_write")
await CreatePosMasterHistoryOfficer(id, null); .where("orgRevision.orgRevisionIsDraft = false")
} .andWhere("orgRevision.orgRevisionIsCurrent = true")
console.timeEnd("[AMQ] batch_create_history"); .getOne();
// Clone oldposMasterAct if (!orgRevisionPublish) {
console.time("[AMQ] clone_oldposMasterAct"); throw new Error("[AMQ] Cannot publish in transaction: no current org revision found");
for (const act of oldposMasterAct) { }
const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? "";
const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? "";
const newParentId = posMasterIdMap.get(parentDNA); const orgRevisionDraft = await repoOrgRevision
const newChildId = posMasterIdMap.get(childDNA); .createQueryBuilder("orgRevision")
.setLock("pessimistic_write")
.where("orgRevision.id = :id", { id })
.andWhere("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false")
.getOne();
if (!newParentId || !newChildId) continue; if (!orgRevisionDraft) {
shouldSkipPublishInTransaction = true;
return;
}
const { id, posMaster, posMasterChild, ...fields } = act; // 4. Batch save posMasterAssign (chunk 500)
console.time("[AMQ] batch_save_posMasterAssign");
if (posMasterAssignsToSave.length > 0) {
const chunks = chunkArray(posMasterAssignsToSave, 500);
for (const chunk of chunks) {
await posMasterAssignRepository.save(chunk);
}
}
console.timeEnd("[AMQ] batch_save_posMasterAssign");
const newAct = { // 5. Batch save profiles (chunk 200)
...fields, console.time("[AMQ] batch_save_profiles");
posMasterId: newParentId, if (profilesToSave.length > 0) {
posMasterChildId: newChildId, const chunks = chunkArray(profilesToSave, 200);
createdAt: new Date(), for (const chunk of chunks) {
createdFullName: user ? user.name : "system", await repoProfile.save(chunk);
createdUserId: user ? user.sub : "system", }
lastUpdatedAt: new Date(), }
lastUpdateFullName: user ? user.name : "system", console.timeEnd("[AMQ] batch_save_profiles");
lastUpdateUserId: user ? user.sub : "system",
};
await posMasterActRepository.save(newAct); // 6. Batch update posMasters
} console.time("[AMQ] batch_update_posMasters");
console.timeEnd("[AMQ] clone_oldposMasterAct"); for (const update of posMasterUpdates) {
await repoPosmaster.update(update.id, {
current_holderId: update.current_holderId,
next_holderId: null,
lastUpdateUserId,
lastUpdateFullName,
lastUpdatedAt,
});
}
console.timeEnd("[AMQ] batch_update_posMasters");
// 7. Batch create history
console.time("[AMQ] batch_create_history");
for (const id of historyCreateIds) {
await CreatePosMasterHistoryOfficer(id, null, undefined, undefined, manager);
}
console.timeEnd("[AMQ] batch_create_history");
// Clone oldposMasterAct
console.time("[AMQ] clone_oldposMasterAct");
for (const act of oldposMasterAct) {
const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? "";
const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? "";
const newParentId = posMasterIdMap.get(parentDNA);
const newChildId = posMasterIdMap.get(childDNA);
if (!newParentId || !newChildId) continue;
const { id, posMaster, posMasterChild, ...fields } = act;
const newAct = {
...fields,
posMasterId: newParentId,
posMasterChildId: newChildId,
createdAt: new Date(),
createdFullName: user ? user.name : "system",
createdUserId: user ? user.sub : "system",
lastUpdatedAt: new Date(),
lastUpdateFullName: user ? user.name : "system",
lastUpdateUserId: user ? user.sub : "system",
};
await posMasterActRepository.save(newAct);
}
console.timeEnd("[AMQ] clone_oldposMasterAct");
if (orgRevisionPublish != null && orgRevisionDraft != null) {
console.time("[AMQ] clone_org_structure"); console.time("[AMQ] clone_org_structure");
//new main revision //new main revision
const before = null; const before = null;
@ -1001,20 +1062,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
console.log(`[AMQ] orgemployeePosMaster count: ${orgemployeePosMaster.length}`); console.log(`[AMQ] orgemployeePosMaster count: ${orgemployeePosMaster.length}`);
let _orgemployeePosMaster: EmployeePosMaster[]; let _orgemployeePosMaster: EmployeePosMaster[];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// _orgemployeePosMaster = orgemployeePosMaster.map((x) => ({
// ...x,
// ancestorDNA:
// x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
// ? x.id
// : x.ancestorDNA,
// }));
// await repoEmployeePosmaster.save(_orgemployeePosMaster);
const validProfileIds = new Set( const validProfileIds = new Set(
(await repoProfileEmployee.find({ select: ["id"] })).map((p) => p.id), (await repoProfileEmployee.find({ select: ["id"] })).map((p) => p.id),
); );
@ -1042,7 +1089,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.execute(); .execute();
console.timeEnd("[AMQ] insert_employeePosMaster"); console.timeEnd("[AMQ] insert_employeePosMaster");
// }
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
console.time("[AMQ] query_employeeTempPosMaster"); console.time("[AMQ] query_employeeTempPosMaster");
const orgemployeeTempPosMaster = await repoEmployeeTempPosmaster.find({ const orgemployeeTempPosMaster = await repoEmployeeTempPosmaster.find({
@ -1053,12 +1099,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
console.log(`[AMQ] orgemployeeTempPosMaster count: ${orgemployeeTempPosMaster.length}`); console.log(`[AMQ] orgemployeeTempPosMaster count: ${orgemployeeTempPosMaster.length}`);
let _orgemployeeTempPosMaster: EmployeeTempPosMaster[]; let _orgemployeeTempPosMaster: EmployeeTempPosMaster[];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
_orgemployeeTempPosMaster = orgemployeeTempPosMaster.map((x) => ({ _orgemployeeTempPosMaster = orgemployeeTempPosMaster.map((x) => ({
...x, ...x,
ancestorDNA: ancestorDNA:
@ -1066,7 +1106,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
? x.id ? x.id
: x.ancestorDNA, : x.ancestorDNA,
})); }));
// await repoEmployeeTempPosmaster.save(_orgemployeeTempPosMaster);
await repoEmployeeTempPosmaster await repoEmployeeTempPosmaster
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
@ -1077,7 +1116,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
overwrite: ["ancestorDNA"], overwrite: ["ancestorDNA"],
}) })
.execute(); .execute();
// }
//create org //create org
console.time("[AMQ] forEach_orgRoot"); console.time("[AMQ] forEach_orgRoot");
@ -1086,13 +1124,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const dataId = x.id; const dataId = x.id;
const matchedOrgRoot = findMatchedNodeByAncestorDNA(orgRootCurrent, x); const matchedOrgRoot = findMatchedNodeByAncestorDNA(orgRootCurrent, x);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
//create employeePosmaster
const filteredEmployeePosMaster = _orgemployeePosMaster.filter( const filteredEmployeePosMaster = _orgemployeePosMaster.filter(
(x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null, (x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null,
); );
@ -1102,24 +1133,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id; delete item.id;
const employeePosMaster = Object.assign(new EmployeePosMaster(), item); const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = []; employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id; employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.createdUserId = ""; employeePosMaster.createdUserId = "";
@ -1130,7 +1143,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date(); employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster); await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1139,12 +1151,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterId = employeePosMaster.id; employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1156,7 +1162,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
//create employeeTempPosmaster
await Promise.all( await Promise.all(
_orgemployeeTempPosMaster _orgemployeeTempPosMaster
.filter((x: EmployeeTempPosMaster) => x.orgRootId == dataId && x.orgChild1Id == null) .filter((x: EmployeeTempPosMaster) => x.orgRootId == dataId && x.orgChild1Id == null)
@ -1164,24 +1170,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id; delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
employeeTempPosMaster.positions = []; employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.createdUserId = ""; employeeTempPosMaster.createdUserId = "";
@ -1192,7 +1180,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date(); employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster); await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1201,12 +1188,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterTempId = employeeTempPosMaster.id; employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1218,46 +1199,17 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// }
//create org
for (const x of orgChild1.filter((item: OrgChild1) => item.orgRootId == dataId)) { for (const x of orgChild1.filter((item: OrgChild1) => item.orgRootId == dataId)) {
const data1Id = x.id; const data1Id = x.id;
const matchedOrgChild1 = findMatchedNodeByAncestorDNA(orgChild1Current, x); const matchedOrgChild1 = findMatchedNodeByAncestorDNA(orgChild1Current, x);
// ("[in case Child1] ancestorDNA", `${x.orgChild1Id == matchedOrgChild1?.id}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
//create employeePosmaster
await Promise.all( await Promise.all(
_orgemployeePosMaster _orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null) .filter((x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null)
.map(async (item: any) => { .map(async (item: any) => {
delete item.id; delete item.id;
// console.log("[in case Child1] orgChild1Id == data1Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item); const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = []; employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id; employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1269,7 +1221,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date(); employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster); await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1278,12 +1229,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterId = employeePosMaster.id; employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1295,7 +1240,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// create employeeTempPosmaster
await Promise.all( await Promise.all(
_orgemployeeTempPosMaster _orgemployeeTempPosMaster
.filter( .filter(
@ -1305,24 +1250,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id; delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
employeeTempPosMaster.positions = []; employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1334,7 +1261,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date(); employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster); await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1343,12 +1269,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterTempId = employeeTempPosMaster.id; employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1360,46 +1280,17 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// }
//create org
for (const x of orgChild2.filter((item: OrgChild2) => item.orgChild1Id == data1Id)) { for (const x of orgChild2.filter((item: OrgChild2) => item.orgChild1Id == data1Id)) {
const data2Id = x.id; const data2Id = x.id;
const matchedOrgChild2 = findMatchedNodeByAncestorDNA(orgChild2Current, x); const matchedOrgChild2 = findMatchedNodeByAncestorDNA(orgChild2Current, x);
// console.log("[in case Child2] ancestorDNA", `${x.orgChild2Id == matchedOrgChild2?.id}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
//create employeePosmaster
await Promise.all( await Promise.all(
_orgemployeePosMaster _orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null) .filter((x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null)
.map(async (item: any) => { .map(async (item: any) => {
delete item.id; delete item.id;
// console.log("[in case Child2] orgChild2Id == data2Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item); const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = []; employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id; employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1412,7 +1303,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date(); employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster); await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1421,12 +1311,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterId = employeePosMaster.id; employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1438,7 +1322,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// create employeeTempPosmaster
await Promise.all( await Promise.all(
_orgemployeeTempPosMaster _orgemployeeTempPosMaster
.filter( .filter(
@ -1448,24 +1332,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id; delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
employeeTempPosMaster.positions = []; employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId; employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id; employeeTempPosMaster.orgChild1Id = data1Id;
@ -1478,7 +1344,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date(); employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster); await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1487,12 +1352,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterTempId = employeeTempPosMaster.id; employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1504,20 +1363,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// }
//create org
for (const x of orgChild3.filter((item: OrgChild3) => item.orgChild2Id == data2Id)) { for (const x of orgChild3.filter((item: OrgChild3) => item.orgChild2Id == data2Id)) {
const data3Id = x.id; const data3Id = x.id;
const matchedOrgChild3 = findMatchedNodeByAncestorDNA(orgChild3Current, x); const matchedOrgChild3 = findMatchedNodeByAncestorDNA(orgChild3Current, x);
// console.log("[in case Child3] ancestorDNA", `${x.orgChild3Id == matchedOrgChild3?.id}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
//create employeePosmaster
await Promise.all( await Promise.all(
_orgemployeePosMaster _orgemployeePosMaster
.filter( .filter(
@ -1525,27 +1374,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
) )
.map(async (item: any) => { .map(async (item: any) => {
delete item.id; delete item.id;
// console.log("[in case Child3] orgChild3Id == data3Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item); const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = []; employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id; employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1559,7 +1389,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date(); employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster); await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1568,12 +1397,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterId = employeePosMaster.id; employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1585,7 +1408,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// create employeeTempPosmaster
await Promise.all( await Promise.all(
_orgemployeeTempPosMaster _orgemployeeTempPosMaster
.filter( .filter(
@ -1595,24 +1418,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id; delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
employeeTempPosMaster.positions = []; employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1626,7 +1431,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date(); employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster); await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1635,12 +1439,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterTempId = employeeTempPosMaster.id; employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1652,46 +1450,17 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// }
//create org
for (const x of orgChild4.filter((item: OrgChild4) => item.orgChild3Id == data3Id)) { for (const x of orgChild4.filter((item: OrgChild4) => item.orgChild3Id == data3Id)) {
const data4Id = x.id; const data4Id = x.id;
const matchedOrgChild4 = findMatchedNodeByAncestorDNA(orgChild4Current, x); const matchedOrgChild4 = findMatchedNodeByAncestorDNA(orgChild4Current, x);
// console.log("[in case Child4] ancestorDNA", `${x.orgChild4Id == matchedOrgChild4?.id}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
//create employeePosmaster
await Promise.all( await Promise.all(
_orgemployeePosMaster _orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id) .filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id)
.map(async (item: any) => { .map(async (item: any) => {
delete item.id; delete item.id;
// console.log("[in case Child4] orgChild4Id == data4Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item); const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = []; employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id; employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1706,7 +1475,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date(); employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster); await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1715,12 +1483,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterId = employeePosMaster.id; employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1732,7 +1494,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
//create employeeTempPosmaster
await Promise.all( await Promise.all(
_orgemployeeTempPosMaster _orgemployeeTempPosMaster
.filter((x: EmployeeTempPosMaster) => x.orgChild4Id == data4Id) .filter((x: EmployeeTempPosMaster) => x.orgChild4Id == data4Id)
@ -1743,24 +1505,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
item, item,
); );
employeeTempPosMaster.positions = []; employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1775,7 +1519,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date(); employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster); await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all( await Promise.all(
item.positions.map(async (pos: any) => { item.positions.map(async (pos: any) => {
delete pos.id; delete pos.id;
@ -1784,12 +1527,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos, pos,
); );
employeePosition.posMasterTempId = employeeTempPosMaster.id; employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = ""; employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator"; employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date(); employeePosition.createdAt = new Date();
@ -1801,13 +1538,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
); );
}), }),
); );
// }
} }
} }
} }
} }
} }
// }
const employeePosMaster = await repoEmployeePosmaster.find({ const employeePosMaster = await repoEmployeePosmaster.find({
where: { orgRevisionId: orgRevisionDraft.id }, where: { orgRevisionId: orgRevisionDraft.id },
@ -1827,8 +1562,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
await repoProfileEmployee.save(profile); await repoProfileEmployee.save(profile);
} }
} }
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId; item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName; item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt; item.lastUpdatedAt = lastUpdatedAt;
@ -1852,14 +1585,32 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
await repoProfileEmployee.save(profile); await repoProfileEmployee.save(profile);
} }
} }
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId; item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName; item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt; item.lastUpdatedAt = lastUpdatedAt;
await repoEmployeeTempPosmaster.save(item); await repoEmployeeTempPosmaster.save(item);
} }
console.timeEnd("[AMQ] clone_org_structure");
console.time("[AMQ] save_revision_status");
orgRevisionPublish.orgRevisionIsDraft = false;
orgRevisionPublish.orgRevisionIsCurrent = false;
await repoOrgRevision.save(orgRevisionPublish);
orgRevisionDraft.orgRevisionIsCurrent = true;
orgRevisionDraft.orgRevisionIsDraft = false;
await repoOrgRevision.save(orgRevisionDraft);
console.timeEnd("[AMQ] save_revision_status");
});
if (shouldSkipPublishInTransaction) {
console.log(
`[AMQ] Skip publish in transaction: revision ${id} state changed before write phase`,
);
console.timeEnd("[AMQ] handler_org_total");
return true;
} }
console.log("[AMQ] Excecute Organization Success"); console.log("[AMQ] Excecute Organization Success");
if (user) { if (user) {
sendWebSocket( sendWebSocket(
@ -1871,18 +1622,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
{ userId: user?.sub }, { userId: user?.sub },
).catch(console.error); ).catch(console.error);
} }
console.timeEnd("[AMQ] clone_org_structure");
// อัปเดตสถานะ orgRevision หลังจากทำงานเสร็จทั้งหมด
console.time("[AMQ] save_revision_status");
orgRevisionPublish.orgRevisionIsDraft = false;
orgRevisionPublish.orgRevisionIsCurrent = false;
await repoOrgRevision.save(orgRevisionPublish);
orgRevisionDraft.orgRevisionIsCurrent = true;
orgRevisionDraft.orgRevisionIsDraft = false;
await repoOrgRevision.save(orgRevisionDraft);
console.timeEnd("[AMQ] save_revision_status");
console.log(`[AMQ] handler_org SUCCESS - Total time: ${Date.now() - startTime}ms`); console.log(`[AMQ] handler_org SUCCESS - Total time: ${Date.now() - startTime}ms`);
console.timeEnd("[AMQ] handler_org_total"); console.timeEnd("[AMQ] handler_org_total");