log test publish
This commit is contained in:
parent
2aaaf53ab0
commit
e33ee0c725
2 changed files with 135 additions and 25 deletions
|
|
@ -2532,11 +2532,16 @@ export class OrganizationController extends Controller {
|
||||||
* Cronjob
|
* Cronjob
|
||||||
*/
|
*/
|
||||||
async cronjobRevision() {
|
async cronjobRevision() {
|
||||||
|
console.log('[CronJob] cronjobRevision START');
|
||||||
|
const startTime = Date.now();
|
||||||
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
today.setUTCHours(0, 0, 0, 0); // Set time to the beginning of the day
|
today.setUTCHours(0, 0, 0, 0); // Set time to the beginning of the day
|
||||||
const tomorrow = new Date(today);
|
const tomorrow = new Date(today);
|
||||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||||
|
|
||||||
|
console.log(`[CronJob] Searching for draft revision with publishDate between ${today.toISOString()} and ${tomorrow.toISOString()}`);
|
||||||
|
|
||||||
const orgRevisionDraft = await this.orgRevisionRepository
|
const orgRevisionDraft = await this.orgRevisionRepository
|
||||||
.createQueryBuilder("orgRevision")
|
.createQueryBuilder("orgRevision")
|
||||||
.where("orgRevision.orgRevisionIsDraft = true")
|
.where("orgRevision.orgRevisionIsDraft = true")
|
||||||
|
|
@ -2545,8 +2550,12 @@ export class OrganizationController extends Controller {
|
||||||
.getOne();
|
.getOne();
|
||||||
|
|
||||||
if (!orgRevisionDraft) {
|
if (!orgRevisionDraft) {
|
||||||
|
console.log('[CronJob] No draft revision found to publish');
|
||||||
return new HttpSuccess();
|
return new HttpSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`[CronJob] Found draft revision: ${orgRevisionDraft.id}, name: ${orgRevisionDraft.orgRevisionName}, publishDate: ${orgRevisionDraft.orgPublishDate}`);
|
||||||
|
|
||||||
// if (orgRevisionPublish) {
|
// if (orgRevisionPublish) {
|
||||||
// orgRevisionPublish.orgRevisionIsDraft = false;
|
// orgRevisionPublish.orgRevisionIsDraft = false;
|
||||||
// orgRevisionPublish.orgRevisionIsCurrent = false;
|
// orgRevisionPublish.orgRevisionIsCurrent = false;
|
||||||
|
|
@ -2575,7 +2584,10 @@ export class OrganizationController extends Controller {
|
||||||
lastUpdatedAt: new Date(),
|
lastUpdatedAt: new Date(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(`[CronJob] Sending to RabbitMQ queue - revisionId: ${orgRevisionDraft.id}`);
|
||||||
sendToQueueOrg(msg);
|
sendToQueueOrg(msg);
|
||||||
|
console.log(`[CronJob] Sent to queue successfully - Total time: ${Date.now() - startTime}ms`);
|
||||||
return new HttpSuccess();
|
return new HttpSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -497,6 +497,10 @@ 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');
|
||||||
|
const startTime = Date.now();
|
||||||
|
console.log(`[AMQ] handler_org START at ${new Date(startTime).toISOString()}`);
|
||||||
|
|
||||||
const repoPosmaster = AppDataSource.getRepository(PosMaster);
|
const repoPosmaster = AppDataSource.getRepository(PosMaster);
|
||||||
const posMasterAssignRepository = AppDataSource.getRepository(PosMasterAssign);
|
const posMasterAssignRepository = AppDataSource.getRepository(PosMasterAssign);
|
||||||
const posMasterActRepository = AppDataSource.getRepository(PosMasterAct);
|
const posMasterActRepository = AppDataSource.getRepository(PosMasterAct);
|
||||||
|
|
@ -514,6 +518,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
const child4Repository = AppDataSource.getRepository(OrgChild4);
|
const child4Repository = AppDataSource.getRepository(OrgChild4);
|
||||||
const { data, token, user } = JSON.parse(msg.content.toString());
|
const { data, token, user } = JSON.parse(msg.content.toString());
|
||||||
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
|
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
|
||||||
|
console.log(`[AMQ] Received message - revisionId: ${id}, status: ${status}`);
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
sendWebSocket(
|
sendWebSocket(
|
||||||
"send-publish-org",
|
"send-publish-org",
|
||||||
|
|
@ -524,6 +530,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
{ userId: user?.sub },
|
{ userId: user?.sub },
|
||||||
).catch(console.error);
|
).catch(console.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
@ -535,19 +543,47 @@ 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();
|
||||||
if (orgRevisionPublish) {
|
console.timeEnd('[AMQ] query_revisions');
|
||||||
//เข้าเงื่อนไขจะเปลี่ยนสถานะ orgRevisionPublish เป็นไม่ใช่ current และไม่เป็น daft
|
console.log(`[AMQ] orgRevisionPublish found: ${orgRevisionPublish ? orgRevisionPublish.id : 'null'}`);
|
||||||
orgRevisionPublish.orgRevisionIsDraft = false;
|
console.log(`[AMQ] orgRevisionDraft found: ${orgRevisionDraft ? orgRevisionDraft.id : 'null'}`);
|
||||||
orgRevisionPublish.orgRevisionIsCurrent = false;
|
|
||||||
await repoOrgRevision.save(orgRevisionPublish);
|
// Validate: ต้องมี orgRevisionPublish เสมอสำหรับการเผยแพร่
|
||||||
|
if (!orgRevisionPublish) {
|
||||||
|
console.error('[AMQ] Cannot publish: No current org revision found (isDraft=false, isCurrent=true)');
|
||||||
|
if (user) {
|
||||||
|
sendWebSocket(
|
||||||
|
"send-publish-org",
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: `ไม่พบข้อมูลโครงสร้างหน่วยงานปัจจุบัน ไม่สามารถเผยแพร่ได้`,
|
||||||
|
},
|
||||||
|
{ userId: user?.sub },
|
||||||
|
).catch(console.error);
|
||||||
}
|
}
|
||||||
if (orgRevisionDraft) {
|
return false;
|
||||||
//เข้าเงื่อนไขจะเปลี่ยนสถานะ orgRevisionDraft เป็นไม่ใช่ daft และเป็น current
|
|
||||||
orgRevisionDraft.orgRevisionIsCurrent = true;
|
|
||||||
orgRevisionDraft.orgRevisionIsDraft = false;
|
|
||||||
await repoOrgRevision.save(orgRevisionDraft);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate: ต้องมี orgRevisionDraft ที่จะเผยแพร่
|
||||||
|
if (!orgRevisionDraft) {
|
||||||
|
console.error('[AMQ] Cannot publish: No draft org revision found (isDraft=true, isCurrent=false)');
|
||||||
|
if (user) {
|
||||||
|
sendWebSocket(
|
||||||
|
"send-publish-org",
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: `ไม่พบข้อมูลโครงสร้างหน่วยงานแบบร่าง ไม่สามารถเผยแพร่ได้`,
|
||||||
|
},
|
||||||
|
{ userId: user?.sub },
|
||||||
|
).catch(console.error);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: ย้ายการอัปเดตสถานะไปไว้หลังจากทำงานเสร็จทั้งหมด
|
||||||
|
// เพื่อป้องกันกรณี timeout/retry ทำให้สถานะเพี้ยน (ทุก row เป็น false,false)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.time('[AMQ] query_posMaster');
|
||||||
const posMaster = await repoPosmaster.find({
|
const posMaster = await repoPosmaster.find({
|
||||||
where: { orgRevisionId: id },
|
where: { orgRevisionId: id },
|
||||||
relations: [
|
relations: [
|
||||||
|
|
@ -562,10 +598,13 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
"positions.posExecutive",
|
"positions.posExecutive",
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
console.timeEnd('[AMQ] query_posMaster');
|
||||||
|
console.log(`[AMQ] posMaster count: ${posMaster.length}`);
|
||||||
|
|
||||||
|
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']
|
||||||
});
|
});
|
||||||
|
|
@ -575,10 +614,15 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
relations: ["posMaster"],
|
relations: ["posMaster"],
|
||||||
where: {
|
where: {
|
||||||
posMaster: {
|
posMaster: {
|
||||||
orgRevisionId: orgRevisionPublish!.id,
|
orgRevisionId: orgRevisionPublish.id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
console.timeEnd('[AMQ] query_old_data');
|
||||||
|
console.log(`[AMQ] oldPosMasters count: ${oldPosMasters.length}`);
|
||||||
|
console.log(`[AMQ] oldposMasterAssigns count: ${oldposMasterAssigns.length}`);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|
@ -592,19 +636,24 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
assignId: posmasterAssign.assignId
|
assignId: posmasterAssign.assignId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
console.timeEnd('[AMQ] build_assignMap');
|
||||||
|
|
||||||
|
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"],
|
||||||
where: {
|
where: {
|
||||||
posMaster: {
|
posMaster: {
|
||||||
orgRevisionId: orgRevisionPublish!.id,
|
orgRevisionId: orgRevisionPublish.id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
console.timeEnd('[AMQ] query_oldposMasterAct');
|
||||||
|
console.log(`[AMQ] oldposMasterAct count: ${oldposMasterAct.length}`);
|
||||||
|
|
||||||
type ActKey = string; // `${parentDNA}|${childDNA}`
|
type ActKey = string; // `${parentDNA}|${childDNA}`
|
||||||
|
|
||||||
|
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() ?? '';
|
||||||
|
|
@ -629,10 +678,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
oldPosMasterMap.set(dna, oldPm);
|
oldPosMasterMap.set(dna, oldPm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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');
|
||||||
// 1. รวบรวม profileIds ทั้งหมดที่ต้องอัพเดท
|
// 1. รวบรวม profileIds ทั้งหมดที่ต้องอัพเดท
|
||||||
const profileIds = posMaster
|
const profileIds = posMaster
|
||||||
.filter(item => item.next_holderId != null)
|
.filter(item => item.next_holderId != null)
|
||||||
|
|
@ -647,6 +698,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
});
|
});
|
||||||
profiles.forEach(p => profilesMap.set(p.id, p));
|
profiles.forEach(p => profilesMap.set(p.id, p));
|
||||||
}
|
}
|
||||||
|
console.log(`[AMQ] profiles to update: ${profilesMap.size}`);
|
||||||
|
|
||||||
// 3. เตรียม arrays สำหรับ batch operations
|
// 3. เตรียม arrays สำหรับ batch operations
|
||||||
const profilesToSave: Profile[] = [];
|
const profilesToSave: Profile[] = [];
|
||||||
|
|
@ -723,26 +775,33 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
historyCreateIds.push(item.id);
|
historyCreateIds.push(item.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.timeEnd('[AMQ] prepare_batch_data');
|
||||||
|
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');
|
||||||
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');
|
||||||
|
|
||||||
// 5. Batch save profiles (chunk 200)
|
// 5. Batch save profiles (chunk 200)
|
||||||
|
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');
|
||||||
|
|
||||||
// 6. Batch update posMasters
|
// 6. 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,
|
||||||
|
|
@ -752,12 +811,17 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
lastUpdatedAt,
|
lastUpdatedAt,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
console.timeEnd('[AMQ] batch_update_posMasters');
|
||||||
|
|
||||||
// 7. Batch create history
|
// 7. 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');
|
||||||
|
|
||||||
|
// 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() ?? '';
|
||||||
|
|
@ -783,20 +847,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
|
|
||||||
await posMasterActRepository.save(newAct);
|
await posMasterActRepository.save(newAct);
|
||||||
}
|
}
|
||||||
|
console.timeEnd('[AMQ] clone_oldposMasterAct');
|
||||||
|
|
||||||
if (orgRevisionPublish != null && orgRevisionDraft != null) {
|
if (orgRevisionPublish != null && orgRevisionDraft != null) {
|
||||||
|
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
|
||||||
// if (
|
console.time('[AMQ] query_old_org_structure');
|
||||||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG" ||
|
|
||||||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION" ||
|
|
||||||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
|
|
||||||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
|
|
||||||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
|
|
||||||
// ) {
|
|
||||||
//หา dna tree
|
//หา dna tree
|
||||||
const orgRoot = await orgRootRepository.find({
|
const orgRoot = await orgRootRepository.find({
|
||||||
where: { orgRevisionId: orgRevisionPublish.id },
|
where: { orgRevisionId: orgRevisionPublish.id },
|
||||||
|
|
@ -817,6 +877,9 @@ 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.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 },
|
||||||
|
|
@ -825,6 +888,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
const newRootMap = new Map(
|
const newRootMap = new Map(
|
||||||
newRoots.map(r => [r.ancestorDNA, r.id])
|
newRoots.map(r => [r.ancestorDNA, r.id])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.time('[AMQ] clone_permissionProfiles');
|
||||||
// ดึง permissionProfiles ของ revision เดิม
|
// ดึง permissionProfiles ของ revision เดิม
|
||||||
const oldPermissionProfiles = await permissionProfilesRepository.find({
|
const oldPermissionProfiles = await permissionProfilesRepository.find({
|
||||||
relations: ["orgRootTree"],
|
relations: ["orgRootTree"],
|
||||||
|
|
@ -857,12 +922,16 @@ 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');
|
||||||
|
|
||||||
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
|
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
|
||||||
|
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.log(`[AMQ] orgemployeePosMaster count: ${orgemployeePosMaster.length}`);
|
||||||
|
|
||||||
let _orgemployeePosMaster: EmployeePosMaster[];
|
let _orgemployeePosMaster: EmployeePosMaster[];
|
||||||
// if (
|
// if (
|
||||||
|
|
@ -892,6 +961,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
? x.id
|
? x.id
|
||||||
: x.ancestorDNA,
|
: x.ancestorDNA,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
console.time('[AMQ] insert_employeePosMaster');
|
||||||
await repoEmployeePosmaster
|
await repoEmployeePosmaster
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
|
|
@ -902,13 +973,17 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
overwrite: ["ancestorDNA"],
|
overwrite: ["ancestorDNA"],
|
||||||
})
|
})
|
||||||
.execute();
|
.execute();
|
||||||
|
console.timeEnd('[AMQ] insert_employeePosMaster');
|
||||||
|
|
||||||
// }
|
// }
|
||||||
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
|
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
|
||||||
|
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.log(`[AMQ] orgemployeeTempPosMaster count: ${orgemployeeTempPosMaster.length}`);
|
||||||
|
|
||||||
let _orgemployeeTempPosMaster: EmployeeTempPosMaster[];
|
let _orgemployeeTempPosMaster: EmployeeTempPosMaster[];
|
||||||
// if (
|
// if (
|
||||||
|
|
@ -937,8 +1012,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
.execute();
|
.execute();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//create org
|
//create org - forEach orgRoot (WARNING: async forEach without await)
|
||||||
|
console.time('[AMQ] forEach_orgRoot');
|
||||||
|
console.log(`[AMQ] Starting forEach orgRoot loop (${orgRoot.length} items)`);
|
||||||
|
let processedOrgRoot = 0;
|
||||||
orgRoot.forEach(async (x: any) => {
|
orgRoot.forEach(async (x: any) => {
|
||||||
|
const itemStartTime = Date.now();
|
||||||
var dataId = x.id;
|
var dataId = x.id;
|
||||||
|
|
||||||
const orgRootCurrent = await orgRootRepository.find({
|
const orgRootCurrent = await orgRootRepository.find({
|
||||||
|
|
@ -965,9 +1044,11 @@ 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
|
||||||
|
.filter((x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null);
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
_orgemployeePosMaster
|
filteredEmployeePosMaster
|
||||||
.filter((x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null)
|
|
||||||
.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);
|
||||||
|
|
@ -1806,9 +1887,25 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
{ userId: user?.sub },
|
{ userId: user?.sub },
|
||||||
).catch(console.error);
|
).catch(console.error);
|
||||||
}
|
}
|
||||||
|
console.timeEnd('[AMQ] clone_org_structure');
|
||||||
|
|
||||||
|
// อัปเดตสถานะ orgRevision หลังจากทำงานเสร็จทั้งหมด
|
||||||
|
console.time('[AMQ] save_revision_status');
|
||||||
|
orgRevisionPublish.orgRevisionIsDraft = false;
|
||||||
|
orgRevisionPublish.orgRevisionIsCurrent = false;
|
||||||
|
await repoOrgRevision.save(orgRevisionPublish);
|
||||||
|
|
||||||
|
orgRevisionDraft.orgRevisionIsCurrent = true;
|
||||||
|
orgRevisionDraft.orgRevisionIsDraft = false;
|
||||||
|
await repoOrgRevision.save(orgRevisionDraft);
|
||||||
|
console.timeEnd('[AMQ] save_revision_status');
|
||||||
|
|
||||||
|
console.log(`[AMQ] handler_org SUCCESS - Total time: ${Date.now() - startTime}ms`);
|
||||||
|
console.timeEnd('[AMQ] handler_org_total');
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
const totalTime = Date.now() - startTime;
|
||||||
|
console.error(`[AMQ] handler_org ERROR after ${totalTime}ms:`, error);
|
||||||
if (user) {
|
if (user) {
|
||||||
sendWebSocket(
|
sendWebSocket(
|
||||||
"send-publish-org",
|
"send-publish-org",
|
||||||
|
|
@ -1819,6 +1916,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');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue