This commit is contained in:
harid 2026-01-09 14:25:04 +07:00
parent 3d34a4c5ef
commit 9aea3cc88c
3 changed files with 69 additions and 67 deletions

View file

@ -560,74 +560,70 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
"positions.posExecutive",
],
});
// // xxx
// // ดึง assignment ของ revision เดิม
// const oldAssigns = await posMasterAssignRepository.find({
// relations: ["posMaster"],
// where: {
// posMaster: {
// orgRevisionId: orgRevisionPublish?.id,
// },
// },
// });
// // สร้าง Map: ancestorDNA → assignments[]
// const assignMap = new Map<string, PosMasterAssign[]>();
// for (const a of oldAssigns) {
// const dna = a.posMaster.ancestorDNA;
// if (!assignMap.has(dna)) {
// assignMap.set(dna, []);
// }
// assignMap.get(dna)!.push(a);
// }
// const permissionProfiles = await permissionProfilesRepository.find({
// relations: ["orgRootTree"],
// where: {
// orgRootTree: {
// orgRevisionId: orgRevisionPublish?.id,
// }
// }
// });
// const permissionMap = new Map<string, PermissionProfile[]>();
// for (const p of permissionProfiles) {
// const dna = p.orgRootTree.ancestorDNA;
// if (!permissionMap.has(dna)) {
// permissionMap.set(dna, []);
// }
// permissionMap.get(dna)!.push(p);
// }
// const newRoots = await orgRootRepository.find({
// where: { orgRevisionId: orgRevisionDraft?.id },
// });
// const newRootMap = new Map(
// newRoots.map(r => [r.ancestorDNA, r.id])
// );
// ดึง posMasterAssign ของ revision เดิม xxx
const oldposMasterAssigns = await posMasterAssignRepository.find({
relations: ["posMaster"],
where: {
posMaster: {
orgRevisionId: orgRevisionPublish?.id,
},
},
});
// สร้าง Map: ancestorDNA → posMasterAssign[]
const assignMap = new Map<string, PosMasterAssign[]>();
for (const posmasterAssign of oldposMasterAssigns) {
const dna = posmasterAssign.posMaster.ancestorDNA;
if (!assignMap.has(dna)) {
assignMap.set(dna, []);
}
assignMap.get(dna)!.push(posmasterAssign);
}
// ดึง permissionProfiles ของ revision เดิม
const oldPermissionProfiles = await permissionProfilesRepository.find({
relations: ["orgRootTree"],
where: {
orgRootTree: {
orgRevisionId: orgRevisionPublish?.id,
}
}
});
// สร้าง Map: ancestorDNA → permissionProfiles[]
const permissionMap = new Map<string, PermissionProfile[]>();
for (const permissionProfile of oldPermissionProfiles) {
const dna = permissionProfile.orgRootTree.ancestorDNA;
if (!permissionMap.has(dna)) {
permissionMap.set(dna, []);
}
permissionMap.get(dna)!.push(permissionProfile);
}
const newRoots = await orgRootRepository.find({
where: { orgRevisionId: orgRevisionDraft?.id },
});
const newRootMap = new Map(
newRoots.map(r => [r.ancestorDNA, r.id])
);
const _null: any = null;
for (const item of posMaster) {
// /* ===============================
// * Clone posMasterAssign & permissionProfiles xxx
// * =============================== */
// const assigns = assignMap.get(item.ancestorDNA);
// if (assigns && assigns.length > 0) {
// const newAssigns = assigns.map(({ id, ...rest }) => ({
// ...rest, // copy ทุก field ยกเว้น id
// posMasterId: item.id, // ผูกกับ posMaster ใหม่
// }));
// await posMasterAssignRepository.save(newAssigns);
// }
// const perms = permissionMap.get(item.ancestorDNA);
// const newRootId = newRootMap.get(item.ancestorDNA);
// if (perms && perms.length > 0 && newRootId) {
// const newPerms = perms.map(({ id, orgRootTree, ...rest }) => ({
// ...rest, // profileId, isEdit, isCheck
// orgRootId: newRootId,
// }));
// await permissionProfilesRepository.save(newPerms);
// }
// Clone posMasterAssign xxx
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 ใหม่
}));
await posMasterAssignRepository.save(newAssigns);
}
// Clone permissionProfiles
const perms = permissionMap.get(item.orgRoot.ancestorDNA);
const newRootId = newRootMap.get(item.orgRoot.ancestorDNA);
if (perms && perms.length > 0 && newRootId) {
const newPerms = perms.map(({ id, ...fields }) => ({
...fields,
orgRootId: newRootId,
}));
await permissionProfilesRepository.save(newPerms);
}
if (item.next_holderId != null) {
const profile = await repoProfile.findOne({