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

2
.gitignore vendored
View file

@ -131,3 +131,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.claude

View file

@ -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;

View file

@ -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,

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" });
}
}