add transaction #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
This commit is contained in:
parent
ecd0388eb0
commit
832c5d2cb3
10 changed files with 2322 additions and 1991 deletions
|
|
@ -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" });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue