fix bug ออกคำสั่งยกเลิกลาออก #2183 + api แก้ไขเปลี่ยนผู้สร้างคำสั่ง #1551
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m20s

This commit is contained in:
harid 2026-03-10 18:12:58 +07:00
parent baa8496a69
commit 6a07841763
4 changed files with 96 additions and 16 deletions

View file

@ -0,0 +1,47 @@
import { AppDataSource } from "../database/data-source";
import { CommandRecive } from "../entities/CommandRecive";
import { Command } from "../entities/Command";
/**
*
* @param refId refId
* @param code
* @returns Promise<void>
*/
export async function reOrderCommandRecivesAndDelete(
refId: string
): Promise<void> {
const commandReciveRepo = AppDataSource.getRepository(CommandRecive);
const commandRepo = AppDataSource.getRepository(Command);
// ค้นหาข้อมูลผู้ได้รับคำสั่งตาม refId
const commandRecive = await commandReciveRepo.findOne({
where: { refId: refId },
relations: { command: true},
});
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);
})
);
} else {
// ถ้าไม่มีผู้ได้รับคำสั่งเหลือเลย ให้ยกเลิกคำสั่ง
await commandRepo.update({ id: commandId }, { status: "CANCEL" });
}
}