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

@ -3,6 +3,7 @@ import { CommandRecive } from "../entities/CommandRecive";
import { Command } from "../entities/Command";
import { OrgRoot } from "../entities/OrgRoot";
import { Profile } from "../entities/Profile";
import { EntityManager } from "typeorm";
export interface PosNumCodeSitResult {
posNumCodeSit: string;
@ -17,43 +18,42 @@ export interface PosNumCodeSitResult {
*
* @param reciveId commandRecive.Id
* @param code
* @param manager operation transaction caller (all-or-nothing)
* @returns Promise<void>
*/
export async function reOrderCommandRecivesAndDelete(
reciveId: string
reciveId: string,
manager?: EntityManager,
): Promise<void> {
const commandReciveRepo = AppDataSource.getRepository(CommandRecive);
const commandRepo = AppDataSource.getRepository(Command);
const ds = manager ?? AppDataSource;
const commandReciveRepo = ds.getRepository(CommandRecive);
const commandRepo = ds.getRepository(Command);
// ค้นหาข้อมูลผู้ได้รับคำสั่งตาม reciveId
const commandRecive = await commandReciveRepo.findOne({
where: { id: reciveId }
where: { id: reciveId },
});
if (commandRecive == null)
return;
if (commandRecive == null) return;
const commandId = commandRecive.commandId;
// ลบตาม refId
await commandReciveRepo.delete(commandRecive.id);
const commandReciveList = await commandReciveRepo.find({
where: { commandId: commandId },
order: { order: "ASC" },
});
// ลำดับผู้ได้รับคำสั่งใหม่
if (commandReciveList.length > 0) {
await Promise.all(
commandReciveList.map(async (p, i) => {
p.order = i + 1;
await commandReciveRepo.save(p);
})
);
for (let i = 0; i < commandReciveList.length; i++) {
commandReciveList[i].order = i + 1;
await commandReciveRepo.save(commandReciveList[i]);
}
} else {
// ถ้าไม่มีผู้ได้รับคำสั่งเหลือเลย ให้ยกเลิกคำสั่ง
await commandRepo.update({ id: commandId }, { status: "CANCEL" });
}
}
/**