refactor code function handler_org
This commit is contained in:
parent
e6c3e80a3d
commit
869bb093a3
1 changed files with 649 additions and 662 deletions
|
|
@ -92,13 +92,12 @@ export async function init() {
|
||||||
// createConsumer(queue2, channel, handler2);
|
// createConsumer(queue2, channel, handler2);
|
||||||
}
|
}
|
||||||
|
|
||||||
let retries = 0;
|
|
||||||
|
|
||||||
function createConsumer( //----> consumer
|
function createConsumer( //----> consumer
|
||||||
queue: string,
|
queue: string,
|
||||||
channel: amqp.Channel,
|
channel: amqp.Channel,
|
||||||
handler: (msg: amqp.ConsumeMessage) => Promise<boolean> | boolean,
|
handler: (msg: amqp.ConsumeMessage) => Promise<boolean> | boolean,
|
||||||
) {
|
) {
|
||||||
|
let retries = 0;
|
||||||
channel.consume(
|
channel.consume(
|
||||||
queue,
|
queue,
|
||||||
async (msg) => {
|
async (msg) => {
|
||||||
|
|
@ -405,7 +404,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 +481,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 +495,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()}`);
|
||||||
|
|
||||||
|
|
@ -520,6 +518,30 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
|
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
|
||||||
console.log(`[AMQ] Received message - revisionId: ${id}, status: ${status}`);
|
console.log(`[AMQ] Received message - revisionId: ${id}, status: ${status}`);
|
||||||
|
|
||||||
|
const targetOrgRevision = await repoOrgRevision.findOne({
|
||||||
|
where: { id },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!targetOrgRevision) {
|
||||||
|
console.error(`[AMQ] Skip publish: revision ${id} not found`);
|
||||||
|
console.timeEnd("[AMQ] handler_org_total");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetOrgRevision.orgRevisionIsCurrent && !targetOrgRevision.orgRevisionIsDraft) {
|
||||||
|
console.log(`[AMQ] Skip publish: revision ${id} is already current`);
|
||||||
|
console.timeEnd("[AMQ] handler_org_total");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!targetOrgRevision.orgRevisionIsDraft || targetOrgRevision.orgRevisionIsCurrent) {
|
||||||
|
console.log(
|
||||||
|
`[AMQ] Skip publish: revision ${id} is no longer publishable (isDraft=${targetOrgRevision.orgRevisionIsDraft}, isCurrent=${targetOrgRevision.orgRevisionIsCurrent})`,
|
||||||
|
);
|
||||||
|
console.timeEnd("[AMQ] handler_org_total");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
sendWebSocket(
|
sendWebSocket(
|
||||||
"send-publish-org",
|
"send-publish-org",
|
||||||
|
|
@ -531,25 +553,30 @@ 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, orgRevisionDraft] = await Promise.all([
|
||||||
|
repoOrgRevision
|
||||||
.createQueryBuilder("orgRevision")
|
.createQueryBuilder("orgRevision")
|
||||||
.where("orgRevision.orgRevisionIsDraft = false")
|
.where("orgRevision.orgRevisionIsDraft = false")
|
||||||
.andWhere("orgRevision.orgRevisionIsCurrent = true")
|
.andWhere("orgRevision.orgRevisionIsCurrent = true")
|
||||||
.getOne();
|
.getOne(),
|
||||||
|
repoOrgRevision
|
||||||
const orgRevisionDraft = await repoOrgRevision
|
|
||||||
.createQueryBuilder("orgRevision")
|
.createQueryBuilder("orgRevision")
|
||||||
.where("orgRevision.orgRevisionIsDraft = true")
|
.where("orgRevision.orgRevisionIsDraft = true")
|
||||||
.andWhere("orgRevision.orgRevisionIsCurrent = false")
|
.andWhere("orgRevision.orgRevisionIsCurrent = false")
|
||||||
.getOne();
|
.getOne(),
|
||||||
console.timeEnd('[AMQ] query_revisions');
|
]);
|
||||||
console.log(`[AMQ] orgRevisionPublish found: ${orgRevisionPublish ? orgRevisionPublish.id : 'null'}`);
|
console.timeEnd("[AMQ] query_revisions");
|
||||||
console.log(`[AMQ] orgRevisionDraft found: ${orgRevisionDraft ? orgRevisionDraft.id : 'null'}`);
|
console.log(
|
||||||
|
`[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 +592,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",
|
||||||
|
|
@ -579,11 +608,19 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (orgRevisionDraft.id !== targetOrgRevision.id) {
|
||||||
|
console.log(
|
||||||
|
`[AMQ] Skip publish: revision ${id} is stale because draft ${orgRevisionDraft.id} is now the active publish candidate`,
|
||||||
|
);
|
||||||
|
console.timeEnd("[AMQ] handler_org_total");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: ย้ายการอัปเดตสถานะไปไว้หลังจากทำงานเสร็จทั้งหมด
|
// NOTE: ย้ายการอัปเดตสถานะไปไว้หลังจากทำงานเสร็จทั้งหมด
|
||||||
// เพื่อป้องกันกรณี timeout/retry ทำให้สถานะเพี้ยน (ทุก row เป็น false,false)
|
// เพื่อป้องกันกรณี timeout/retry ทำให้สถานะเพี้ยน (ทุก row เป็น false,false)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.time('[AMQ] query_posMaster');
|
console.time("[AMQ] query_posMaster");
|
||||||
const posMaster = await repoPosmaster.find({
|
const posMaster = await repoPosmaster.find({
|
||||||
where: { orgRevisionId: id },
|
where: { orgRevisionId: id },
|
||||||
relations: [
|
relations: [
|
||||||
|
|
@ -598,15 +635,15 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
"positions.posExecutive",
|
"positions.posExecutive",
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
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 เดิม
|
||||||
|
|
@ -618,11 +655,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) {
|
||||||
|
|
@ -633,12 +670,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"],
|
||||||
|
|
@ -648,16 +685,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)) {
|
||||||
|
|
@ -668,7 +705,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>();
|
||||||
|
|
@ -678,25 +715,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}`);
|
||||||
|
|
||||||
|
|
@ -724,7 +761,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);
|
||||||
}
|
}
|
||||||
|
|
@ -775,33 +812,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");
|
||||||
for (const update of posMasterUpdates) {
|
for (const update of posMasterUpdates) {
|
||||||
await repoPosmaster.update(update.id, {
|
await repoPosmaster.update(update.id, {
|
||||||
current_holderId: update.current_holderId,
|
current_holderId: update.current_holderId,
|
||||||
|
|
@ -811,20 +850,20 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
lastUpdatedAt,
|
lastUpdatedAt,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
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");
|
||||||
for (const id of historyCreateIds) {
|
for (const id of historyCreateIds) {
|
||||||
await CreatePosMasterHistoryOfficer(id, null);
|
await CreatePosMasterHistoryOfficer(id, null);
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
|
@ -847,16 +886,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 },
|
||||||
|
|
@ -877,27 +916,55 @@ 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])
|
const emptyAncestorDNA = "00000000-0000-0000-0000-000000000000";
|
||||||
);
|
const hasEmptyAncestorDNA = (ancestorDNA?: string | null) =>
|
||||||
|
ancestorDNA == null || ancestorDNA === emptyAncestorDNA;
|
||||||
|
const hasSelfOrEmptyAncestorDNA = (node: { id: string; ancestorDNA: string | null }) =>
|
||||||
|
node.ancestorDNA === node.id || hasEmptyAncestorDNA(node.ancestorDNA);
|
||||||
|
const findMatchedNodeByAncestorDNA = <T extends { id: string; ancestorDNA: string | null }>(
|
||||||
|
nodes: T[],
|
||||||
|
node: T,
|
||||||
|
) =>
|
||||||
|
nodes.find((item) => {
|
||||||
|
if (hasSelfOrEmptyAncestorDNA(node)) {
|
||||||
|
return hasEmptyAncestorDNA(item.ancestorDNA);
|
||||||
|
}
|
||||||
|
return item.ancestorDNA === node.ancestorDNA;
|
||||||
|
});
|
||||||
|
const [
|
||||||
|
orgRootCurrent,
|
||||||
|
orgChild1Current,
|
||||||
|
orgChild2Current,
|
||||||
|
orgChild3Current,
|
||||||
|
orgChild4Current,
|
||||||
|
] = await Promise.all([
|
||||||
|
orgRootRepository.find({ where: { orgRevisionId: orgRevisionDraft.id } }),
|
||||||
|
child1Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }),
|
||||||
|
child2Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }),
|
||||||
|
child3Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }),
|
||||||
|
child4Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }),
|
||||||
|
]);
|
||||||
|
|
||||||
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) {
|
||||||
|
|
@ -922,15 +989,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[];
|
||||||
|
|
@ -962,7 +1029,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()
|
||||||
|
|
@ -973,16 +1040,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[];
|
||||||
|
|
@ -1012,30 +1079,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
.execute();
|
.execute();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//create org - forEach orgRoot (WARNING: async forEach without await)
|
//create org
|
||||||
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;
|
for (const x of orgRoot) {
|
||||||
orgRoot.forEach(async (x: any) => {
|
const dataId = x.id;
|
||||||
const itemStartTime = Date.now();
|
const matchedOrgRoot = findMatchedNodeByAncestorDNA(orgRootCurrent, x);
|
||||||
var dataId = x.id;
|
|
||||||
|
|
||||||
const orgRootCurrent = await orgRootRepository.find({
|
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const matchedOrgRoot = orgRootCurrent.find((i: OrgRoot) => {
|
|
||||||
if (
|
|
||||||
x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง
|
|
||||||
x.ancestorDNA === null ||
|
|
||||||
x.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
i.ancestorDNA === null || i.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return i.ancestorDNA === x.ancestorDNA;
|
|
||||||
});
|
|
||||||
|
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
||||||
|
|
@ -1044,12 +1093,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 = [];
|
||||||
|
|
@ -1082,6 +1131,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeePosmaster.save(employeePosMaster);
|
await repoEmployeePosmaster.save(employeePosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1102,7 +1152,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
//create employeeTempPosmaster
|
//create employeeTempPosmaster
|
||||||
|
|
@ -1142,6 +1193,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1162,32 +1214,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//create org
|
//create org
|
||||||
orgChild1
|
for (const x of orgChild1.filter((item: OrgChild1) => item.orgRootId == dataId)) {
|
||||||
.filter((x: OrgChild1) => x.orgRootId == dataId)
|
const data1Id = x.id;
|
||||||
.forEach(async (x: any) => {
|
const matchedOrgChild1 = findMatchedNodeByAncestorDNA(orgChild1Current, x);
|
||||||
var data1Id = x.id;
|
|
||||||
const orgChild1Current = await child1Repository.find({
|
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const matchedOrgChild1 = orgChild1Current.find((i: OrgChild1) => {
|
|
||||||
if (
|
|
||||||
x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง
|
|
||||||
x.ancestorDNA === null ||
|
|
||||||
x.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
i.ancestorDNA === null || i.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return i.ancestorDNA === x.ancestorDNA;
|
|
||||||
});
|
|
||||||
// ("[in case Child1] ancestorDNA", `${x.orgChild1Id == matchedOrgChild1?.id}`);
|
// ("[in case Child1] ancestorDNA", `${x.orgChild1Id == matchedOrgChild1?.id}`);
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
||||||
|
|
@ -1234,6 +1270,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeePosmaster.save(employeePosMaster);
|
await repoEmployeePosmaster.save(employeePosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1254,7 +1291,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// create employeeTempPosmaster
|
// create employeeTempPosmaster
|
||||||
|
|
@ -1297,6 +1335,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1317,33 +1356,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//create org
|
//create org
|
||||||
orgChild2
|
for (const x of orgChild2.filter((item: OrgChild2) => item.orgChild1Id == data1Id)) {
|
||||||
.filter((x: OrgChild2) => x.orgChild1Id == data1Id)
|
const data2Id = x.id;
|
||||||
.forEach(async (x: any) => {
|
const matchedOrgChild2 = findMatchedNodeByAncestorDNA(orgChild2Current, x);
|
||||||
var data2Id = x.id;
|
|
||||||
const orgChild2Current = await child2Repository.find({
|
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const matchedOrgChild2 = orgChild2Current.find((i: OrgChild2) => {
|
|
||||||
if (
|
|
||||||
x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง
|
|
||||||
x.ancestorDNA === null ||
|
|
||||||
x.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
i.ancestorDNA === null ||
|
|
||||||
i.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return i.ancestorDNA === x.ancestorDNA;
|
|
||||||
});
|
|
||||||
// console.log("[in case Child2] ancestorDNA", `${x.orgChild2Id == matchedOrgChild2?.id}`);
|
// console.log("[in case Child2] ancestorDNA", `${x.orgChild2Id == matchedOrgChild2?.id}`);
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
||||||
|
|
@ -1354,9 +1376,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
//create employeePosmaster
|
//create employeePosmaster
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
_orgemployeePosMaster
|
_orgemployeePosMaster
|
||||||
.filter(
|
.filter((x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null)
|
||||||
(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");
|
// console.log("[in case Child2] orgChild2Id == data2Id");
|
||||||
|
|
@ -1393,6 +1413,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeePosmaster.save(employeePosMaster);
|
await repoEmployeePosmaster.save(employeePosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1413,22 +1434,19 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// create employeeTempPosmaster
|
// create employeeTempPosmaster
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
_orgemployeeTempPosMaster
|
_orgemployeeTempPosMaster
|
||||||
.filter(
|
.filter(
|
||||||
(x: EmployeeTempPosMaster) =>
|
(x: EmployeeTempPosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null,
|
||||||
x.orgChild2Id == data2Id && x.orgChild3Id == null,
|
|
||||||
)
|
)
|
||||||
.map(async (item: any) => {
|
.map(async (item: any) => {
|
||||||
delete item.id;
|
delete item.id;
|
||||||
const employeeTempPosMaster = Object.assign(
|
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
|
||||||
new EmployeeTempPosMaster(),
|
|
||||||
item,
|
|
||||||
);
|
|
||||||
employeeTempPosMaster.positions = [];
|
employeeTempPosMaster.positions = [];
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
|
||||||
|
|
@ -1461,6 +1479,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1481,33 +1500,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//create org
|
//create org
|
||||||
orgChild3
|
for (const x of orgChild3.filter((item: OrgChild3) => item.orgChild2Id == data2Id)) {
|
||||||
.filter((x: OrgChild3) => x.orgChild2Id == data2Id)
|
const data3Id = x.id;
|
||||||
.forEach(async (x: any) => {
|
const matchedOrgChild3 = findMatchedNodeByAncestorDNA(orgChild3Current, x);
|
||||||
var data3Id = x.id;
|
|
||||||
const orgChild3Current = await child3Repository.find({
|
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const matchedOrgChild3 = orgChild3Current.find((i: OrgChild3) => {
|
|
||||||
if (
|
|
||||||
x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง
|
|
||||||
x.ancestorDNA === null ||
|
|
||||||
x.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
i.ancestorDNA === null ||
|
|
||||||
i.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return i.ancestorDNA === x.ancestorDNA;
|
|
||||||
});
|
|
||||||
// console.log("[in case Child3] ancestorDNA", `${x.orgChild3Id == matchedOrgChild3?.id}`);
|
// console.log("[in case Child3] ancestorDNA", `${x.orgChild3Id == matchedOrgChild3?.id}`);
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
||||||
|
|
@ -1519,8 +1521,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
_orgemployeePosMaster
|
_orgemployeePosMaster
|
||||||
.filter(
|
.filter(
|
||||||
(x: EmployeePosMaster) =>
|
(x: EmployeePosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null,
|
||||||
x.orgChild3Id == data3Id && x.orgChild4Id == null,
|
|
||||||
)
|
)
|
||||||
.map(async (item: any) => {
|
.map(async (item: any) => {
|
||||||
delete item.id;
|
delete item.id;
|
||||||
|
|
@ -1559,6 +1560,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeePosmaster.save(employeePosMaster);
|
await repoEmployeePosmaster.save(employeePosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1579,22 +1581,19 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// create employeeTempPosmaster
|
// create employeeTempPosmaster
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
_orgemployeeTempPosMaster
|
_orgemployeeTempPosMaster
|
||||||
.filter(
|
.filter(
|
||||||
(x: EmployeeTempPosMaster) =>
|
(x: EmployeeTempPosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null,
|
||||||
x.orgChild3Id == data3Id && x.orgChild4Id == null,
|
|
||||||
)
|
)
|
||||||
.map(async (item: any) => {
|
.map(async (item: any) => {
|
||||||
delete item.id;
|
delete item.id;
|
||||||
const employeeTempPosMaster = Object.assign(
|
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
|
||||||
new EmployeeTempPosMaster(),
|
|
||||||
item,
|
|
||||||
);
|
|
||||||
employeeTempPosMaster.positions = [];
|
employeeTempPosMaster.positions = [];
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
|
||||||
|
|
@ -1628,6 +1627,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1648,33 +1648,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//create org
|
//create org
|
||||||
orgChild4
|
for (const x of orgChild4.filter((item: OrgChild4) => item.orgChild3Id == data3Id)) {
|
||||||
.filter((x: OrgChild4) => x.orgChild3Id == data3Id)
|
const data4Id = x.id;
|
||||||
.forEach(async (x: any) => {
|
const matchedOrgChild4 = findMatchedNodeByAncestorDNA(orgChild4Current, x);
|
||||||
var data4Id = x.id;
|
|
||||||
const orgChild4Current = await child4Repository.find({
|
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const matchedOrgChild4 = orgChild4Current.find((i: OrgChild4) => {
|
|
||||||
if (
|
|
||||||
x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง
|
|
||||||
x.ancestorDNA === null ||
|
|
||||||
x.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
i.ancestorDNA === null ||
|
|
||||||
i.ancestorDNA === "00000000-0000-0000-0000-000000000000"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return i.ancestorDNA === x.ancestorDNA;
|
|
||||||
});
|
|
||||||
// console.log("[in case Child4] ancestorDNA", `${x.orgChild4Id == matchedOrgChild4?.id}`);
|
// console.log("[in case Child4] ancestorDNA", `${x.orgChild4Id == matchedOrgChild4?.id}`);
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
||||||
|
|
@ -1689,10 +1672,7 @@ 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 Child4] orgChild4Id == data4Id");
|
// console.log("[in case Child4] orgChild4Id == data4Id");
|
||||||
const employeePosMaster = Object.assign(
|
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
|
||||||
new EmployeePosMaster(),
|
|
||||||
item,
|
|
||||||
);
|
|
||||||
employeePosMaster.positions = [];
|
employeePosMaster.positions = [];
|
||||||
// if (
|
// if (
|
||||||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
|
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
|
||||||
|
|
@ -1727,6 +1707,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeePosmaster.save(employeePosMaster);
|
await repoEmployeePosmaster.save(employeePosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1747,7 +1728,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
//create employeeTempPosmaster
|
//create employeeTempPosmaster
|
||||||
|
|
@ -1794,6 +1776,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
|
||||||
|
|
||||||
//create employeePosition
|
//create employeePosition
|
||||||
|
await Promise.all(
|
||||||
item.positions.map(async (pos: any) => {
|
item.positions.map(async (pos: any) => {
|
||||||
delete pos.id;
|
delete pos.id;
|
||||||
const employeePosition: EmployeePosition = Object.assign(
|
const employeePosition: EmployeePosition = Object.assign(
|
||||||
|
|
@ -1814,15 +1797,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
employeePosition.lastUpdateFullName = "System Administrator";
|
employeePosition.lastUpdateFullName = "System Administrator";
|
||||||
employeePosition.lastUpdatedAt = new Date();
|
employeePosition.lastUpdatedAt = new Date();
|
||||||
await employeePositionRepository.save(employeePosition);
|
await employeePositionRepository.save(employeePosition);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// }
|
// }
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
// }
|
// }
|
||||||
|
|
||||||
const employeePosMaster = await repoEmployeePosmaster.find({
|
const employeePosMaster = await repoEmployeePosmaster.find({
|
||||||
|
|
@ -1848,7 +1832,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
item.lastUpdateUserId = lastUpdateUserId;
|
item.lastUpdateUserId = lastUpdateUserId;
|
||||||
item.lastUpdateFullName = lastUpdateFullName;
|
item.lastUpdateFullName = lastUpdateFullName;
|
||||||
item.lastUpdatedAt = lastUpdatedAt;
|
item.lastUpdatedAt = lastUpdatedAt;
|
||||||
await repoEmployeePosmaster.save(item).catch((e) => console.log(e));
|
await repoEmployeePosmaster.save(item);
|
||||||
}
|
}
|
||||||
const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({
|
const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
where: { orgRevisionId: orgRevisionDraft.id },
|
||||||
|
|
@ -1873,7 +1857,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
item.lastUpdateUserId = lastUpdateUserId;
|
item.lastUpdateUserId = lastUpdateUserId;
|
||||||
item.lastUpdateFullName = lastUpdateFullName;
|
item.lastUpdateFullName = lastUpdateFullName;
|
||||||
item.lastUpdatedAt = lastUpdatedAt;
|
item.lastUpdatedAt = lastUpdatedAt;
|
||||||
await repoEmployeeTempPosmaster.save(item).catch((e) => console.log(e));
|
await repoEmployeeTempPosmaster.save(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("[AMQ] Excecute Organization Success");
|
console.log("[AMQ] Excecute Organization Success");
|
||||||
|
|
@ -1887,10 +1871,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);
|
||||||
|
|
@ -1898,10 +1882,10 @@ 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");
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const totalTime = Date.now() - startTime;
|
const totalTime = Date.now() - startTime;
|
||||||
|
|
@ -1916,7 +1900,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2591,7 +2575,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);
|
||||||
|
|
@ -2619,24 +2604,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)),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue