This commit is contained in:
AdisakKanthawilang 2025-06-20 15:51:36 +07:00
parent de92ad520c
commit 3403b2201e

View file

@ -397,7 +397,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const orgRevisionPublish = await repoOrgRevision
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
@ -406,12 +406,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.where("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false")
.getOne();
if (orgRevisionPublish) {
if (orgRevisionPublish) { //เข้าเงื่อนไขจะเปลี่ยนสถานะ orgRevisionPublish เป็นไม่ใช่ current และไม่เป็น daft
orgRevisionPublish.orgRevisionIsDraft = false;
orgRevisionPublish.orgRevisionIsCurrent = false;
await repoOrgRevision.save(orgRevisionPublish);
}
if (orgRevisionDraft) {
if (orgRevisionDraft) { //เข้าเงื่อนไขจะเปลี่ยนสถานะ orgRevisionDraft เป็นไม่ใช่ daft และเป็น current
orgRevisionDraft.orgRevisionIsCurrent = true;
orgRevisionDraft.orgRevisionIsDraft = false;
await repoOrgRevision.save(orgRevisionDraft);
@ -458,7 +458,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
if (orgRevisionPublish != null && orgRevisionDraft != null) {
//new main revision
const before = null;
//ทุก orgRoot และ orgChild ข้างล่างนี้จะเป็นตัวเก่าที่ไม่ได้เป็น current revision
//cone tree
// if (
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG" ||
@ -471,7 +472,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const orgRoot = await orgRootRepository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild1 = await child1Repository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
@ -509,6 +510,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
: x.ancestorDNA,
}));
await repoEmployeePosmaster.save(_orgemployeePosMaster);
// }
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgemployeeTempPosMaster = await repoEmployeeTempPosmaster.find({
@ -532,14 +534,30 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
}));
await repoEmployeeTempPosmaster.save(_orgemployeeTempPosMaster);
// }
//create org
orgRoot.forEach(async (x: any) => {
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 (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
//create employeePosmaster
@ -569,7 +587,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
@ -629,7 +647,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
@ -669,6 +687,21 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.filter((x: OrgChild1) => x.orgRootId == dataId)
.forEach(async (x: any) => {
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}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
@ -681,6 +714,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.filter((x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child1] orgChild1Id == data1Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = [];
// if (
@ -702,8 +736,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
@ -745,7 +779,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.map(async (item: any) => {
delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
employeeTempPosMaster.positions = [];
employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
@ -765,8 +799,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
@ -806,6 +840,21 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.filter((x: OrgChild2) => x.orgChild1Id == data1Id)
.forEach(async (x: any) => {
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}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
@ -820,6 +869,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child2] orgChild2Id == data2Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = [];
// if (
@ -841,9 +891,9 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.orgChild2Id = data2Id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
@ -951,6 +1001,21 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.filter((x: OrgChild3) => x.orgChild2Id == data2Id)
.forEach(async (x: any) => {
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}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
@ -966,6 +1031,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child3] orgChild3Id == data3Id");
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = [];
// if (
@ -987,10 +1053,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.orgChild2Id = data2Id;
employeePosMaster.orgChild3Id = data3Id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null;
employeePosMaster.orgChild3Id = matchedOrgChild3?.id ?? null;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
@ -1056,10 +1122,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
employeeTempPosMaster.orgChild2Id = data2Id;
employeeTempPosMaster.orgChild3Id = data3Id;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
employeeTempPosMaster.orgChild2Id = matchedOrgChild2?.id ?? null;
employeeTempPosMaster.orgChild3Id = matchedOrgChild3?.id ?? null;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
@ -1099,6 +1165,21 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.filter((x: OrgChild4) => x.orgChild3Id == data3Id)
.forEach(async (x: any) => {
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}`);
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
@ -1111,6 +1192,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
.filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id)
.map(async (item: any) => {
delete item.id;
// console.log("[in case Child4] orgChild4Id == data4Id");
const employeePosMaster = Object.assign(
new EmployeePosMaster(),
item,
@ -1135,11 +1217,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeePosMaster.current_holderId = null;
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.orgChild2Id = data2Id;
employeePosMaster.orgChild3Id = data3Id;
employeePosMaster.orgChild4Id = data4Id;
employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null;
employeePosMaster.orgChild3Id = matchedOrgChild3?.id ?? null;
employeePosMaster.orgChild4Id = matchedOrgChild4?.id ?? null;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
@ -1202,11 +1284,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
// }
// employeeTempPosMaster.current_holderId = null;
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
employeeTempPosMaster.orgChild2Id = data2Id;
employeeTempPosMaster.orgChild3Id = data3Id;
employeeTempPosMaster.orgChild4Id = data4Id;
employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null;
employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null;
employeeTempPosMaster.orgChild2Id = matchedOrgChild2?.id ?? null;
employeeTempPosMaster.orgChild3Id = matchedOrgChild3?.id ?? null;
employeeTempPosMaster.orgChild4Id = matchedOrgChild4?.id ?? null;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
@ -1436,8 +1518,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
// Create org
for await (const x0 of _orgRoot) {
var dataId = x0.id;
console.log(`ch0 ${x0.orgRootOrder}`);
console.log(dataId);
// console.log(`ch0 ${x0.orgRootOrder}`);
// console.log(dataId);
delete x0.id;
const data = Object.assign(new OrgRoot(), x0);
data.orgRevisionId = revision.id;
@ -1534,8 +1616,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
// Create orgChild1
for await (const x1 of _orgChild1.filter((x: OrgChild1) => x.orgRootId == dataId)) {
var data1Id = x1.id;
console.log(`ch1 ${x1.orgChild1Order}`);
console.log(data1Id);
// console.log(`ch1 ${x1.orgChild1Order}`);
// console.log(data1Id);
delete x1.id;
const data1 = Object.assign(new OrgChild1(), x1);
data1.orgRootId = data.id;
@ -1627,8 +1709,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
}
for await (const x2 of _orgChild2.filter((x: OrgChild2) => x.orgChild1Id == data1Id)) {
var data2Id = x2.id;
console.log(`ch2 ${x2.orgChild2Order}`);
console.log(data2Id);
// console.log(`ch2 ${x2.orgChild2Order}`);
// console.log(data2Id);
delete x2.id;
const data2 = Object.assign(new OrgChild2(), x2);
data2.orgChild1Id = data1.id;
@ -1722,8 +1804,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
// Create org
for await (const x3 of _orgChild3.filter((x: OrgChild3) => x.orgChild2Id == data2Id)) {
var data3Id = x3.id;
console.log(`ch3 ${x3.orgChild3Order}`);
console.log(data3Id);
// console.log(`ch3 ${x3.orgChild3Order}`);
// console.log(data3Id);
delete x3.id;
const data3 = Object.assign(new OrgChild3(), x3);
data3.orgChild2Id = data2.id;
@ -1820,8 +1902,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
(x: OrgChild4) => x.orgChild3Id == data3Id,
)) {
var data4Id = x4.id;
console.log(`ch4 ${x4.orgChild4Order}`);
console.log(data4Id);
// console.log(`ch4 ${x4.orgChild4Order}`);
// console.log(data4Id);
delete x4.id;
const data4 = Object.assign(new OrgChild4(), x4);
data4.orgChild3Id = data3.id;
@ -1921,8 +2003,9 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
}
}
}
//ลบ daft เก่าที่ค้างแล้วยังไม่ได้เผยแพร่
const _orgRevisions = await orgRevisionRepository.find({
where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }],
where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }],
});
const _roots = await orgRootRepository.find({
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],