From 6a07841763daa6967bf811adcd890504a382d085 Mon Sep 17 00:00:00 2001 From: harid Date: Tue, 10 Mar 2026 18:12:58 +0700 Subject: [PATCH] =?UTF-8?q?fix=20bug=20=E0=B8=AD=E0=B8=AD=E0=B8=81?= =?UTF-8?q?=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=E0=B8=A2?= =?UTF-8?q?=E0=B8=81=E0=B9=80=E0=B8=A5=E0=B8=B4=E0=B8=81=E0=B8=A5=E0=B8=B2?= =?UTF-8?q?=E0=B8=AD=E0=B8=AD=E0=B8=81=20#2183=20+=20api=20=E0=B9=81?= =?UTF-8?q?=E0=B8=81=E0=B9=89=E0=B9=84=E0=B8=82=E0=B9=80=E0=B8=9B=E0=B8=A5?= =?UTF-8?q?=E0=B8=B5=E0=B9=88=E0=B8=A2=E0=B8=99=E0=B8=9C=E0=B8=B9=E0=B9=89?= =?UTF-8?q?=E0=B8=AA=E0=B8=A3=E0=B9=89=E0=B8=B2=E0=B8=87=E0=B8=84=E0=B8=B3?= =?UTF-8?q?=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=20#1551?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + src/controllers/CommandController.ts | 62 +++++++++++++++++++++------- src/controllers/ProfileController.ts | 1 + src/services/CommandService.ts | 47 +++++++++++++++++++++ 4 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 src/services/CommandService.ts diff --git a/.gitignore b/.gitignore index e8ab8057..55e347d2 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,5 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +.claude \ No newline at end of file diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index fa0e7172..9ed83c08 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -102,6 +102,7 @@ import { import { PostRetireToExprofile } from "./ExRetirementController"; import { LeaveType } from "../entities/LeaveType"; import { KeycloakAttributeService } from "../services/KeycloakAttributeService"; +import { reOrderCommandRecivesAndDelete } from "../services/CommandService"; @Route("api/v1/org/command") @Tags("Command") @Security("bearerAuth") @@ -2488,6 +2489,35 @@ export class CommandController extends Controller { return new HttpSuccess(); } + /** + * API แก้ไขเปลี่ยนผู้สร้างคำสั่ง + * @summary API แก้ไขเปลี่ยนผู้สร้างคำสั่ง + * @param {string} id Id คำสั่ง + */ + @Put("change-creator/{id}") + async ChangeCreator( + @Path() id: string, + @Body() req: { assignId: string; }, + @Request() request: RequestWithUser, + ) { + await new permission().PermissionUpdate(request, "COMMAND"); + const command = await this.commandRepository.findOne({ + where: { id: id } + }); + + if (!command) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); + } + + command.createdUserId = req.assignId; + command.lastUpdateUserId = request.user.sub; + command.lastUpdateFullName = request.user.name; + command.lastUpdatedAt = new Date(); + + await this.commandRepository.save(command); + return new HttpSuccess(); + } + /** * API สร้างรายการ body คำสั่ง * @@ -3984,6 +4014,7 @@ export class CommandController extends Controller { .orgRootShortName ?? ""; } } + const today = new Date().setHours(0,0,0,0); await Promise.all( body.data.map(async (item) => { const profile = await this.profileRepository.findOne({ @@ -4003,17 +4034,16 @@ export class CommandController extends Controller { if (code && ["C-PM-08", "C-PM-17", "C-PM-18"].includes(code)) { removePostMasterAct(profile.id); } - //ออกคำสั่งยกเลิกลาออกต้องอัพเดทสถานะคำสั่งลาออกเป็น CANCEL + //ออกคำสั่งยกเลิกลาออก ลบเฉพาะคนที่ขอยกเลิกลาออก else if (item.resignId && code && ["C-PM-41"].includes(code)) { - const commandRecive = await this.commandReciveRepository.findOne({ - select: ["commandId"], + const commandResign = await this.commandReciveRepository.findOne({ where: { refId: item.resignId }, + relations: { command: true }, }); - if (commandRecive && commandRecive.commandId) { - await this.commandRepository.update( - { id: commandRecive?.commandId }, - { status: "CANCEL" }, - ); + const executeDate = commandResign ? new Date(commandResign.command.commandExcecuteDate).setHours(0,0,0,0) : today; + if (_command.status != "REPORTED" + && (_command.status != "WAITING" || today < executeDate)) { + await reOrderCommandRecivesAndDelete(commandResign!.refId); } } let _commandYear = item.commandYear; @@ -4386,6 +4416,7 @@ export class CommandController extends Controller { .orgRootShortName ?? ""; } } + const today = new Date().setHours(0,0,0,0); await Promise.all( body.data.map(async (item) => { const profile = await this.profileEmployeeRepository.findOne({ @@ -4401,17 +4432,16 @@ export class CommandController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const code = _command?.commandType?.code; - //ออกคำสั่งยกเลิกลาออกต้องอัพเดทสถานะคำสั่งลาออกเป็น CANCEL + //ออกคำสั่งยกเลิกลาออก ลบเฉพาะคนที่ขอยกเลิกลาออก if (item.resignId && code && ["C-PM-42"].includes(code)) { - const commandRecive = await this.commandReciveRepository.findOne({ - select: ["commandId"], + const commandResign = await this.commandReciveRepository.findOne({ where: { refId: item.resignId }, + relations: { command: true }, }); - if (commandRecive && commandRecive.commandId) { - await this.commandRepository.update( - { id: commandRecive?.commandId }, - { status: "CANCEL" }, - ); + const executeDate = commandResign ? new Date(commandResign.command.commandExcecuteDate).setHours(0,0,0,0) : today; + if (_command.status != "REPORTED" + && (_command.status !== "WAITING" || today < executeDate)) { + await reOrderCommandRecivesAndDelete(commandResign!.id); } } let _commandYear = item.commandYear; diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index a9436648..141b1b82 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -9200,6 +9200,7 @@ export class ProfileController extends Controller { return { id: item.id, + keycloak: item.keycloak, prefix: item.prefix, rank: item.rank, firstName: item.firstName, diff --git a/src/services/CommandService.ts b/src/services/CommandService.ts new file mode 100644 index 00000000..ad9326b6 --- /dev/null +++ b/src/services/CommandService.ts @@ -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 + */ +export async function reOrderCommandRecivesAndDelete( + refId: string +): Promise { + 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" }); + } + +}