fix: script org move draf to current save posMasterHistory
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m57s

This commit is contained in:
Warunee Tamkoo 2026-02-12 10:38:16 +07:00
parent 7694a83d5a
commit 3c9e3a1bb6
3 changed files with 173 additions and 8 deletions

View file

@ -1,3 +1,4 @@
import { SavePosMasterHistory } from "./../interfaces/OrgMapping";
import { AppDataSource } from "../database/data-source";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
@ -44,9 +45,9 @@ export async function CreatePosMasterHistoryOfficer(
where: {
id: pm.orgRevisionId,
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false
}
})
orgRevisionIsDraft: false,
},
});
const _null: any = null;
const h = new PosMasterHistory();
const selectedPosition =
@ -260,3 +261,62 @@ export async function getTopDegrees(educations: ProfileEducation[]): Promise<str
.filter(Boolean)
.join("\n");
}
export async function SavePosMasterHistoryOfficer(
queryRunner: any,
posMasterDnaId: string,
profileId: string | null,
pm: SavePosMasterHistory | null,
): Promise<boolean> {
try {
// Type workaround: entity columns are nullable but types don't reflect it
const _null: any = null;
const repoPosMasterHistory = queryRunner.manager.getRepository(PosMasterHistory);
const pmh = await repoPosMasterHistory.findOne({
where: {
ancestorDNA: posMasterDnaId,
},
order: { createdAt: "DESC" },
});
// Check if we need to insert a new history record
const shouldInsert = !pmh && profileId && pm;
const profileChanged = pmh && pmh.profileId !== profileId;
if (shouldInsert || profileChanged) {
// insert new record
const newPmh = new PosMasterHistory();
newPmh.ancestorDNA = posMasterDnaId;
newPmh.prefix = pm?.prefix ?? _null;
newPmh.firstName = pm?.firstName ?? _null;
newPmh.lastName = pm?.lastName ?? _null;
newPmh.position = pm?.position ?? _null;
newPmh.posType = pm?.posType ?? _null;
newPmh.posLevel = pm?.posLevel ?? _null;
newPmh.posExecutive = pm?.posExecutive ?? _null;
newPmh.profileId = profileId ?? _null;
newPmh.rootDnaId = pm?.rootDnaId ?? _null;
newPmh.child1DnaId = pm?.child1DnaId ?? _null;
newPmh.child2DnaId = pm?.child2DnaId ?? _null;
newPmh.child3DnaId = pm?.child3DnaId ?? _null;
newPmh.child4DnaId = pm?.child4DnaId ?? _null;
newPmh.shortName = pm?.shortName ?? _null;
newPmh.posMasterNoPrefix = pm?.posMasterNoPrefix ?? _null;
newPmh.posMasterNo = pm?.posMasterNo ?? _null;
newPmh.posMasterNoSuffix = pm?.posMasterNoSuffix ?? _null;
// Add audit fields for data integrity
newPmh.createdUserId = "system";
newPmh.createdFullName = "system";
newPmh.lastUpdateUserId = "system";
newPmh.lastUpdateFullName = "system";
newPmh.createdAt = new Date();
newPmh.lastUpdatedAt = new Date();
await queryRunner.manager.save(PosMasterHistory, newPmh);
return true;
}
return true;
} catch (err) {
console.error("SavePosMasterHistoryOfficer error:", err);
return false;
}
}