fix performance
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m2s

This commit is contained in:
Warunee Tamkoo 2026-04-30 16:35:00 +07:00
parent 3ccdb691f6
commit 519fd97968
4 changed files with 790 additions and 25 deletions

View file

@ -11,6 +11,7 @@ import { PosMasterHistory } from "../entities/PosMasterHistory";
import { Position } from "../entities/Position";
import { ProfileEducation } from "../entities/ProfileEducation";
import { RequestWithUser } from "../middlewares/user";
import { chunkArray } from "../interfaces/utils";
export async function CreatePosMasterHistoryOfficer(
posMasterId: string,
@ -417,3 +418,123 @@ export async function BatchSavePosMasterHistoryOfficer(
return false;
}
}
export interface BatchHistoryOperation {
posMasterId: string;
posMasterData: PosMaster;
orgRevisionId: string;
lastUpdateUserId: string;
lastUpdateFullName: string;
}
export async function BatchUpdatePosMasters(
manager: any,
updates: { id: string; current_holderId: string | null; lastUpdateUserId: string; lastUpdateFullName: string; lastUpdatedAt: Date }[]
): Promise<void> {
if (updates.length === 0) return;
const repoPosmaster = manager.getRepository(PosMaster);
const CHUNK_SIZE = 1000;
const chunks = chunkArray(updates, CHUNK_SIZE);
for (const chunk of chunks) {
const ids = chunk.map((u: any) => u.id);
await repoPosmaster
.createQueryBuilder()
.update(PosMaster)
.set({
next_holderId: null,
lastUpdateUserId: chunk[0].lastUpdateUserId,
lastUpdateFullName: chunk[0].lastUpdateFullName,
lastUpdatedAt: chunk[0].lastUpdatedAt
})
.where('id IN (:...ids)', { ids })
.execute();
for (const update of chunk) {
await repoPosmaster.update(update.id, {
current_holderId: update.current_holderId
});
}
}
}
export async function BatchCreatePosMasterHistoryOfficer(
manager: any,
operations: BatchHistoryOperation[]
): Promise<void> {
if (operations.length === 0) return;
const repoHistory = manager.getRepository(PosMasterHistory);
const repoOrgRevision = manager.getRepository(OrgRevision);
const _null: any = null;
const orgRevisionIds = [...new Set(operations.map(op => op.orgRevisionId))];
const revisions = await repoOrgRevision.findBy({
id: In(orgRevisionIds),
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
});
const currentRevisionIds = new Set(revisions.map((r: any) => r.id));
const historyRecords: PosMasterHistory[] = [];
for (const op of operations) {
const pm = op.posMasterData;
const checkCurrentRevision = currentRevisionIds.has(pm.orgRevisionId);
const h = new PosMasterHistory();
h.ancestorDNA = pm.ancestorDNA ?? _null;
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;
}
const selectedPosition = pm.positions?.find((p: any) => p.positionIsSelected === true) ?? null;
h.position = selectedPosition?.positionName ?? _null;
h.posType = selectedPosition?.posType?.posTypeName ?? _null;
h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null;
h.posExecutive = selectedPosition?.posExecutive?.posExecutiveName ?? _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.shortName = [
pm.orgChild4?.orgChild4ShortName,
pm.orgChild3?.orgChild3ShortName,
pm.orgChild2?.orgChild2ShortName,
pm.orgChild1?.orgChild1ShortName,
pm.orgRoot?.orgRootShortName,
].find((s: any) => typeof s === "string" && s.trim().length > 0) ?? _null;
h.createdUserId = op.lastUpdateUserId;
h.createdFullName = op.lastUpdateFullName;
h.lastUpdateUserId = op.lastUpdateUserId;
h.lastUpdateFullName = op.lastUpdateFullName;
h.createdAt = new Date();
h.lastUpdatedAt = new Date();
historyRecords.push(h);
}
const CHUNK_SIZE = 500;
const chunks = chunkArray(historyRecords, CHUNK_SIZE);
for (const chunk of chunks) {
await repoHistory.save(chunk);
}
}