fixed move posMaster to level
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s

This commit is contained in:
Warunee Tamkoo 2026-05-11 22:23:42 +07:00
parent 5c2b3e9689
commit 760fef5c2f

View file

@ -65,7 +65,13 @@ import {
import { orgStructureCache } from "../utils/OrgStructureCache"; import { orgStructureCache } from "../utils/OrgStructureCache";
import { OrgIdMapping, AllOrgMappings, SavePosMasterHistory } from "../interfaces/OrgMapping"; import { OrgIdMapping, AllOrgMappings, SavePosMasterHistory } from "../interfaces/OrgMapping";
import { OrgPermissionData, NodeLevel } from "../interfaces/OrgTypes"; import { OrgPermissionData, NodeLevel } from "../interfaces/OrgTypes";
import { formatPosMaster, generateLabelName, filterPosMasters, getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; import {
formatPosMaster,
generateLabelName,
filterPosMasters,
getPosMasterNo,
getOrgFullName,
} from "../utils/org-formatting";
@Route("api/v1/org") @Route("api/v1/org")
@Tags("Organization") @Tags("Organization")
@ -2531,7 +2537,7 @@ export class OrganizationController extends Controller {
* Cronjob * Cronjob
*/ */
async cronjobRevision() { async cronjobRevision() {
console.log('[CronJob] cronjobRevision START'); console.log("[CronJob] cronjobRevision START");
const startTime = Date.now(); const startTime = Date.now();
const today = new Date(); const today = new Date();
@ -2539,7 +2545,9 @@ export class OrganizationController extends Controller {
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()}`); 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")
@ -2549,11 +2557,13 @@ export class OrganizationController extends Controller {
.getOne(); .getOne();
if (!orgRevisionDraft) { if (!orgRevisionDraft) {
console.log('[CronJob] No draft revision found to publish'); 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}`); console.log(
`[CronJob] Found draft revision: ${orgRevisionDraft.id}, name: ${orgRevisionDraft.orgRevisionName}, publishDate: ${orgRevisionDraft.orgPublishDate}`,
);
// if (orgRevisionPublish) { // if (orgRevisionPublish) {
// orgRevisionPublish.orgRevisionIsDraft = false; // orgRevisionPublish.orgRevisionIsDraft = false;
@ -7836,7 +7846,11 @@ export class OrganizationController extends Controller {
profileEmp.lastUpdatedAt = new Date(); profileEmp.lastUpdatedAt = new Date();
profileEmp.isActive = false; profileEmp.isActive = false;
if (profileEmp.keycloak != null && profileEmp.keycloak != "" && profileEmp.isDelete === false) { if (
profileEmp.keycloak != null &&
profileEmp.keycloak != "" &&
profileEmp.isDelete === false
) {
const delUserKeycloak = await deleteUser(profileEmp.keycloak, token); const delUserKeycloak = await deleteUser(profileEmp.keycloak, token);
if (delUserKeycloak) { if (delUserKeycloak) {
// profileEmp.keycloak = ""; // profileEmp.keycloak = "";
@ -8134,6 +8148,8 @@ export class OrganizationController extends Controller {
if (!orgRootDraft) return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้างร่าง"); if (!orgRootDraft) return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้างร่าง");
let createdCurrentRoot = false;
// if current record not found, create new one // if current record not found, create new one
if (!orgRootCurrent) { if (!orgRootCurrent) {
// Create new current record using draft's ID // Create new current record using draft's ID
@ -8145,6 +8161,7 @@ export class OrganizationController extends Controller {
const savedRoot = await queryRunner.manager.save(OrgRoot, newCurrentRoot); const savedRoot = await queryRunner.manager.save(OrgRoot, newCurrentRoot);
orgRootCurrent = savedRoot; // Use saved record for sync orgRootCurrent = savedRoot; // Use saved record for sync
createdCurrentRoot = true;
} }
// Part 1: Differential sync of organization structure (bottom-up) // Part 1: Differential sync of organization structure (bottom-up)
@ -8170,11 +8187,7 @@ export class OrganizationController extends Controller {
mapping: OrgIdMapping; mapping: OrgIdMapping;
counts: { deleted: number; updated: number; inserted: number }; counts: { deleted: number; updated: number; inserted: number };
}; };
if ( if (createdCurrentRoot && orgRootCurrent && orgRootDraft) {
orgRootCurrent &&
orgRootDraft &&
orgRootCurrent.ancestorDNA === orgRootDraft.ancestorDNA
) {
// Manually created - set up mapping directly // Manually created - set up mapping directly
const rootMapping: OrgIdMapping = { const rootMapping: OrgIdMapping = {
byAncestorDNA: new Map([[orgRootDraft.ancestorDNA, orgRootCurrent.id]]), byAncestorDNA: new Map([[orgRootDraft.ancestorDNA, orgRootCurrent.id]]),
@ -8192,6 +8205,7 @@ export class OrganizationController extends Controller {
this.orgRootRepository, this.orgRootRepository,
drafRevisionId, drafRevisionId,
currentRevisionId, currentRevisionId,
rootDnaId,
allMappings, allMappings,
orgRootDraft?.id, orgRootDraft?.id,
orgRootCurrent?.id, orgRootCurrent?.id,
@ -8207,6 +8221,7 @@ export class OrganizationController extends Controller {
this.child1Repository, this.child1Repository,
drafRevisionId, drafRevisionId,
currentRevisionId, currentRevisionId,
rootDnaId,
allMappings, allMappings,
orgRootDraft?.id, orgRootDraft?.id,
orgRootCurrent?.id, orgRootCurrent?.id,
@ -8221,6 +8236,7 @@ export class OrganizationController extends Controller {
this.child2Repository, this.child2Repository,
drafRevisionId, drafRevisionId,
currentRevisionId, currentRevisionId,
rootDnaId,
allMappings, allMappings,
orgRootDraft?.id, orgRootDraft?.id,
orgRootCurrent?.id, orgRootCurrent?.id,
@ -8235,6 +8251,7 @@ export class OrganizationController extends Controller {
this.child3Repository, this.child3Repository,
drafRevisionId, drafRevisionId,
currentRevisionId, currentRevisionId,
rootDnaId,
allMappings, allMappings,
orgRootDraft?.id, orgRootDraft?.id,
orgRootCurrent?.id, orgRootCurrent?.id,
@ -8249,6 +8266,7 @@ export class OrganizationController extends Controller {
this.child4Repository, this.child4Repository,
drafRevisionId, drafRevisionId,
currentRevisionId, currentRevisionId,
rootDnaId,
allMappings, allMappings,
orgRootDraft?.id, orgRootDraft?.id,
orgRootCurrent?.id, orgRootCurrent?.id,
@ -8315,33 +8333,33 @@ export class OrganizationController extends Controller {
posMasterDnaId: pos.ancestorDNA, posMasterDnaId: pos.ancestorDNA,
profileId: null, profileId: null,
pm: { pm: {
prefix: null, prefix: null,
firstName: null, firstName: null,
lastName: null, lastName: null,
position: null, position: null,
posType: null, posType: null,
posLevel: null, posLevel: null,
posExecutive: null, posExecutive: null,
profileId: null, profileId: null,
shortName: pos shortName: pos
? [ ? [
pos.orgChild4?.orgChild4ShortName, pos.orgChild4?.orgChild4ShortName,
pos.orgChild3?.orgChild3ShortName, pos.orgChild3?.orgChild3ShortName,
pos.orgChild2?.orgChild2ShortName, pos.orgChild2?.orgChild2ShortName,
pos.orgChild1?.orgChild1ShortName, pos.orgChild1?.orgChild1ShortName,
pos.orgRoot?.orgRootShortName, pos.orgRoot?.orgRootShortName,
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ?? ].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
null null
: null, : null,
posMasterNoPrefix: pos.posMasterNoPrefix ?? null, posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null, posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
posMasterNoSuffix: pos.posMasterNoSuffix ?? null, posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null, rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null, child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null, child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null, child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null, child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
} as SavePosMasterHistory, } as SavePosMasterHistory,
})); }));
await BatchSavePosMasterHistoryOfficer(queryRunner, deleteHistoryOps); await BatchSavePosMasterHistoryOfficer(queryRunner, deleteHistoryOps);
} }
@ -8390,33 +8408,33 @@ export class OrganizationController extends Controller {
posMasterDnaId: pos.ancestorDNA, posMasterDnaId: pos.ancestorDNA,
profileId: null, profileId: null,
pm: { pm: {
prefix: null, prefix: null,
firstName: null, firstName: null,
lastName: null, lastName: null,
position: null, position: null,
posType: null, posType: null,
posLevel: null, posLevel: null,
posExecutive: null, posExecutive: null,
profileId: null, profileId: null,
shortName: pos shortName: pos
? [ ? [
pos.orgChild4?.orgChild4ShortName, pos.orgChild4?.orgChild4ShortName,
pos.orgChild3?.orgChild3ShortName, pos.orgChild3?.orgChild3ShortName,
pos.orgChild2?.orgChild2ShortName, pos.orgChild2?.orgChild2ShortName,
pos.orgChild1?.orgChild1ShortName, pos.orgChild1?.orgChild1ShortName,
pos.orgRoot?.orgRootShortName, pos.orgRoot?.orgRootShortName,
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ?? ].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
null null
: null, : null,
posMasterNoPrefix: pos.posMasterNoPrefix ?? null, posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null, posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
posMasterNoSuffix: pos.posMasterNoSuffix ?? null, posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null, rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null, child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null, child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null, child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null, child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
} as SavePosMasterHistory, } as SavePosMasterHistory,
})); }));
await BatchSavePosMasterHistoryOfficer(queryRunner, historyOps); await BatchSavePosMasterHistoryOfficer(queryRunner, historyOps);
@ -8464,39 +8482,39 @@ export class OrganizationController extends Controller {
posMasterDnaId: pos.ancestorDNA, posMasterDnaId: pos.ancestorDNA,
profileId: null, profileId: null,
pm: { pm: {
prefix: null, prefix: null,
firstName: null, firstName: null,
lastName: null, lastName: null,
position: null, position: null,
posType: null, posType: null,
posLevel: null, posLevel: null,
posExecutive: null, posExecutive: null,
profileId: null, profileId: null,
shortName: pos shortName: pos
? [ ? [
pos.orgChild4?.orgChild4ShortName, pos.orgChild4?.orgChild4ShortName,
pos.orgChild3?.orgChild3ShortName, pos.orgChild3?.orgChild3ShortName,
pos.orgChild2?.orgChild2ShortName, pos.orgChild2?.orgChild2ShortName,
pos.orgChild1?.orgChild1ShortName, pos.orgChild1?.orgChild1ShortName,
pos.orgRoot?.orgRootShortName, pos.orgRoot?.orgRootShortName,
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ?? ].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
null null
: null, : null,
posMasterNoPrefix: pos.posMasterNoPrefix ?? null, posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null, posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
posMasterNoSuffix: pos.posMasterNoSuffix ?? null, posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null, rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null, child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null, child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null, child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null, child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
} as SavePosMasterHistory, } as SavePosMasterHistory,
})); }));
await BatchSavePosMasterHistoryOfficer(queryRunner, deleteHistoryOps); await BatchSavePosMasterHistoryOfficer(queryRunner, deleteHistoryOps);
} }
// 2.4 Process draft positions (UPDATE or INSERT) // 2.4 Process draft positions (UPDATE or INSERT)
const toUpdate: PosMaster[] = []; const toUpdate: Partial<PosMaster>[] = [];
const toInsert: any[] = []; const toInsert: any[] = [];
// Track draft PosMaster ID to current PosMaster ID mapping for position sync // Track draft PosMaster ID to current PosMaster ID mapping for position sync
@ -8504,7 +8522,7 @@ export class OrganizationController extends Controller {
const posMasterMapping: Map<string, [string, string | null | undefined]> = new Map(); const posMasterMapping: Map<string, [string, string | null | undefined]> = new Map();
// Collect positions where next_holderId is null for batch history saving // Collect positions where next_holderId is null for batch history saving
const nullHolderPosIds: string[] = []; const nullHolderDraftPosIds: string[] = [];
for (const draftPos of posMasterDraft) { for (const draftPos of posMasterDraft) {
const current = currentByDNA.get(draftPos.ancestorDNA); const current = currentByDNA.get(draftPos.ancestorDNA);
@ -8518,7 +8536,9 @@ export class OrganizationController extends Controller {
if (current) { if (current) {
// UPDATE existing position // UPDATE existing position
Object.assign(current, { toUpdate.push({
id: current.id,
ancestorDNA: current.ancestorDNA,
createdAt: draftPos.createdAt, createdAt: draftPos.createdAt,
createdUserId: draftPos.createdUserId, createdUserId: draftPos.createdUserId,
createdFullName: draftPos.createdFullName, createdFullName: draftPos.createdFullName,
@ -8529,12 +8549,14 @@ export class OrganizationController extends Controller {
posMasterNoSuffix: draftPos.posMasterNoSuffix, posMasterNoSuffix: draftPos.posMasterNoSuffix,
posMasterNo: draftPos.posMasterNo, posMasterNo: draftPos.posMasterNo,
posMasterOrder: draftPos.posMasterOrder, posMasterOrder: draftPos.posMasterOrder,
orgRevisionId: currentRevisionId,
orgRootId, orgRootId,
orgChild1Id, orgChild1Id,
orgChild2Id, orgChild2Id,
orgChild3Id, orgChild3Id,
orgChild4Id, orgChild4Id,
current_holderId: draftPos.next_holderId, current_holderId: draftPos.next_holderId,
next_holderId: draftPos.next_holderId,
isSit: draftPos.isSit, isSit: draftPos.isSit,
reason: draftPos.reason, reason: draftPos.reason,
isDirector: draftPos.isDirector, isDirector: draftPos.isDirector,
@ -8544,10 +8566,9 @@ export class OrganizationController extends Controller {
isCondition: draftPos.isCondition, isCondition: draftPos.isCondition,
conditionReason: draftPos.conditionReason, conditionReason: draftPos.conditionReason,
}); });
toUpdate.push(current);
if (draftPos.next_holderId === null) { if (draftPos.next_holderId === null) {
nullHolderPosIds.push(draftPos.id); nullHolderDraftPosIds.push(draftPos.id);
} }
// Track mapping for position sync // Track mapping for position sync
@ -8573,7 +8594,7 @@ export class OrganizationController extends Controller {
// Batch save updates and inserts // Batch save updates and inserts
if (toUpdate.length > 0) { if (toUpdate.length > 0) {
await queryRunner.manager.save(toUpdate); await queryRunner.manager.save(PosMaster, toUpdate);
} }
if (toInsert.length > 0) { if (toInsert.length > 0) {
const saved = await queryRunner.manager.save(toInsert); const saved = await queryRunner.manager.save(toInsert);
@ -8592,17 +8613,22 @@ export class OrganizationController extends Controller {
// 2.4.1 Save PosMasterHistory for positions where next_holderId was cleared (null) // 2.4.1 Save PosMasterHistory for positions where next_holderId was cleared (null)
// These need org relations to populate shortName, rootDnaId, child*DnaId fields // These need org relations to populate shortName, rootDnaId, child*DnaId fields
if (nullHolderPosIds.length > 0) { if (nullHolderDraftPosIds.length > 0) {
const nullHolderCurrentPosIds = nullHolderDraftPosIds
.map((draftPosId) => posMasterMapping.get(draftPosId)?.[0] ?? null)
.filter((currentPosId): currentPosId is string => currentPosId !== null);
const nullHolderPosMasters = await queryRunner.manager.find(PosMaster, { const nullHolderPosMasters = await queryRunner.manager.find(PosMaster, {
where: { id: In(nullHolderPosIds) }, where: { id: In(nullHolderCurrentPosIds) },
relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"],
}); });
const nullHolderMap = new Map(nullHolderPosMasters.map((pm) => [pm.id, pm as any])); const nullHolderMap = new Map(nullHolderPosMasters.map((pm) => [pm.id, pm as any]));
const nullHolderHistoryOps = posMasterDraft const nullHolderHistoryOps = posMasterDraft
.filter((d) => nullHolderPosIds.includes(d.id)) .filter((d) => nullHolderDraftPosIds.includes(d.id))
.map((draftPos) => { .map((draftPos) => {
const pmWithRelations = nullHolderMap.get(draftPos.id); const currentPosId = posMasterMapping.get(draftPos.id)?.[0] ?? null;
const pmWithRelations = currentPosId ? nullHolderMap.get(currentPosId) : null;
return { return {
posMasterDnaId: draftPos.ancestorDNA, posMasterDnaId: draftPos.ancestorDNA,
profileId: null as string | null, profileId: null as string | null,
@ -8622,8 +8648,9 @@ export class OrganizationController extends Controller {
pmWithRelations.orgChild2?.orgChild2ShortName, pmWithRelations.orgChild2?.orgChild2ShortName,
pmWithRelations.orgChild1?.orgChild1ShortName, pmWithRelations.orgChild1?.orgChild1ShortName,
pmWithRelations.orgRoot?.orgRootShortName, pmWithRelations.orgRoot?.orgRootShortName,
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ?? ].find(
null (s: string | undefined) => typeof s === "string" && s.trim().length > 0,
) ?? null
: null, : null,
posMasterNoPrefix: draftPos.posMasterNoPrefix ?? null, posMasterNoPrefix: draftPos.posMasterNoPrefix ?? null,
posMasterNo: draftPos.posMasterNo != null ? String(draftPos.posMasterNo) : null, posMasterNo: draftPos.posMasterNo != null ? String(draftPos.posMasterNo) : null,
@ -8688,6 +8715,99 @@ export class OrganizationController extends Controller {
return mapping.byDraftId.get(draftId) ?? null; return mapping.byDraftId.get(draftId) ?? null;
} }
private resolveRequiredOrgId(
draftId: string | null | undefined,
mapping: OrgIdMapping,
fieldName: string,
entityName: string,
entityDna: string,
rootDnaId: string,
): string | null {
if (!draftId) return null;
const mappedId = mapping.byDraftId.get(draftId) ?? null;
if (mappedId) return mappedId;
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
`ไม่สามารถ map ${fieldName} ของ ${entityName} (${entityDna}) ใน rootDnaId ${rootDnaId} ได้`,
);
}
private getMappedParentIds(
entityClass: any,
draftNode: any,
parentMappings: AllOrgMappings | undefined,
rootDnaId: string,
): Partial<{
orgRootId: string | null;
orgChild1Id: string | null;
orgChild2Id: string | null;
orgChild3Id: string | null;
}> {
if (entityClass === OrgRoot) {
return {};
}
if (!parentMappings) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
`ไม่พบข้อมูล mapping ของโครงสร้างสำหรับ rootDnaId ${rootDnaId}`,
);
}
const mappedParentIds: Partial<{
orgRootId: string | null;
orgChild1Id: string | null;
orgChild2Id: string | null;
orgChild3Id: string | null;
}> = {};
mappedParentIds.orgRootId = this.resolveRequiredOrgId(
draftNode.orgRootId,
parentMappings.orgRoot,
"orgRootId",
entityClass.name,
draftNode.ancestorDNA,
rootDnaId,
);
if (entityClass === OrgChild2 || entityClass === OrgChild3 || entityClass === OrgChild4) {
mappedParentIds.orgChild1Id = this.resolveRequiredOrgId(
draftNode.orgChild1Id,
parentMappings.orgChild1,
"orgChild1Id",
entityClass.name,
draftNode.ancestorDNA,
rootDnaId,
);
}
if (entityClass === OrgChild3 || entityClass === OrgChild4) {
mappedParentIds.orgChild2Id = this.resolveRequiredOrgId(
draftNode.orgChild2Id,
parentMappings.orgChild2,
"orgChild2Id",
entityClass.name,
draftNode.ancestorDNA,
rootDnaId,
);
}
if (entityClass === OrgChild4) {
mappedParentIds.orgChild3Id = this.resolveRequiredOrgId(
draftNode.orgChild3Id,
parentMappings.orgChild3,
"orgChild3Id",
entityClass.name,
draftNode.ancestorDNA,
rootDnaId,
);
}
return mappedParentIds;
}
/** /**
* Helper function: Cascade delete positions before deleting org node * Helper function: Cascade delete positions before deleting org node
*/ */
@ -8726,6 +8846,7 @@ export class OrganizationController extends Controller {
repository: any, repository: any,
draftRevisionId: string, draftRevisionId: string,
currentRevisionId: string, currentRevisionId: string,
rootDnaId: string,
parentMappings?: AllOrgMappings, parentMappings?: AllOrgMappings,
draftOrgRootId?: string, draftOrgRootId?: string,
currentOrgRootId?: string, currentOrgRootId?: string,
@ -8798,53 +8919,9 @@ export class OrganizationController extends Controller {
...draft, ...draft,
id: current.id, id: current.id,
orgRevisionId: currentRevisionId, orgRevisionId: currentRevisionId,
...this.getMappedParentIds(entityClass, draft, parentMappings, rootDnaId),
}; };
// Map parent IDs based on entity level
if (entityClass === OrgChild1 && draft.orgRootId && parentMappings) {
updateData.orgRootId =
parentMappings.orgRoot.byDraftId.get(draft.orgRootId) ?? draft.orgRootId;
} else if (entityClass === OrgChild2) {
if (draft.orgRootId && parentMappings) {
updateData.orgRootId =
parentMappings.orgRoot.byDraftId.get(draft.orgRootId) ?? draft.orgRootId;
}
if (draft.orgChild1Id && parentMappings) {
updateData.orgChild1Id =
parentMappings.orgChild1.byDraftId.get(draft.orgChild1Id) ?? draft.orgChild1Id;
}
} else if (entityClass === OrgChild3) {
if (draft.orgRootId && parentMappings) {
updateData.orgRootId =
parentMappings.orgRoot.byDraftId.get(draft.orgRootId) ?? draft.orgRootId;
}
if (draft.orgChild1Id && parentMappings) {
updateData.orgChild1Id =
parentMappings.orgChild1.byDraftId.get(draft.orgChild1Id) ?? draft.orgChild1Id;
}
if (draft.orgChild2Id && parentMappings) {
updateData.orgChild2Id =
parentMappings.orgChild2.byDraftId.get(draft.orgChild2Id) ?? draft.orgChild2Id;
}
} else if (entityClass === OrgChild4) {
if (draft.orgRootId && parentMappings) {
updateData.orgRootId =
parentMappings.orgRoot.byDraftId.get(draft.orgRootId) ?? draft.orgRootId;
}
if (draft.orgChild1Id && parentMappings) {
updateData.orgChild1Id =
parentMappings.orgChild1.byDraftId.get(draft.orgChild1Id) ?? draft.orgChild1Id;
}
if (draft.orgChild2Id && parentMappings) {
updateData.orgChild2Id =
parentMappings.orgChild2.byDraftId.get(draft.orgChild2Id) ?? draft.orgChild2Id;
}
if (draft.orgChild3Id && parentMappings) {
updateData.orgChild3Id =
parentMappings.orgChild3.byDraftId.get(draft.orgChild3Id) ?? draft.orgChild3Id;
}
}
await queryRunner.manager.update(entityClass, current.id, updateData); await queryRunner.manager.update(entityClass, current.id, updateData);
mapping.byAncestorDNA.set(draft.ancestorDNA, current.id); mapping.byAncestorDNA.set(draft.ancestorDNA, current.id);
@ -8858,77 +8935,9 @@ export class OrganizationController extends Controller {
...draft, ...draft,
id: undefined, id: undefined,
orgRevisionId: currentRevisionId, orgRevisionId: currentRevisionId,
...this.getMappedParentIds(entityClass, draft, parentMappings, rootDnaId),
}); });
// Map parent IDs based on entity level
if (entityClass === OrgChild1 && draft.orgRootId) {
if (draft.orgRootId === draftOrgRootId) {
newNode.orgRootId = currentOrgRootId;
} else if (parentMappings) {
newNode.orgRootId = parentMappings.orgRoot.byDraftId.get(draft.orgRootId);
}
} else if (entityClass === OrgChild2) {
if (draft.orgRootId) {
if (draft.orgRootId === draftOrgRootId) {
newNode.orgRootId = currentOrgRootId;
} else if (parentMappings) {
newNode.orgRootId = parentMappings.orgRoot.byDraftId.get(draft.orgRootId);
}
}
if (draft.orgChild1Id && parentMappings) {
const mappedChild1Id = parentMappings.orgChild1.byDraftId.get(draft.orgChild1Id);
if (mappedChild1Id) {
newNode.orgChild1Id = mappedChild1Id;
}
}
} else if (entityClass === OrgChild3) {
if (draft.orgRootId) {
if (draft.orgRootId === draftOrgRootId) {
newNode.orgRootId = currentOrgRootId;
} else if (parentMappings) {
newNode.orgRootId = parentMappings.orgRoot.byDraftId.get(draft.orgRootId);
}
}
if (draft.orgChild1Id && parentMappings) {
const mappedChild1Id = parentMappings.orgChild1.byDraftId.get(draft.orgChild1Id);
if (mappedChild1Id) {
newNode.orgChild1Id = mappedChild1Id;
}
}
if (draft.orgChild2Id && parentMappings) {
const mappedChild2Id = parentMappings.orgChild2.byDraftId.get(draft.orgChild2Id);
if (mappedChild2Id) {
newNode.orgChild2Id = mappedChild2Id;
}
}
} else if (entityClass === OrgChild4) {
if (draft.orgRootId) {
if (draft.orgRootId === draftOrgRootId) {
newNode.orgRootId = currentOrgRootId;
} else if (parentMappings) {
newNode.orgRootId = parentMappings.orgRoot.byDraftId.get(draft.orgRootId);
}
}
if (draft.orgChild1Id && parentMappings) {
const mappedChild1Id = parentMappings.orgChild1.byDraftId.get(draft.orgChild1Id);
if (mappedChild1Id) {
newNode.orgChild1Id = mappedChild1Id;
}
}
if (draft.orgChild2Id && parentMappings) {
const mappedChild2Id = parentMappings.orgChild2.byDraftId.get(draft.orgChild2Id);
if (mappedChild2Id) {
newNode.orgChild2Id = mappedChild2Id;
}
}
if (draft.orgChild3Id && parentMappings) {
const mappedChild3Id = parentMappings.orgChild3.byDraftId.get(draft.orgChild3Id);
if (mappedChild3Id) {
newNode.orgChild3Id = mappedChild3Id;
}
}
}
const saved = await queryRunner.manager.save(newNode); const saved = await queryRunner.manager.save(newNode);
mapping.byAncestorDNA.set(draft.ancestorDNA, saved.id); mapping.byAncestorDNA.set(draft.ancestorDNA, saved.id);
@ -9100,8 +9109,10 @@ export class OrganizationController extends Controller {
if (nextHolderId != null && draftPos.positionIsSelected) { if (nextHolderId != null && draftPos.positionIsSelected) {
const _null: any = null; const _null: any = null;
profileUpdates.set(nextHolderId, { profileUpdates.set(nextHolderId, {
posMasterNo: draftPosMaster ? (getPosMasterNo(draftPosMaster as PosMaster) ?? _null) : _null, posMasterNo: draftPosMaster
org: draftPosMaster ? (getOrgFullName(draftPosMaster as PosMaster) ?? _null) : _null, ? getPosMasterNo(draftPosMaster as PosMaster) ?? _null
: _null,
org: draftPosMaster ? getOrgFullName(draftPosMaster as PosMaster) ?? _null : _null,
}); });
} }
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
@ -9169,10 +9180,10 @@ export class OrganizationController extends Controller {
prefix: null, prefix: null,
firstName: null, firstName: null,
lastName: null, lastName: null,
position: null, position: null,
posType: null, posType: null,
posLevel: null, posLevel: null,
posExecutive: null, posExecutive: null,
profileId: null, profileId: null,
rootDnaId: posMaster?.orgRoot?.ancestorDNA ?? null, rootDnaId: posMaster?.orgRoot?.ancestorDNA ?? null,
child1DnaId: posMaster?.orgChild1?.ancestorDNA ?? null, child1DnaId: posMaster?.orgChild1?.ancestorDNA ?? null,