refactor transaction
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m2s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m2s
This commit is contained in:
parent
869bb093a3
commit
3335c4f44c
2 changed files with 251 additions and 499 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { In } from "typeorm";
|
||||
import { EntityManager, In } from "typeorm";
|
||||
import { SavePosMasterHistory } from "./../interfaces/OrgMapping";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
|
||||
|
|
@ -17,105 +17,118 @@ export async function CreatePosMasterHistoryOfficer(
|
|||
request: RequestWithUser | null,
|
||||
type?: string | null,
|
||||
positionData?: { positionId?: string } | null,
|
||||
manager?: EntityManager,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
await AppDataSource.transaction(async (manager) => {
|
||||
const repoPosmaster = manager.getRepository(PosMaster);
|
||||
const repoHistory = manager.getRepository(PosMasterHistory);
|
||||
const repoOrgRevision = manager.getRepository(OrgRevision);
|
||||
const repoPosition = manager.getRepository(Position);
|
||||
const execute = async (transactionManager: EntityManager) => {
|
||||
const repoPosmaster = transactionManager.getRepository(PosMaster);
|
||||
const repoHistory = transactionManager.getRepository(PosMasterHistory);
|
||||
const repoOrgRevision = transactionManager.getRepository(OrgRevision);
|
||||
const repoPosition = transactionManager.getRepository(Position);
|
||||
|
||||
const pm = await repoPosmaster.findOne({
|
||||
where: { id: posMasterId },
|
||||
relations: [
|
||||
"positions",
|
||||
"positions.posLevel",
|
||||
"positions.posType",
|
||||
"positions.posExecutive",
|
||||
"orgRoot",
|
||||
"orgChild1",
|
||||
"orgChild2",
|
||||
"orgChild3",
|
||||
"orgChild4",
|
||||
"current_holder",
|
||||
"next_holder",
|
||||
],
|
||||
});
|
||||
|
||||
if (!pm) return false;
|
||||
if (!pm.ancestorDNA) return false;
|
||||
|
||||
const checkCurrentRevision = await repoOrgRevision.findOne({
|
||||
where: {
|
||||
id: pm.orgRevisionId,
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
});
|
||||
const _null: any = null;
|
||||
const h = new PosMasterHistory();
|
||||
|
||||
// query position โดยตรงจาก positionRepository
|
||||
let selectedPosition: Position | null = null;
|
||||
if (positionData?.positionId) {
|
||||
selectedPosition = await repoPosition.findOne({
|
||||
where: { id: positionData.positionId },
|
||||
relations: { posLevel: true, posType: true, posExecutive: true },
|
||||
});
|
||||
} else {
|
||||
// ใช้ logic เดิม หาจาก pm.positions ที่ positionIsSelected = true
|
||||
selectedPosition =
|
||||
pm.positions.length > 0
|
||||
? pm.positions.find((p) => p.positionIsSelected === true) ?? null
|
||||
: null;
|
||||
}
|
||||
|
||||
h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null;
|
||||
if (!type || type != "DELETE") {
|
||||
if (checkCurrentRevision) {
|
||||
h.prefix = pm.current_holder?.prefix || _null;
|
||||
h.firstName = pm.current_holder?.firstName || _null;
|
||||
h.lastName = pm.current_holder?.lastName || _null;
|
||||
h.profileId = pm.current_holder?.id || _null;
|
||||
} else {
|
||||
h.prefix = pm.next_holder?.prefix || _null;
|
||||
h.firstName = pm.next_holder?.firstName || _null;
|
||||
h.lastName = pm.next_holder?.lastName || _null;
|
||||
}
|
||||
h.position = selectedPosition?.positionName ?? _null;
|
||||
h.posType = selectedPosition?.posType?.posTypeName ?? _null;
|
||||
h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null;
|
||||
}
|
||||
h.rootDnaId = pm.orgRoot?.ancestorDNA || _null;
|
||||
h.child1DnaId = pm.orgChild1?.ancestorDNA || _null;
|
||||
h.child2DnaId = pm.orgChild2?.ancestorDNA || _null;
|
||||
h.child3DnaId = pm.orgChild3?.ancestorDNA || _null;
|
||||
h.child4DnaId = pm.orgChild4?.ancestorDNA || _null;
|
||||
h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null;
|
||||
h.posMasterNo = pm.posMasterNo ?? _null;
|
||||
h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null;
|
||||
h.posExecutive = selectedPosition?.posExecutive?.posExecutiveName ?? _null;
|
||||
h.shortName =
|
||||
[
|
||||
pm.orgChild4?.orgChild4ShortName,
|
||||
pm.orgChild3?.orgChild3ShortName,
|
||||
pm.orgChild2?.orgChild2ShortName,
|
||||
pm.orgChild1?.orgChild1ShortName,
|
||||
pm.orgRoot?.orgRootShortName,
|
||||
].find((s) => typeof s === "string" && s.trim().length > 0) ?? _null;
|
||||
const userId = request?.user?.sub ?? "";
|
||||
const userName = request?.user?.name ?? "system";
|
||||
h.createdUserId = userId;
|
||||
h.createdFullName = userName;
|
||||
h.lastUpdateUserId = userId;
|
||||
h.lastUpdateFullName = userName;
|
||||
h.createdAt = new Date();
|
||||
h.lastUpdatedAt = new Date();
|
||||
await repoHistory.save(h);
|
||||
const pm = await repoPosmaster.findOne({
|
||||
where: { id: posMasterId },
|
||||
relations: [
|
||||
"positions",
|
||||
"positions.posLevel",
|
||||
"positions.posType",
|
||||
"positions.posExecutive",
|
||||
"orgRoot",
|
||||
"orgChild1",
|
||||
"orgChild2",
|
||||
"orgChild3",
|
||||
"orgChild4",
|
||||
"current_holder",
|
||||
"next_holder",
|
||||
],
|
||||
});
|
||||
|
||||
if (!pm || !pm.ancestorDNA) {
|
||||
return;
|
||||
}
|
||||
|
||||
const checkCurrentRevision = await repoOrgRevision.findOne({
|
||||
where: {
|
||||
id: pm.orgRevisionId,
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
});
|
||||
const _null: any = null;
|
||||
const h = new PosMasterHistory();
|
||||
|
||||
// query position โดยตรงจาก positionRepository
|
||||
let selectedPosition: Position | null = null;
|
||||
if (positionData?.positionId) {
|
||||
selectedPosition = await repoPosition.findOne({
|
||||
where: { id: positionData.positionId },
|
||||
relations: { posLevel: true, posType: true, posExecutive: true },
|
||||
});
|
||||
} else {
|
||||
// ใช้ logic เดิม หาจาก pm.positions ที่ positionIsSelected = true
|
||||
selectedPosition =
|
||||
pm.positions.length > 0
|
||||
? pm.positions.find((p) => p.positionIsSelected === true) ?? null
|
||||
: null;
|
||||
}
|
||||
|
||||
h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null;
|
||||
if (!type || type != "DELETE") {
|
||||
if (checkCurrentRevision) {
|
||||
h.prefix = pm.current_holder?.prefix || _null;
|
||||
h.firstName = pm.current_holder?.firstName || _null;
|
||||
h.lastName = pm.current_holder?.lastName || _null;
|
||||
h.profileId = pm.current_holder?.id || _null;
|
||||
} else {
|
||||
h.prefix = pm.next_holder?.prefix || _null;
|
||||
h.firstName = pm.next_holder?.firstName || _null;
|
||||
h.lastName = pm.next_holder?.lastName || _null;
|
||||
}
|
||||
h.position = selectedPosition?.positionName ?? _null;
|
||||
h.posType = selectedPosition?.posType?.posTypeName ?? _null;
|
||||
h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null;
|
||||
}
|
||||
h.rootDnaId = pm.orgRoot?.ancestorDNA || _null;
|
||||
h.child1DnaId = pm.orgChild1?.ancestorDNA || _null;
|
||||
h.child2DnaId = pm.orgChild2?.ancestorDNA || _null;
|
||||
h.child3DnaId = pm.orgChild3?.ancestorDNA || _null;
|
||||
h.child4DnaId = pm.orgChild4?.ancestorDNA || _null;
|
||||
h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null;
|
||||
h.posMasterNo = pm.posMasterNo ?? _null;
|
||||
h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null;
|
||||
h.posExecutive = selectedPosition?.posExecutive?.posExecutiveName ?? _null;
|
||||
h.shortName =
|
||||
[
|
||||
pm.orgChild4?.orgChild4ShortName,
|
||||
pm.orgChild3?.orgChild3ShortName,
|
||||
pm.orgChild2?.orgChild2ShortName,
|
||||
pm.orgChild1?.orgChild1ShortName,
|
||||
pm.orgRoot?.orgRootShortName,
|
||||
].find((s) => typeof s === "string" && s.trim().length > 0) ?? _null;
|
||||
const userId = request?.user?.sub ?? "";
|
||||
const userName = request?.user?.name ?? "system";
|
||||
h.createdUserId = userId;
|
||||
h.createdFullName = userName;
|
||||
h.lastUpdateUserId = userId;
|
||||
h.lastUpdateFullName = userName;
|
||||
h.createdAt = new Date();
|
||||
h.lastUpdatedAt = new Date();
|
||||
await repoHistory.save(h);
|
||||
};
|
||||
|
||||
try {
|
||||
if (manager) {
|
||||
await execute(manager);
|
||||
return true;
|
||||
}
|
||||
|
||||
await AppDataSource.transaction(async (transactionManager) => {
|
||||
await execute(transactionManager);
|
||||
});
|
||||
return true;
|
||||
} catch (err) {
|
||||
if (manager) {
|
||||
throw err;
|
||||
}
|
||||
console.error("CreatePosMasterHistoryOfficer transaction error:", err);
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue