fix โคลนสิทธิ์เมนู task #2160
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m0s

This commit is contained in:
harid 2026-01-13 14:57:43 +07:00
parent 7a25dc98aa
commit 96a2d34c1f
2 changed files with 36 additions and 25 deletions

View file

@ -17,7 +17,7 @@ import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import { OrgRoot } from "../entities/OrgRoot";
import { PosMasterAssign } from "../entities/PosMasterAssign";
import { PosMasterAssign, PosMasterAssignDTO } from "../entities/PosMasterAssign";
import { Position } from "../entities/Position";
import { In, Not } from "typeorm";
import { PosMasterAct } from "../entities/PosMasterAct";
@ -561,7 +561,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
"positions.posExecutive",
],
});
// ดึง posMasterAssign ของ revision เดิม xxx
// Task #2160 ดึง posMasterAssign ของ revision เดิม
const oldposMasterAssigns = await posMasterAssignRepository.find({
relations: ["posMaster"],
where: {
@ -570,14 +570,18 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
},
},
});
// สร้าง Map: ancestorDNA → posMasterAssign[]
const assignMap = new Map<string, PosMasterAssign[]>();
// สร้าง assignMap เอาไว้เก็บ posMasterAssign.ancestorDNA ของ revision เดิม
const assignMap = new Map<string, PosMasterAssignDTO[]>();
for (const posmasterAssign of oldposMasterAssigns) {
const dna = posmasterAssign.posMaster.ancestorDNA;
if (!assignMap.has(dna)) {
assignMap.set(dna, []);
}
assignMap.get(dna)!.push(posmasterAssign);
assignMap.get(dna)!.push({
id: posmasterAssign.id,
posMasterId: posmasterAssign.posMasterId,
assignId: posmasterAssign.assignId
});
}
// ดึง posMasterAct ของ revision เดิม xxx
@ -612,18 +616,18 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const _null: any = null;
for (const item of posMaster) {
// Clone posMasterAssign xxx
// Task #2160 Clone posMasterAssign
const assigns = assignMap.get(item.ancestorDNA);
if (assigns && assigns.length > 0) {
const newAssigns = assigns.map(({ id, ...fields }) => ({
...fields, // copy ทุก field ยกเว้น id
posMasterId: item.id, // ผูกกับ posMasterId ใหม่
createdAt: new Date(),
createdFullName: user.name,
createdUserId: user.sub,
lastUpdatedAt: new Date(),
lastUpdateFullName: user.name,
lastUpdateUserId: user.sub,
createdAt: lastUpdatedAt,
createdFullName: lastUpdateFullName,
createdUserId: lastUpdateUserId,
lastUpdatedAt: lastUpdatedAt,
lastUpdateFullName: lastUpdateFullName,
lastUpdateUserId: lastUpdateUserId,
}));
await posMasterAssignRepository.save(newAssigns);
}
@ -720,11 +724,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
const orgChild4 = await child4Repository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
// Task #2172 ดึง orgRoot ของ revision ใหม่
const newRoots = await orgRootRepository.find({
where: { orgRevisionId: orgRevisionDraft.id },
});
// map ancestorDNA -> newRootId
// สร้าง newRootMap เอาไว้เก็บ orgRoot.ancestorDNA ของ revision ใหม่
const newRootMap = new Map(
newRoots.map(r => [r.ancestorDNA, r.id])
);
@ -739,23 +743,24 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
});
const inserts: any[] = [];
for (const permiss of oldPermissionProfiles) {
// หา orgRootId ใหม่จาก newRootMap
const newRootId = newRootMap.get(permiss.orgRootTree.ancestorDNA);
if (!newRootId) continue;
const { id, ...fields } = permiss;
// ตัด id กับ orgRootTree ออกแล้วสร้าง object ใหม่
const { id, orgRootTree, ...fields } = permiss;
// เตรียมข้อมูลสำหรับ insert
inserts.push({
...fields,
orgRootId: newRootId,
createdAt: new Date(),
createdFullName: user.name,
createdUserId: user.sub,
lastUpdatedAt: new Date(),
lastUpdateFullName: user.name,
lastUpdateUserId: user.sub,
createdAt: lastUpdatedAt,
createdFullName: lastUpdateFullName,
createdUserId: lastUpdateUserId,
lastUpdatedAt: lastUpdatedAt,
lastUpdateFullName: lastUpdateFullName,
lastUpdateUserId: lastUpdateUserId,
});
}
// ทำการ insert ข้อมูลใหม่ครั้งเดียว
if (inserts.length > 0) {
await permissionProfilesRepository.insert(inserts);
}
@ -2422,7 +2427,7 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
await child3Repository.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)) });
// ถ้าเลือกทำสำเนาให้อัพเดทจากแบบร่างเดิมไปแบบร่างใหม่แทนการลบ xxx
// Task #2160 อัพเดทหน้าที่จัดการโครงสร้างแบบร่าง
if (["ORG", "ORG_POSITION", "ORG_POSITION_PERSON", "ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(requestBody.typeDraft?.toUpperCase())) {
const _newRoots = await orgRootRepository.find({
where: { orgRevisionId: revision.id }