fixed move posMaster to level
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s
This commit is contained in:
parent
5c2b3e9689
commit
760fef5c2f
1 changed files with 235 additions and 224 deletions
|
|
@ -65,7 +65,13 @@ import {
|
|||
import { orgStructureCache } from "../utils/OrgStructureCache";
|
||||
import { OrgIdMapping, AllOrgMappings, SavePosMasterHistory } from "../interfaces/OrgMapping";
|
||||
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")
|
||||
@Tags("Organization")
|
||||
|
|
@ -2531,7 +2537,7 @@ export class OrganizationController extends Controller {
|
|||
* Cronjob
|
||||
*/
|
||||
async cronjobRevision() {
|
||||
console.log('[CronJob] cronjobRevision START');
|
||||
console.log("[CronJob] cronjobRevision START");
|
||||
const startTime = Date.now();
|
||||
|
||||
const today = new Date();
|
||||
|
|
@ -2539,7 +2545,9 @@ export class OrganizationController extends Controller {
|
|||
const tomorrow = new Date(today);
|
||||
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
|
||||
.createQueryBuilder("orgRevision")
|
||||
|
|
@ -2549,11 +2557,13 @@ export class OrganizationController extends Controller {
|
|||
.getOne();
|
||||
|
||||
if (!orgRevisionDraft) {
|
||||
console.log('[CronJob] No draft revision found to publish');
|
||||
console.log("[CronJob] No draft revision found to publish");
|
||||
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) {
|
||||
// orgRevisionPublish.orgRevisionIsDraft = false;
|
||||
|
|
@ -7836,7 +7846,11 @@ export class OrganizationController extends Controller {
|
|||
profileEmp.lastUpdatedAt = new Date();
|
||||
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);
|
||||
if (delUserKeycloak) {
|
||||
// profileEmp.keycloak = "";
|
||||
|
|
@ -8134,6 +8148,8 @@ export class OrganizationController extends Controller {
|
|||
|
||||
if (!orgRootDraft) return new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้างร่าง");
|
||||
|
||||
let createdCurrentRoot = false;
|
||||
|
||||
// if current record not found, create new one
|
||||
if (!orgRootCurrent) {
|
||||
// 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);
|
||||
orgRootCurrent = savedRoot; // Use saved record for sync
|
||||
createdCurrentRoot = true;
|
||||
}
|
||||
|
||||
// Part 1: Differential sync of organization structure (bottom-up)
|
||||
|
|
@ -8170,11 +8187,7 @@ export class OrganizationController extends Controller {
|
|||
mapping: OrgIdMapping;
|
||||
counts: { deleted: number; updated: number; inserted: number };
|
||||
};
|
||||
if (
|
||||
orgRootCurrent &&
|
||||
orgRootDraft &&
|
||||
orgRootCurrent.ancestorDNA === orgRootDraft.ancestorDNA
|
||||
) {
|
||||
if (createdCurrentRoot && orgRootCurrent && orgRootDraft) {
|
||||
// Manually created - set up mapping directly
|
||||
const rootMapping: OrgIdMapping = {
|
||||
byAncestorDNA: new Map([[orgRootDraft.ancestorDNA, orgRootCurrent.id]]),
|
||||
|
|
@ -8192,6 +8205,7 @@ export class OrganizationController extends Controller {
|
|||
this.orgRootRepository,
|
||||
drafRevisionId,
|
||||
currentRevisionId,
|
||||
rootDnaId,
|
||||
allMappings,
|
||||
orgRootDraft?.id,
|
||||
orgRootCurrent?.id,
|
||||
|
|
@ -8207,6 +8221,7 @@ export class OrganizationController extends Controller {
|
|||
this.child1Repository,
|
||||
drafRevisionId,
|
||||
currentRevisionId,
|
||||
rootDnaId,
|
||||
allMappings,
|
||||
orgRootDraft?.id,
|
||||
orgRootCurrent?.id,
|
||||
|
|
@ -8221,6 +8236,7 @@ export class OrganizationController extends Controller {
|
|||
this.child2Repository,
|
||||
drafRevisionId,
|
||||
currentRevisionId,
|
||||
rootDnaId,
|
||||
allMappings,
|
||||
orgRootDraft?.id,
|
||||
orgRootCurrent?.id,
|
||||
|
|
@ -8235,6 +8251,7 @@ export class OrganizationController extends Controller {
|
|||
this.child3Repository,
|
||||
drafRevisionId,
|
||||
currentRevisionId,
|
||||
rootDnaId,
|
||||
allMappings,
|
||||
orgRootDraft?.id,
|
||||
orgRootCurrent?.id,
|
||||
|
|
@ -8249,6 +8266,7 @@ export class OrganizationController extends Controller {
|
|||
this.child4Repository,
|
||||
drafRevisionId,
|
||||
currentRevisionId,
|
||||
rootDnaId,
|
||||
allMappings,
|
||||
orgRootDraft?.id,
|
||||
orgRootCurrent?.id,
|
||||
|
|
@ -8315,33 +8333,33 @@ export class OrganizationController extends Controller {
|
|||
posMasterDnaId: pos.ancestorDNA,
|
||||
profileId: null,
|
||||
pm: {
|
||||
prefix: null,
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
profileId: null,
|
||||
shortName: pos
|
||||
? [
|
||||
pos.orgChild4?.orgChild4ShortName,
|
||||
pos.orgChild3?.orgChild3ShortName,
|
||||
pos.orgChild2?.orgChild2ShortName,
|
||||
pos.orgChild1?.orgChild1ShortName,
|
||||
pos.orgRoot?.orgRootShortName,
|
||||
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
|
||||
null
|
||||
: null,
|
||||
posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
|
||||
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
|
||||
posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
|
||||
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
|
||||
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
|
||||
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
|
||||
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
|
||||
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
|
||||
} as SavePosMasterHistory,
|
||||
prefix: null,
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
profileId: null,
|
||||
shortName: pos
|
||||
? [
|
||||
pos.orgChild4?.orgChild4ShortName,
|
||||
pos.orgChild3?.orgChild3ShortName,
|
||||
pos.orgChild2?.orgChild2ShortName,
|
||||
pos.orgChild1?.orgChild1ShortName,
|
||||
pos.orgRoot?.orgRootShortName,
|
||||
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
|
||||
null
|
||||
: null,
|
||||
posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
|
||||
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
|
||||
posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
|
||||
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
|
||||
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
|
||||
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
|
||||
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
|
||||
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
|
||||
} as SavePosMasterHistory,
|
||||
}));
|
||||
await BatchSavePosMasterHistoryOfficer(queryRunner, deleteHistoryOps);
|
||||
}
|
||||
|
|
@ -8390,33 +8408,33 @@ export class OrganizationController extends Controller {
|
|||
posMasterDnaId: pos.ancestorDNA,
|
||||
profileId: null,
|
||||
pm: {
|
||||
prefix: null,
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
profileId: null,
|
||||
shortName: pos
|
||||
? [
|
||||
pos.orgChild4?.orgChild4ShortName,
|
||||
pos.orgChild3?.orgChild3ShortName,
|
||||
pos.orgChild2?.orgChild2ShortName,
|
||||
pos.orgChild1?.orgChild1ShortName,
|
||||
pos.orgRoot?.orgRootShortName,
|
||||
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
|
||||
null
|
||||
: null,
|
||||
posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
|
||||
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
|
||||
posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
|
||||
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
|
||||
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
|
||||
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
|
||||
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
|
||||
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
|
||||
} as SavePosMasterHistory,
|
||||
prefix: null,
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
profileId: null,
|
||||
shortName: pos
|
||||
? [
|
||||
pos.orgChild4?.orgChild4ShortName,
|
||||
pos.orgChild3?.orgChild3ShortName,
|
||||
pos.orgChild2?.orgChild2ShortName,
|
||||
pos.orgChild1?.orgChild1ShortName,
|
||||
pos.orgRoot?.orgRootShortName,
|
||||
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
|
||||
null
|
||||
: null,
|
||||
posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
|
||||
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
|
||||
posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
|
||||
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
|
||||
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
|
||||
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
|
||||
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
|
||||
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
|
||||
} as SavePosMasterHistory,
|
||||
}));
|
||||
await BatchSavePosMasterHistoryOfficer(queryRunner, historyOps);
|
||||
|
||||
|
|
@ -8464,39 +8482,39 @@ export class OrganizationController extends Controller {
|
|||
posMasterDnaId: pos.ancestorDNA,
|
||||
profileId: null,
|
||||
pm: {
|
||||
prefix: null,
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
profileId: null,
|
||||
shortName: pos
|
||||
? [
|
||||
pos.orgChild4?.orgChild4ShortName,
|
||||
pos.orgChild3?.orgChild3ShortName,
|
||||
pos.orgChild2?.orgChild2ShortName,
|
||||
pos.orgChild1?.orgChild1ShortName,
|
||||
pos.orgRoot?.orgRootShortName,
|
||||
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
|
||||
null
|
||||
: null,
|
||||
posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
|
||||
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
|
||||
posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
|
||||
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
|
||||
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
|
||||
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
|
||||
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
|
||||
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
|
||||
} as SavePosMasterHistory,
|
||||
prefix: null,
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
profileId: null,
|
||||
shortName: pos
|
||||
? [
|
||||
pos.orgChild4?.orgChild4ShortName,
|
||||
pos.orgChild3?.orgChild3ShortName,
|
||||
pos.orgChild2?.orgChild2ShortName,
|
||||
pos.orgChild1?.orgChild1ShortName,
|
||||
pos.orgRoot?.orgRootShortName,
|
||||
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
|
||||
null
|
||||
: null,
|
||||
posMasterNoPrefix: pos.posMasterNoPrefix ?? null,
|
||||
posMasterNo: pos.posMasterNo != null ? String(pos.posMasterNo) : null,
|
||||
posMasterNoSuffix: pos.posMasterNoSuffix ?? null,
|
||||
rootDnaId: pos?.orgRoot?.ancestorDNA ?? null,
|
||||
child1DnaId: pos?.orgChild1?.ancestorDNA ?? null,
|
||||
child2DnaId: pos?.orgChild2?.ancestorDNA ?? null,
|
||||
child3DnaId: pos?.orgChild3?.ancestorDNA ?? null,
|
||||
child4DnaId: pos?.orgChild4?.ancestorDNA ?? null,
|
||||
} as SavePosMasterHistory,
|
||||
}));
|
||||
await BatchSavePosMasterHistoryOfficer(queryRunner, deleteHistoryOps);
|
||||
}
|
||||
|
||||
// 2.4 Process draft positions (UPDATE or INSERT)
|
||||
const toUpdate: PosMaster[] = [];
|
||||
const toUpdate: Partial<PosMaster>[] = [];
|
||||
const toInsert: any[] = [];
|
||||
|
||||
// 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();
|
||||
|
||||
// Collect positions where next_holderId is null for batch history saving
|
||||
const nullHolderPosIds: string[] = [];
|
||||
const nullHolderDraftPosIds: string[] = [];
|
||||
|
||||
for (const draftPos of posMasterDraft) {
|
||||
const current = currentByDNA.get(draftPos.ancestorDNA);
|
||||
|
|
@ -8518,7 +8536,9 @@ export class OrganizationController extends Controller {
|
|||
|
||||
if (current) {
|
||||
// UPDATE existing position
|
||||
Object.assign(current, {
|
||||
toUpdate.push({
|
||||
id: current.id,
|
||||
ancestorDNA: current.ancestorDNA,
|
||||
createdAt: draftPos.createdAt,
|
||||
createdUserId: draftPos.createdUserId,
|
||||
createdFullName: draftPos.createdFullName,
|
||||
|
|
@ -8529,12 +8549,14 @@ export class OrganizationController extends Controller {
|
|||
posMasterNoSuffix: draftPos.posMasterNoSuffix,
|
||||
posMasterNo: draftPos.posMasterNo,
|
||||
posMasterOrder: draftPos.posMasterOrder,
|
||||
orgRevisionId: currentRevisionId,
|
||||
orgRootId,
|
||||
orgChild1Id,
|
||||
orgChild2Id,
|
||||
orgChild3Id,
|
||||
orgChild4Id,
|
||||
current_holderId: draftPos.next_holderId,
|
||||
next_holderId: draftPos.next_holderId,
|
||||
isSit: draftPos.isSit,
|
||||
reason: draftPos.reason,
|
||||
isDirector: draftPos.isDirector,
|
||||
|
|
@ -8544,10 +8566,9 @@ export class OrganizationController extends Controller {
|
|||
isCondition: draftPos.isCondition,
|
||||
conditionReason: draftPos.conditionReason,
|
||||
});
|
||||
toUpdate.push(current);
|
||||
|
||||
if (draftPos.next_holderId === null) {
|
||||
nullHolderPosIds.push(draftPos.id);
|
||||
nullHolderDraftPosIds.push(draftPos.id);
|
||||
}
|
||||
|
||||
// Track mapping for position sync
|
||||
|
|
@ -8573,7 +8594,7 @@ export class OrganizationController extends Controller {
|
|||
|
||||
// Batch save updates and inserts
|
||||
if (toUpdate.length > 0) {
|
||||
await queryRunner.manager.save(toUpdate);
|
||||
await queryRunner.manager.save(PosMaster, toUpdate);
|
||||
}
|
||||
if (toInsert.length > 0) {
|
||||
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)
|
||||
// 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, {
|
||||
where: { id: In(nullHolderPosIds) },
|
||||
where: { id: In(nullHolderCurrentPosIds) },
|
||||
relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"],
|
||||
});
|
||||
const nullHolderMap = new Map(nullHolderPosMasters.map((pm) => [pm.id, pm as any]));
|
||||
|
||||
const nullHolderHistoryOps = posMasterDraft
|
||||
.filter((d) => nullHolderPosIds.includes(d.id))
|
||||
.filter((d) => nullHolderDraftPosIds.includes(d.id))
|
||||
.map((draftPos) => {
|
||||
const pmWithRelations = nullHolderMap.get(draftPos.id);
|
||||
const currentPosId = posMasterMapping.get(draftPos.id)?.[0] ?? null;
|
||||
const pmWithRelations = currentPosId ? nullHolderMap.get(currentPosId) : null;
|
||||
return {
|
||||
posMasterDnaId: draftPos.ancestorDNA,
|
||||
profileId: null as string | null,
|
||||
|
|
@ -8622,8 +8648,9 @@ export class OrganizationController extends Controller {
|
|||
pmWithRelations.orgChild2?.orgChild2ShortName,
|
||||
pmWithRelations.orgChild1?.orgChild1ShortName,
|
||||
pmWithRelations.orgRoot?.orgRootShortName,
|
||||
].find((s: string | undefined) => typeof s === "string" && s.trim().length > 0) ??
|
||||
null
|
||||
].find(
|
||||
(s: string | undefined) => typeof s === "string" && s.trim().length > 0,
|
||||
) ?? null
|
||||
: null,
|
||||
posMasterNoPrefix: draftPos.posMasterNoPrefix ?? null,
|
||||
posMasterNo: draftPos.posMasterNo != null ? String(draftPos.posMasterNo) : null,
|
||||
|
|
@ -8688,6 +8715,99 @@ export class OrganizationController extends Controller {
|
|||
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
|
||||
*/
|
||||
|
|
@ -8726,6 +8846,7 @@ export class OrganizationController extends Controller {
|
|||
repository: any,
|
||||
draftRevisionId: string,
|
||||
currentRevisionId: string,
|
||||
rootDnaId: string,
|
||||
parentMappings?: AllOrgMappings,
|
||||
draftOrgRootId?: string,
|
||||
currentOrgRootId?: string,
|
||||
|
|
@ -8798,53 +8919,9 @@ export class OrganizationController extends Controller {
|
|||
...draft,
|
||||
id: current.id,
|
||||
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);
|
||||
|
||||
mapping.byAncestorDNA.set(draft.ancestorDNA, current.id);
|
||||
|
|
@ -8858,77 +8935,9 @@ export class OrganizationController extends Controller {
|
|||
...draft,
|
||||
id: undefined,
|
||||
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);
|
||||
|
||||
mapping.byAncestorDNA.set(draft.ancestorDNA, saved.id);
|
||||
|
|
@ -9100,8 +9109,10 @@ export class OrganizationController extends Controller {
|
|||
if (nextHolderId != null && draftPos.positionIsSelected) {
|
||||
const _null: any = null;
|
||||
profileUpdates.set(nextHolderId, {
|
||||
posMasterNo: draftPosMaster ? (getPosMasterNo(draftPosMaster as PosMaster) ?? _null) : _null,
|
||||
org: draftPosMaster ? (getOrgFullName(draftPosMaster as PosMaster) ?? _null) : _null,
|
||||
posMasterNo: draftPosMaster
|
||||
? getPosMasterNo(draftPosMaster as PosMaster) ?? _null
|
||||
: _null,
|
||||
org: draftPosMaster ? getOrgFullName(draftPosMaster as PosMaster) ?? _null : _null,
|
||||
});
|
||||
}
|
||||
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
|
||||
|
|
@ -9169,10 +9180,10 @@ export class OrganizationController extends Controller {
|
|||
prefix: null,
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
position: null,
|
||||
posType: null,
|
||||
posLevel: null,
|
||||
posExecutive: null,
|
||||
profileId: null,
|
||||
rootDnaId: posMaster?.orgRoot?.ancestorDNA ?? null,
|
||||
child1DnaId: posMaster?.orgChild1?.ancestorDNA ?? null,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue