refactor transaction
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m2s

This commit is contained in:
Warunee Tamkoo 2026-05-05 12:32:21 +07:00
parent 869bb093a3
commit 3335c4f44c
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 { AppDataSource } from "../database/data-source";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
@ -17,105 +17,118 @@ export async function CreatePosMasterHistoryOfficer(
request: RequestWithUser | null,
type?: string | null,
positionData?: { positionId?: string } | null,
manager?: EntityManager,
): Promise<boolean> {
try {
await AppDataSource.transaction(async (manager) => {
const repoPosmaster = manager.getRepository(PosMaster);
const repoHistory = manager.getRepository(PosMasterHistory);
const repoOrgRevision = manager.getRepository(OrgRevision);
const repoPosition = manager.getRepository(Position);
const execute = async (transactionManager: EntityManager) => {
const repoPosmaster = transactionManager.getRepository(PosMaster);
const repoHistory = transactionManager.getRepository(PosMasterHistory);
const repoOrgRevision = transactionManager.getRepository(OrgRevision);
const repoPosition = transactionManager.getRepository(Position);
const pm = await repoPosmaster.findOne({
where: { id: posMasterId },
relations: [
"positions",
"positions.posLevel",
"positions.posType",
"positions.posExecutive",
"orgRoot",
"orgChild1",
"orgChild2",
"orgChild3",
"orgChild4",
"current_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);
const pm = await repoPosmaster.findOne({
where: { id: posMasterId },
relations: [
"positions",
"positions.posLevel",
"positions.posType",
"positions.posExecutive",
"orgRoot",
"orgChild1",
"orgChild2",
"orgChild3",
"orgChild4",
"current_holder",
"next_holder",
],
});
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;
} catch (err) {
if (manager) {
throw err;
}
console.error("CreatePosMasterHistoryOfficer transaction error:", err);
return false;
}

View file

@ -818,77 +818,138 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
);
// ===== 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)
console.time("[AMQ] batch_save_posMasterAssign");
if (posMasterAssignsToSave.length > 0) {
const chunks = chunkArray(posMasterAssignsToSave, 500);
for (const chunk of chunks) {
await posMasterAssignRepository.save(chunk);
const targetOrgRevision = await repoOrgRevision
.createQueryBuilder("orgRevision")
.setLock("pessimistic_write")
.where("orgRevision.id = :id", { id })
.getOne();
if (!targetOrgRevision) {
shouldSkipPublishInTransaction = true;
return;
}
}
console.timeEnd("[AMQ] batch_save_posMasterAssign");
// 5. Batch save profiles (chunk 200)
console.time("[AMQ] batch_save_profiles");
if (profilesToSave.length > 0) {
const chunks = chunkArray(profilesToSave, 200);
for (const chunk of chunks) {
await repoProfile.save(chunk);
if (targetOrgRevision.orgRevisionIsCurrent && !targetOrgRevision.orgRevisionIsDraft) {
shouldSkipPublishInTransaction = true;
return;
}
}
console.timeEnd("[AMQ] batch_save_profiles");
// 6. Batch update posMasters
console.time("[AMQ] batch_update_posMasters");
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");
if (!targetOrgRevision.orgRevisionIsDraft || targetOrgRevision.orgRevisionIsCurrent) {
shouldSkipPublishInTransaction = true;
return;
}
// 7. Batch create history
console.time("[AMQ] batch_create_history");
for (const id of historyCreateIds) {
await CreatePosMasterHistoryOfficer(id, null);
}
console.timeEnd("[AMQ] batch_create_history");
const orgRevisionPublish = await repoOrgRevision
.createQueryBuilder("orgRevision")
.setLock("pessimistic_write")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
// 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() ?? "";
if (!orgRevisionPublish) {
throw new Error("[AMQ] Cannot publish in transaction: no current org revision found");
}
const newParentId = posMasterIdMap.get(parentDNA);
const newChildId = posMasterIdMap.get(childDNA);
const orgRevisionDraft = await repoOrgRevision
.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 = {
...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",
};
// 5. Batch save profiles (chunk 200)
console.time("[AMQ] batch_save_profiles");
if (profilesToSave.length > 0) {
const chunks = chunkArray(profilesToSave, 200);
for (const chunk of chunks) {
await repoProfile.save(chunk);
}
}
console.timeEnd("[AMQ] batch_save_profiles");
await posMasterActRepository.save(newAct);
}
console.timeEnd("[AMQ] clone_oldposMasterAct");
// 6. Batch update posMasters
console.time("[AMQ] batch_update_posMasters");
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");
//new main revision
const before = null;
@ -1001,20 +1062,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
console.log(`[AMQ] orgemployeePosMaster count: ${orgemployeePosMaster.length}`);
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(
(await repoProfileEmployee.find({ select: ["id"] })).map((p) => p.id),
);
@ -1042,7 +1089,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.execute();
console.timeEnd("[AMQ] insert_employeePosMaster");
// }
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
console.time("[AMQ] query_employeeTempPosMaster");
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}`);
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) => ({
...x,
ancestorDNA:
@ -1066,7 +1106,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
? x.id
: x.ancestorDNA,
}));
// await repoEmployeeTempPosmaster.save(_orgemployeeTempPosMaster);
await repoEmployeeTempPosmaster
.createQueryBuilder()
.insert()
@ -1077,7 +1116,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
overwrite: ["ancestorDNA"],
})
.execute();
// }
//create org
console.time("[AMQ] forEach_orgRoot");
@ -1086,13 +1124,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const dataId = x.id;
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(
(x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null,
);
@ -1102,24 +1133,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id;
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.createdUserId = "";
@ -1130,7 +1143,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1139,12 +1151,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
@ -1156,7 +1162,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
);
}),
);
//create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.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;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.createdUserId = "";
@ -1192,7 +1180,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1201,12 +1188,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
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)) {
const data1Id = x.id;
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(
_orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child1] orgChild1Id == data1Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1269,7 +1221,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1278,12 +1229,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
@ -1295,7 +1240,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
);
}),
);
// create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter(
@ -1305,24 +1250,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1334,7 +1261,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1343,12 +1269,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
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)) {
const data2Id = x.id;
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(
_orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child2] orgChild2Id == data2Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1412,7 +1303,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1421,12 +1311,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
@ -1438,7 +1322,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
);
}),
);
// create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter(
@ -1448,24 +1332,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
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.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
@ -1478,7 +1344,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1487,12 +1352,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
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)) {
const data3Id = x.id;
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(
_orgemployeePosMaster
.filter(
@ -1525,27 +1374,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child3] orgChild3Id == data3Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1559,7 +1389,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1568,12 +1397,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
@ -1585,7 +1408,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
);
}),
);
// create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter(
@ -1595,24 +1418,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1626,7 +1431,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1635,12 +1439,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
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)) {
const data4Id = x.id;
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(
_orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child4] orgChild4Id == data4Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1706,7 +1475,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1715,12 +1483,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
@ -1732,7 +1494,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
);
}),
);
//create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter((x: EmployeeTempPosMaster) => x.orgChild4Id == data4Id)
@ -1743,24 +1505,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
item,
);
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.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
@ -1775,7 +1519,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
await Promise.all(
item.positions.map(async (pos: any) => {
delete pos.id;
@ -1784,12 +1527,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
@ -1801,13 +1538,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
);
}),
);
// }
}
}
}
}
}
// }
const employeePosMaster = await repoEmployeePosmaster.find({
where: { orgRevisionId: orgRevisionDraft.id },
@ -1827,8 +1562,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
await repoProfileEmployee.save(profile);
}
}
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt;
@ -1852,14 +1585,32 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
await repoProfileEmployee.save(profile);
}
}
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt;
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");
if (user) {
sendWebSocket(
@ -1871,18 +1622,6 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
{ userId: user?.sub },
).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.timeEnd("[AMQ] handler_org_total");