rollback code handler_org
Some checks failed
Build & Deploy on Dev / build (push) Has been cancelled

This commit is contained in:
Warunee Tamkoo 2026-05-01 17:08:53 +07:00
parent cba5991097
commit fd7a2af0a1
2 changed files with 1320 additions and 1515 deletions

View file

@ -11,7 +11,6 @@ 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,
@ -418,145 +417,3 @@ 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 CHUNK_SIZE = 5000;
const chunks = chunkArray(updates, CHUNK_SIZE);
for (const chunk of chunks) {
// Create a temporary table for this batch
const tempTableName = `temp_posmaster_update_${Date.now()}_${Math.random().toString(36).substring(7)}`;
try {
// Create temporary table
await manager.query(`
CREATE TEMPORARY TABLE ${tempTableName} (
id CHAR(36) PRIMARY KEY,
current_holderId CHAR(36) NULL,
lastUpdateUserId CHAR(36) NOT NULL,
lastUpdateFullName VARCHAR(255) NOT NULL,
lastUpdatedAt DATETIME NOT NULL
) ENGINE=InnoDB
`);
// Build insert query with proper parameter count
const insertParams: any[] = [];
const valuePlaceholders: string[] = [];
for (const u of chunk) {
valuePlaceholders.push('(?, ?, ?, ?, ?)');
insertParams.push(u.id, u.current_holderId, u.lastUpdateUserId, u.lastUpdateFullName, u.lastUpdatedAt);
}
// Bulk insert into temporary table
await manager.query(`
INSERT INTO ${tempTableName} (id, current_holderId, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt)
VALUES ${valuePlaceholders.join(',')}
`, insertParams);
// Update using JOIN with temporary table (very fast - single query per chunk)
await manager.query(`
UPDATE posMaster p
INNER JOIN ${tempTableName} t ON p.id = t.id
SET p.current_holderId = t.current_holderId,
p.next_holderId = NULL,
p.lastUpdateUserId = t.lastUpdateUserId,
p.lastUpdateFullName = t.lastUpdateFullName,
p.lastUpdatedAt = t.lastUpdatedAt
`);
} finally {
// Drop temporary table
await manager.query(`DROP TEMPORARY TABLE IF EXISTS ${tempTableName}`).catch(() => {});
}
}
}
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);
}
}

File diff suppressed because it is too large Load diff