fix error

This commit is contained in:
Warunee Tamkoo 2026-04-30 20:15:37 +07:00
parent 519fd97968
commit 342fd7e2f9

View file

@ -24,7 +24,12 @@ import { In, Not } from "typeorm";
import { PosMasterAct } from "../entities/PosMasterAct"; import { PosMasterAct } from "../entities/PosMasterAct";
import { PermissionOrg } from "../entities/PermissionOrg"; import { PermissionOrg } from "../entities/PermissionOrg";
import { sendWebSocket } from "./webSocket"; import { sendWebSocket } from "./webSocket";
import { CreatePosMasterHistoryOfficer, BatchUpdatePosMasters, BatchCreatePosMasterHistoryOfficer, BatchHistoryOperation } from "./PositionService"; import {
CreatePosMasterHistoryOfficer,
BatchUpdatePosMasters,
BatchCreatePosMasterHistoryOfficer,
BatchHistoryOperation,
} from "./PositionService";
import { PayloadSendNoti } from "../interfaces/utils"; import { PayloadSendNoti } from "../interfaces/utils";
import { PermissionProfile } from "../entities/PermissionProfile"; import { PermissionProfile } from "../entities/PermissionProfile";
@ -405,7 +410,7 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean>
try { try {
let profilesNotiRequest: Promise<any> | undefined; let profilesNotiRequest: Promise<any> | undefined;
if (!(["C-PM-10"].includes(command.commandType.code))) { if (!["C-PM-10"].includes(command.commandType.code)) {
profilesNotiRequest = new CallAPI() profilesNotiRequest = new CallAPI()
.PostData( .PostData(
{ headers: { authorization: token } }, { headers: { authorization: token } },
@ -482,8 +487,7 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean>
/*เฉพาะคำสั่ง C-PM-10 ให้ตัด profilesNotiRequest ที่ส่ง noti ครั้งแรกออก*/ /*เฉพาะคำสั่ง C-PM-10 ให้ตัด profilesNotiRequest ที่ส่ง noti ครั้งแรกออก*/
if (["C-PM-10"].includes(command.commandType.code)) { if (["C-PM-10"].includes(command.commandType.code)) {
await Promise.all([profilesSendRequest]); await Promise.all([profilesSendRequest]);
} } else {
else {
await Promise.all([profilesNotiRequest!, profilesSendRequest]); await Promise.all([profilesNotiRequest!, profilesSendRequest]);
} }
@ -497,7 +501,7 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean>
async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> { async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
//----> condition before process consume //----> condition before process consume
console.time('[AMQ] handler_org_total'); console.time("[AMQ] handler_org_total");
const startTime = Date.now(); const startTime = Date.now();
console.log(`[AMQ] handler_org START at ${new Date(startTime).toISOString()}`); console.log(`[AMQ] handler_org START at ${new Date(startTime).toISOString()}`);
@ -531,7 +535,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
).catch(console.error); ).catch(console.error);
} }
console.time('[AMQ] query_revisions'); console.time("[AMQ] query_revisions");
const orgRevisionPublish = await repoOrgRevision const orgRevisionPublish = await repoOrgRevision
.createQueryBuilder("orgRevision") .createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false") .where("orgRevision.orgRevisionIsDraft = false")
@ -543,13 +547,17 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.where("orgRevision.orgRevisionIsDraft = true") .where("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false") .andWhere("orgRevision.orgRevisionIsCurrent = false")
.getOne(); .getOne();
console.timeEnd('[AMQ] query_revisions'); console.timeEnd("[AMQ] query_revisions");
console.log(`[AMQ] orgRevisionPublish found: ${orgRevisionPublish ? orgRevisionPublish.id : 'null'}`); console.log(
console.log(`[AMQ] orgRevisionDraft found: ${orgRevisionDraft ? orgRevisionDraft.id : 'null'}`); `[AMQ] orgRevisionPublish found: ${orgRevisionPublish ? orgRevisionPublish.id : "null"}`,
);
console.log(`[AMQ] orgRevisionDraft found: ${orgRevisionDraft ? orgRevisionDraft.id : "null"}`);
// Validate: ต้องมี orgRevisionPublish เสมอสำหรับการเผยแพร่ // Validate: ต้องมี orgRevisionPublish เสมอสำหรับการเผยแพร่
if (!orgRevisionPublish) { if (!orgRevisionPublish) {
console.error('[AMQ] Cannot publish: No current org revision found (isDraft=false, isCurrent=true)'); console.error(
"[AMQ] Cannot publish: No current org revision found (isDraft=false, isCurrent=true)",
);
if (user) { if (user) {
sendWebSocket( sendWebSocket(
"send-publish-org", "send-publish-org",
@ -565,7 +573,9 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// Validate: ต้องมี orgRevisionDraft ที่จะเผยแพร่ // Validate: ต้องมี orgRevisionDraft ที่จะเผยแพร่
if (!orgRevisionDraft) { if (!orgRevisionDraft) {
console.error('[AMQ] Cannot publish: No draft org revision found (isDraft=true, isCurrent=false)'); console.error(
"[AMQ] Cannot publish: No draft org revision found (isDraft=true, isCurrent=false)",
);
if (user) { if (user) {
sendWebSocket( sendWebSocket(
"send-publish-org", "send-publish-org",
@ -583,7 +593,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// เพื่อป้องกันกรณี timeout/retry ทำให้สถานะเพี้ยน (ทุก row เป็น false,false) // เพื่อป้องกันกรณี timeout/retry ทำให้สถานะเพี้ยน (ทุก row เป็น false,false)
try { try {
console.time('[AMQ] query_posMaster'); console.time("[AMQ] query_posMaster");
const POS_MASTER_PAGE_SIZE = 2000; const POS_MASTER_PAGE_SIZE = 2000;
let totalPosMastersProcessed = 0; let totalPosMastersProcessed = 0;
let hasMoreRecords = true; let hasMoreRecords = true;
@ -604,7 +614,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
"positions.posType", "positions.posType",
"positions.posExecutive", "positions.posExecutive",
], ],
order: { id: 'ASC' }, order: { id: "ASC" },
skip: skip, skip: skip,
take: POS_MASTER_PAGE_SIZE, take: POS_MASTER_PAGE_SIZE,
}); });
@ -616,15 +626,15 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
console.log(`[AMQ] Loaded posMaster page: ${totalPosMastersProcessed} records`); console.log(`[AMQ] Loaded posMaster page: ${totalPosMastersProcessed} records`);
} }
console.timeEnd('[AMQ] query_posMaster'); console.timeEnd("[AMQ] query_posMaster");
console.log(`[AMQ] posMaster count: ${posMaster.length}`); console.log(`[AMQ] posMaster count: ${posMaster.length}`);
console.time('[AMQ] query_old_data'); console.time("[AMQ] query_old_data");
const oldPosMasters = await repoPosmaster.find({ const oldPosMasters = await repoPosmaster.find({
where: { where: {
orgRevisionId: orgRevisionPublish.id, orgRevisionId: orgRevisionPublish.id,
}, },
select: ['id', 'current_holderId', 'ancestorDNA'] select: ["id", "current_holderId", "ancestorDNA"],
}); });
// Task #2160 ดึง posMasterAssign ของ revision เดิม // Task #2160 ดึง posMasterAssign ของ revision เดิม
@ -636,11 +646,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
}, },
}, },
}); });
console.timeEnd('[AMQ] query_old_data'); console.timeEnd("[AMQ] query_old_data");
console.log(`[AMQ] oldPosMasters count: ${oldPosMasters.length}`); console.log(`[AMQ] oldPosMasters count: ${oldPosMasters.length}`);
console.log(`[AMQ] oldposMasterAssigns count: ${oldposMasterAssigns.length}`); console.log(`[AMQ] oldposMasterAssigns count: ${oldposMasterAssigns.length}`);
console.time('[AMQ] build_assignMap'); console.time("[AMQ] build_assignMap");
// สร้าง assignMap เอาไว้เก็บ posMasterAssign.ancestorDNA ของ revision เดิม // สร้าง assignMap เอาไว้เก็บ posMasterAssign.ancestorDNA ของ revision เดิม
const assignMap = new Map<string, PosMasterAssignDTO[]>(); const assignMap = new Map<string, PosMasterAssignDTO[]>();
for (const posmasterAssign of oldposMasterAssigns) { for (const posmasterAssign of oldposMasterAssigns) {
@ -651,12 +661,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
assignMap.get(dna)!.push({ assignMap.get(dna)!.push({
id: posmasterAssign.id, id: posmasterAssign.id,
posMasterId: posmasterAssign.posMasterId, posMasterId: posmasterAssign.posMasterId,
assignId: posmasterAssign.assignId assignId: posmasterAssign.assignId,
}); });
} }
console.timeEnd('[AMQ] build_assignMap'); console.timeEnd("[AMQ] build_assignMap");
console.time('[AMQ] query_oldposMasterAct'); console.time("[AMQ] query_oldposMasterAct");
// ดึง posMasterAct ของ revision เดิม xxx // ดึง posMasterAct ของ revision เดิม xxx
const oldposMasterAct = await posMasterActRepository.find({ const oldposMasterAct = await posMasterActRepository.find({
relations: ["posMaster", "posMasterChild"], relations: ["posMaster", "posMasterChild"],
@ -666,16 +676,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
}, },
}, },
}); });
console.timeEnd('[AMQ] query_oldposMasterAct'); console.timeEnd("[AMQ] query_oldposMasterAct");
console.log(`[AMQ] oldposMasterAct count: ${oldposMasterAct.length}`); console.log(`[AMQ] oldposMasterAct count: ${oldposMasterAct.length}`);
type ActKey = string; // `${parentDNA}|${childDNA}` type ActKey = string; // `${parentDNA}|${childDNA}`
console.time('[AMQ] build_maps'); console.time("[AMQ] build_maps");
const posMasterActMap = new Map<ActKey, PosMasterAct[]>(); const posMasterActMap = new Map<ActKey, PosMasterAct[]>();
for (const act of oldposMasterAct) { for (const act of oldposMasterAct) {
const parentDNA = act.posMaster?.ancestorDNA?.trim() ?? ''; const parentDNA = act.posMaster?.ancestorDNA?.trim() ?? "";
const childDNA = act.posMasterChild?.ancestorDNA?.trim() ?? ''; const childDNA = act.posMasterChild?.ancestorDNA?.trim() ?? "";
const key = `${parentDNA}|${childDNA}`; const key = `${parentDNA}|${childDNA}`;
if (!posMasterActMap.has(key)) { if (!posMasterActMap.has(key)) {
@ -686,7 +696,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const posMasterIdMap = new Map<string, string>(); const posMasterIdMap = new Map<string, string>();
for (const pm of posMaster) { for (const pm of posMaster) {
posMasterIdMap.set(pm.ancestorDNA?.trim() ?? '', pm.id); posMasterIdMap.set(pm.ancestorDNA?.trim() ?? "", pm.id);
} }
const oldPosMasterMap = new Map<string, PosMaster>(); const oldPosMasterMap = new Map<string, PosMaster>();
@ -696,25 +706,25 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
oldPosMasterMap.set(dna, oldPm); oldPosMasterMap.set(dna, oldPm);
} }
} }
console.timeEnd('[AMQ] build_maps'); console.timeEnd("[AMQ] build_maps");
const _null: any = null; const _null: any = null;
// ===== BATCH PROCESSING: เตรียมข้อมูลก่อน loop ===== // ===== BATCH PROCESSING: เตรียมข้อมูลก่อน loop =====
console.time('[AMQ] prepare_batch_data'); console.time("[AMQ] prepare_batch_data");
// 1. รวบรวม profileIds ทั้งหมดที่ต้องอัพเดท // 1. รวบรวม profileIds ทั้งหมดที่ต้องอัพเดท
const profileIds = posMaster const profileIds = posMaster
.filter(item => item.next_holderId != null) .filter((item) => item.next_holderId != null)
.map(item => item.next_holderId!) .map((item) => item.next_holderId!)
.filter(id => id != null && id !== ""); .filter((id) => id != null && id !== "");
// 2. Batch load profiles ทั้งหมดในครั้งเดียว (แก้ปัญหา N+1 Query) // 2. Batch load profiles ทั้งหมดในครั้งเดียว (แก้ปัญหา N+1 Query)
const profilesMap = new Map<string, Profile>(); const profilesMap = new Map<string, Profile>();
if (profileIds.length > 0) { if (profileIds.length > 0) {
const profiles = await repoProfile.findBy({ const profiles = await repoProfile.findBy({
id: In(profileIds) id: In(profileIds),
}); });
profiles.forEach(p => profilesMap.set(p.id, p)); profiles.forEach((p) => profilesMap.set(p.id, p));
} }
console.log(`[AMQ] profiles to update: ${profilesMap.size}`); console.log(`[AMQ] profiles to update: ${profilesMap.size}`);
@ -742,7 +752,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
lastUpdatedAt: lastUpdatedAt, lastUpdatedAt: lastUpdatedAt,
lastUpdateFullName: lastUpdateFullName, lastUpdateFullName: lastUpdateFullName,
lastUpdateUserId: lastUpdateUserId, lastUpdateUserId: lastUpdateUserId,
}) }),
); );
posMasterAssignsToSave.push(...newAssigns); posMasterAssignsToSave.push(...newAssigns);
} }
@ -793,33 +803,35 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
historyCreateIds.push(item.id); historyCreateIds.push(item.id);
} }
} }
console.timeEnd('[AMQ] prepare_batch_data'); console.timeEnd("[AMQ] prepare_batch_data");
console.log(`[AMQ] Prepared - posMasterAssignsToSave: ${posMasterAssignsToSave.length}, profilesToSave: ${profilesToSave.length}, posMasterUpdates: ${posMasterUpdates.length}, historyCreateIds: ${historyCreateIds.length}`); console.log(
`[AMQ] Prepared - posMasterAssignsToSave: ${posMasterAssignsToSave.length}, profilesToSave: ${profilesToSave.length}, posMasterUpdates: ${posMasterUpdates.length}, historyCreateIds: ${historyCreateIds.length}`,
);
// ===== BATCH EXECUTION: save ทีละ batch ===== // ===== BATCH EXECUTION: save ทีละ batch =====
// 4. Batch save posMasterAssign (chunk 500) // 4. Batch save posMasterAssign (chunk 500)
console.time('[AMQ] batch_save_posMasterAssign'); console.time("[AMQ] batch_save_posMasterAssign");
if (posMasterAssignsToSave.length > 0) { if (posMasterAssignsToSave.length > 0) {
const chunks = chunkArray(posMasterAssignsToSave, 500); const chunks = chunkArray(posMasterAssignsToSave, 500);
for (const chunk of chunks) { for (const chunk of chunks) {
await posMasterAssignRepository.save(chunk); await posMasterAssignRepository.save(chunk);
} }
} }
console.timeEnd('[AMQ] batch_save_posMasterAssign'); console.timeEnd("[AMQ] batch_save_posMasterAssign");
// 5. Batch save profiles (chunk 200) // 5. Batch save profiles (chunk 200)
console.time('[AMQ] batch_save_profiles'); console.time("[AMQ] batch_save_profiles");
if (profilesToSave.length > 0) { if (profilesToSave.length > 0) {
const chunks = chunkArray(profilesToSave, 200); const chunks = chunkArray(profilesToSave, 200);
for (const chunk of chunks) { for (const chunk of chunks) {
await repoProfile.save(chunk); await repoProfile.save(chunk);
} }
} }
console.timeEnd('[AMQ] batch_save_profiles'); console.timeEnd("[AMQ] batch_save_profiles");
// 6. Batch update posMasters // 6. Batch update posMasters
console.time('[AMQ] batch_update_posMasters'); console.time("[AMQ] batch_update_posMasters");
const posMasterUpdatesForBatch = posMasterUpdates.map((u: any) => ({ const posMasterUpdatesForBatch = posMasterUpdates.map((u: any) => ({
id: u.id, id: u.id,
@ -829,19 +841,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
lastUpdatedAt, lastUpdatedAt,
})); }));
await BatchUpdatePosMasters( await BatchUpdatePosMasters(AppDataSource.manager, posMasterUpdatesForBatch);
AppDataSource.manager,
posMasterUpdatesForBatch
);
console.timeEnd('[AMQ] batch_update_posMasters'); console.timeEnd("[AMQ] batch_update_posMasters");
// 7. Batch create history // 7. Batch create history
console.time('[AMQ] batch_create_history'); console.time("[AMQ] batch_create_history");
const historyOperations: BatchHistoryOperation[] = []; const historyOperations: BatchHistoryOperation[] = [];
for (const id of historyCreateIds) { for (const id of historyCreateIds) {
const pm = posMaster.find(p => p.id === id); const pm = posMaster.find((p) => p.id === id);
if (pm) { if (pm) {
historyOperations.push({ historyOperations.push({
posMasterId: id, posMasterId: id,
@ -853,18 +862,15 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
} }
} }
await BatchCreatePosMasterHistoryOfficer( await BatchCreatePosMasterHistoryOfficer(AppDataSource.manager, historyOperations);
AppDataSource.manager,
historyOperations
);
console.timeEnd('[AMQ] batch_create_history'); console.timeEnd("[AMQ] batch_create_history");
// Clone oldposMasterAct // Clone oldposMasterAct
console.time('[AMQ] clone_oldposMasterAct'); console.time("[AMQ] clone_oldposMasterAct");
for (const act of oldposMasterAct) { for (const act of oldposMasterAct) {
const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? ''; const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? "";
const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? ''; const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? "";
const newParentId = posMasterIdMap.get(parentDNA); const newParentId = posMasterIdMap.get(parentDNA);
const newChildId = posMasterIdMap.get(childDNA); const newChildId = posMasterIdMap.get(childDNA);
@ -887,16 +893,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
await posMasterActRepository.save(newAct); await posMasterActRepository.save(newAct);
} }
console.timeEnd('[AMQ] clone_oldposMasterAct'); console.timeEnd("[AMQ] clone_oldposMasterAct");
if (orgRevisionPublish != null && orgRevisionDraft != null) { 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;
//ทุก orgRoot และ orgChild ข้างล่างนี้จะเป็นตัวเก่าที่ไม่ได้เป็น current revision //ทุก orgRoot และ orgChild ข้างล่างนี้จะเป็นตัวเก่าที่ไม่ได้เป็น current revision
//cone tree //cone tree
console.time('[AMQ] query_old_org_structure'); console.time("[AMQ] query_old_org_structure");
//หา dna tree //หา dna tree
const orgRoot = await orgRootRepository.find({ const orgRoot = await orgRootRepository.find({
where: { orgRevisionId: orgRevisionPublish.id }, where: { orgRevisionId: orgRevisionPublish.id },
@ -917,27 +923,46 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const orgChild4 = await child4Repository.find({ const orgChild4 = await child4Repository.find({
where: { orgRevisionId: orgRevisionPublish.id }, where: { orgRevisionId: orgRevisionPublish.id },
}); });
console.timeEnd('[AMQ] query_old_org_structure'); console.timeEnd("[AMQ] query_old_org_structure");
console.log(`[AMQ] Old structure - orgRoot: ${orgRoot.length}, orgChild1: ${orgChild1.length}, orgChild2: ${orgChild2.length}, orgChild3: ${orgChild3.length}, orgChild4: ${orgChild4.length}`); console.log(
`[AMQ] Old structure - orgRoot: ${orgRoot.length}, orgChild1: ${orgChild1.length}, orgChild2: ${orgChild2.length}, orgChild3: ${orgChild3.length}, orgChild4: ${orgChild4.length}`,
);
// Task #2172 ดึง orgRoot ของ revision ใหม่ // Task #2172 ดึง orgRoot ของ revision ใหม่
const newRoots = await orgRootRepository.find({ const newRoots = await orgRootRepository.find({
where: { orgRevisionId: orgRevisionDraft.id }, where: { orgRevisionId: orgRevisionDraft.id },
}); });
// สร้าง newRootMap เอาไว้เก็บ orgRoot.ancestorDNA ของ revision ใหม่ // สร้าง newRootMap เอาไว้เก็บ orgRoot.ancestorDNA ของ revision ใหม่
const newRootMap = new Map( const newRootMap = new Map(newRoots.map((r) => [r.ancestorDNA, r.id]));
newRoots.map(r => [r.ancestorDNA, r.id])
// Pre-load new revision org structure data to avoid N+1 queries inside nested loops
console.time("[AMQ] query_new_org_structure");
const newOrgChild1List = await child1Repository.find({
where: { orgRevisionId: orgRevisionDraft.id },
});
const newOrgChild2List = await child2Repository.find({
where: { orgRevisionId: orgRevisionDraft.id },
});
const newOrgChild3List = await child3Repository.find({
where: { orgRevisionId: orgRevisionDraft.id },
});
const newOrgChild4List = await child4Repository.find({
where: { orgRevisionId: orgRevisionDraft.id },
});
console.timeEnd("[AMQ] query_new_org_structure");
console.log(
`[AMQ] New structure loaded - Child1: ${newOrgChild1List.length}, Child2: ${newOrgChild2List.length}, Child3: ${newOrgChild3List.length}, Child4: ${newOrgChild4List.length}`,
); );
console.time('[AMQ] clone_permissionProfiles'); console.time("[AMQ] clone_permissionProfiles");
// ดึง permissionProfiles ของ revision เดิม // ดึง permissionProfiles ของ revision เดิม
const oldPermissionProfiles = await permissionProfilesRepository.find({ const oldPermissionProfiles = await permissionProfilesRepository.find({
relations: ["orgRootTree"], relations: ["orgRootTree"],
where: { where: {
orgRootTree: { orgRootTree: {
orgRevisionId: orgRevisionPublish.id, orgRevisionId: orgRevisionPublish.id,
} },
} },
}); });
const inserts: any[] = []; const inserts: any[] = [];
for (const permiss of oldPermissionProfiles) { for (const permiss of oldPermissionProfiles) {
@ -962,15 +987,15 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
if (inserts.length > 0) { if (inserts.length > 0) {
await permissionProfilesRepository.insert(inserts); await permissionProfilesRepository.insert(inserts);
} }
console.timeEnd('[AMQ] clone_permissionProfiles'); console.timeEnd("[AMQ] clone_permissionProfiles");
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
console.time('[AMQ] query_employeePosMaster'); console.time("[AMQ] query_employeePosMaster");
const orgemployeePosMaster = await repoEmployeePosmaster.find({ const orgemployeePosMaster = await repoEmployeePosmaster.find({
where: { orgRevisionId: orgRevisionPublish.id }, where: { orgRevisionId: orgRevisionPublish.id },
relations: ["positions"], relations: ["positions"],
}); });
console.timeEnd('[AMQ] query_employeePosMaster'); console.timeEnd("[AMQ] query_employeePosMaster");
console.log(`[AMQ] orgemployeePosMaster count: ${orgemployeePosMaster.length}`); console.log(`[AMQ] orgemployeePosMaster count: ${orgemployeePosMaster.length}`);
let _orgemployeePosMaster: EmployeePosMaster[]; let _orgemployeePosMaster: EmployeePosMaster[];
@ -1002,7 +1027,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
: x.ancestorDNA, : x.ancestorDNA,
})); }));
console.time('[AMQ] insert_employeePosMaster'); console.time("[AMQ] insert_employeePosMaster");
await repoEmployeePosmaster await repoEmployeePosmaster
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()
@ -1013,16 +1038,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
overwrite: ["ancestorDNA"], overwrite: ["ancestorDNA"],
}) })
.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({
where: { orgRevisionId: orgRevisionPublish.id }, where: { orgRevisionId: orgRevisionPublish.id },
relations: ["positions"], relations: ["positions"],
}); });
console.timeEnd('[AMQ] query_employeeTempPosMaster'); console.timeEnd("[AMQ] query_employeeTempPosMaster");
console.log(`[AMQ] orgemployeeTempPosMaster count: ${orgemployeeTempPosMaster.length}`); console.log(`[AMQ] orgemployeeTempPosMaster count: ${orgemployeeTempPosMaster.length}`);
let _orgemployeeTempPosMaster: EmployeeTempPosMaster[]; let _orgemployeeTempPosMaster: EmployeeTempPosMaster[];
@ -1053,10 +1078,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// } // }
//create org - forEach orgRoot (WARNING: async forEach without await) //create org - forEach orgRoot (WARNING: async forEach without await)
console.time('[AMQ] forEach_orgRoot'); console.time("[AMQ] forEach_orgRoot");
console.log(`[AMQ] Starting forEach orgRoot loop (${orgRoot.length} items)`); console.log(`[AMQ] Starting forEach orgRoot loop (${orgRoot.length} items)`);
let processedOrgRoot = 0; let processedOrgRoot = 0;
orgRoot.forEach(async (x: any) => { await Promise.all(
orgRoot.map(async (x: any) => {
const itemStartTime = Date.now(); const itemStartTime = Date.now();
var dataId = x.id; var dataId = x.id;
@ -1084,12 +1110,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) { // ) {
//create employeePosmaster //create employeePosmaster
const filteredEmployeePosMaster = _orgemployeePosMaster const filteredEmployeePosMaster = _orgemployeePosMaster.filter(
.filter((x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null); (x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null,
);
await Promise.all( await Promise.all(
filteredEmployeePosMaster filteredEmployeePosMaster.map(async (item: any) => {
.map(async (item: any) => {
delete item.id; delete item.id;
const employeePosMaster = Object.assign(new EmployeePosMaster(), item); const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = []; employeePosMaster.positions = [];
@ -1223,7 +1249,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
x.ancestorDNA === "00000000-0000-0000-0000-000000000000" x.ancestorDNA === "00000000-0000-0000-0000-000000000000"
) { ) {
return ( return (
i.ancestorDNA === null || i.ancestorDNA === "00000000-0000-0000-0000-000000000000" i.ancestorDNA === null ||
i.ancestorDNA === "00000000-0000-0000-0000-000000000000"
); );
} }
return i.ancestorDNA === x.ancestorDNA; return i.ancestorDNA === x.ancestorDNA;
@ -1238,7 +1265,9 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
//create employeePosmaster //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"); // console.log("[in case Child1] orgChild1Id == data1Id");
@ -1862,7 +1891,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
}); });
}); });
}); });
}); }),
);
// } // }
const employeePosMaster = await repoEmployeePosmaster.find({ const employeePosMaster = await repoEmployeePosmaster.find({
@ -1927,10 +1957,10 @@ 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'); console.timeEnd("[AMQ] clone_org_structure");
// อัปเดตสถานะ orgRevision หลังจากทำงานเสร็จทั้งหมด // อัปเดตสถานะ orgRevision หลังจากทำงานเสร็จทั้งหมด
console.time('[AMQ] save_revision_status'); console.time("[AMQ] save_revision_status");
orgRevisionPublish.orgRevisionIsDraft = false; orgRevisionPublish.orgRevisionIsDraft = false;
orgRevisionPublish.orgRevisionIsCurrent = false; orgRevisionPublish.orgRevisionIsCurrent = false;
await repoOrgRevision.save(orgRevisionPublish); await repoOrgRevision.save(orgRevisionPublish);
@ -1938,10 +1968,27 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
orgRevisionDraft.orgRevisionIsCurrent = true; orgRevisionDraft.orgRevisionIsCurrent = true;
orgRevisionDraft.orgRevisionIsDraft = false; orgRevisionDraft.orgRevisionIsDraft = false;
await repoOrgRevision.save(orgRevisionDraft); await repoOrgRevision.save(orgRevisionDraft);
console.timeEnd('[AMQ] save_revision_status'); 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");
// Memory cleanup: clear arrays that are in scope
try {
if (typeof posMaster !== "undefined" && posMaster) posMaster.length = 0;
if (typeof oldPosMasters !== "undefined" && oldPosMasters) oldPosMasters.length = 0;
if (typeof oldposMasterAssigns !== "undefined" && oldposMasterAssigns)
oldposMasterAssigns.length = 0;
if (typeof oldposMasterAct !== "undefined" && oldposMasterAct) oldposMasterAct.length = 0;
if (typeof assignMap !== "undefined" && assignMap) assignMap.clear();
if (typeof posMasterActMap !== "undefined" && posMasterActMap) posMasterActMap.clear();
if (typeof posMasterIdMap !== "undefined" && posMasterIdMap) posMasterIdMap.clear();
if (typeof oldPosMasterMap !== "undefined" && oldPosMasterMap) oldPosMasterMap.clear();
if (typeof profilesMap !== "undefined" && profilesMap) profilesMap.clear();
} catch (cleanupError) {
console.error("[AMQ] Error during memory cleanup:", cleanupError);
}
return true; return true;
} catch (error) { } catch (error) {
const totalTime = Date.now() - startTime; const totalTime = Date.now() - startTime;
@ -1956,7 +2003,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
{ userId: user?.sub }, { userId: user?.sub },
).catch(console.error); ).catch(console.error);
} }
console.timeEnd('[AMQ] handler_org_total'); console.timeEnd("[AMQ] handler_org_total");
return false; return false;
} }
} }
@ -2631,7 +2678,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
}); });
await posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); await posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) });
await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน
await posMasterActRepository.delete({ //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน await posMasterActRepository.delete({
//ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน
posMasterChildId: In(_posMasters.map((x) => x.id)), posMasterChildId: In(_posMasters.map((x) => x.id)),
}); });
// await posMasterRepository.remove(_posMasters); // await posMasterRepository.remove(_posMasters);
@ -2659,24 +2707,26 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
await child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
// Task #2160 อัพเดทหน้าที่จัดการโครงสร้างแบบร่าง // Task #2160 อัพเดทหน้าที่จัดการโครงสร้างแบบร่าง
if (["ORG", "ORG_POSITION", "ORG_POSITION_PERSON", "ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(requestBody.typeDraft?.toUpperCase())) { if (
[
"ORG",
"ORG_POSITION",
"ORG_POSITION_PERSON",
"ORG_POSITION_ROLE",
"ORG_POSITION_PERSON_ROLE",
].includes(requestBody.typeDraft?.toUpperCase())
) {
const _newRoots = await orgRootRepository.find({ const _newRoots = await orgRootRepository.find({
where: { orgRevisionId: revision.id } where: { orgRevisionId: revision.id },
}); });
const newRootMap = new Map( const newRootMap = new Map(_newRoots.map((r) => [r.ancestorDNA, r.id]));
_newRoots.map(r => [r.ancestorDNA, r.id])
);
for (const oldRoot of _roots) { for (const oldRoot of _roots) {
const newRootId = newRootMap.get(oldRoot.ancestorDNA); const newRootId = newRootMap.get(oldRoot.ancestorDNA);
if (!newRootId) continue; if (!newRootId) continue;
// อัพเดท orgRootId ที่อยู่ภายใต้ orgRevision แบบร่างเดิมเป็นของ orgRevision แบบร่างใหม่ // อัพเดท orgRootId ที่อยู่ภายใต้ orgRevision แบบร่างเดิมเป็นของ orgRevision แบบร่างใหม่
await permissionOrgRepository.update( await permissionOrgRepository.update({ orgRootId: oldRoot.id }, { orgRootId: newRootId });
{ orgRootId: oldRoot.id },
{ orgRootId: newRootId }
);
} }
} } else {
else {
await permissionOrgRepository.delete({ await permissionOrgRepository.delete({
orgRootId: In(_roots.map((x) => x.id)), orgRootId: In(_roots.map((x) => x.id)),
}); });