add transaction #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s

This commit is contained in:
harid 2026-06-24 18:05:54 +07:00
parent ecd0388eb0
commit 832c5d2cb3
10 changed files with 2322 additions and 1991 deletions

View file

@ -60,17 +60,6 @@ export interface SalaryCurrentExecutionContext {
req?: any;
}
/**
* batch all-or-nothing (single transaction batch)
* return result; throw rollback batch
* propagate error (caller failure )
*/
export interface ExecuteSalaryResult {
successCount: number;
failureCount: number;
failures: { profileId: string; reason: string }[];
}
/**
* Service ProfileSalary + ()
*
@ -98,7 +87,7 @@ export class ExecuteSalaryCurrentService {
async executeSalaryCurrent(
data: SalaryCurrentItem[],
ctx: SalaryCurrentExecutionContext,
): Promise<ExecuteSalaryResult> {
): Promise<void> {
const commandId = data?.find((x) => x.commandId)?.commandId ?? "unknown";
const commandCode = data?.find((x) => x.commandCode)?.commandCode ?? "unknown";
console.log(
@ -170,12 +159,10 @@ export class ExecuteSalaryCurrentService {
// ทุกคนใช้ manager ตัวเดียวกัน — คนใด throw จะ rollback ทั้ง batch
// และ propagate error ออกไป (ล้มเหลวทั้งหมด) โดย log error ของคนที่ทำให้ fail ก่อน rethrow
// ─────────────────────────────────────────────────────────────
let successCount = 0;
await AppDataSource.transaction(async (manager) => {
for (const item of data ?? []) {
try {
await this.processOne(item, ctx, manager, _posNumCodeSit, _posNumCodeSitAbb);
successCount++;
} catch (err) {
const reason =
err instanceof HttpError
@ -191,12 +178,6 @@ export class ExecuteSalaryCurrentService {
}
}
});
console.log(
`[ExecuteSalaryCurrentService] executeSalaryCurrent completed — success: ${successCount}, failure: 0`,
);
return { successCount, failureCount: 0, failures: [] };
}
/**
@ -373,10 +354,10 @@ export class ExecuteSalaryCurrentService {
if (posMasterOld != null) {
await posMasterRepository.save(posMasterOld);
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
await CreatePosMasterHistoryOfficer(posMasterOld.id, req, null, null, manager);
console.log(
`[ExecuteSalaryCurrentService] PosMasterOldId: ${posMasterOld.id}, profileId: ${item.profileId}`,
`[ExecuteSalaryCurrentService] Creating PosMasterHistory — posMasterId: ${posMasterOld.id}, profileId: ${item.profileId} (old)`,
);
await CreatePosMasterHistoryOfficer(posMasterOld.id, req, null, null, manager);
}
await posMasterRepository.save(posMaster);
@ -504,11 +485,11 @@ export class ExecuteSalaryCurrentService {
profile.amountSpecial = item.amountSpecial ?? null;
await profileRepository.save(profile);
await positionRepository.save(positionNew);
console.log(
`[ExecuteSalaryCurrentService] Applied new position — profileId: ${item.profileId}, positionId: ${positionNew.id}, posMasterId: ${posMaster.id}`,
);
}
// ส่ง manager เข้าไปเพื่อให้อยู่ใน transaction เดียวกัน
console.log(
`[ExecuteSalaryCurrentService] Creating PosMasterHistory — posMasterId: ${posMaster.id}, profileId: ${item.profileId}`,
);
await CreatePosMasterHistoryOfficer(posMaster.id, req, null, null, manager);
console.log(