From 81e8dadd9bf30d6b653a94b38d96525729942c41 Mon Sep 17 00:00:00 2001 From: harid Date: Tue, 26 May 2026 15:30:51 +0700 Subject: [PATCH] Migrate update_command_add_shortName #242 --- src/controllers/CommandController.ts | 108 ++++++++++++++---- src/entities/Command.ts | 8 ++ ...9776860350-update_command_add_shortName.ts | 14 +++ 3 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 src/migration/1779776860350-update_command_add_shortName.ts diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index a30a3048..92c368e6 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -372,7 +372,7 @@ export class CommandController extends Controller { const data = commands.map((_data) => ({ id: _data.id, - commandNo: _data.commandNo, + commandNo: `${_data.shortName ?? ""} ${_data.commandNo}`.trim(), commandYear: _data.commandYear, commandAffectDate: _data.commandAffectDate, commandExcecuteDate: _data.commandExcecuteDate, @@ -522,7 +522,7 @@ export class CommandController extends Controller { const _command = { id: command.id, status: command.status, - commandNo: command.commandNo, + commandNo: `${command.shortName ?? ""} ${command.commandNo}`.trim(), commandYear: command.commandYear, issue: command.issue, detailHeader: command.detailHeader, @@ -572,6 +572,34 @@ export class CommandController extends Controller { } const data = new Command(); Object.assign(data, { ...command, ...requestBody }); + + // ถ้าเป็น officer (isOfficer == true) ดึง orgRoot.shortName มาใช้ + const userProfile = await this.profileRepository.findOne({ + where: { keycloak: request.user.sub }, + relations: { + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + }, + }, + }); + + if (userProfile) { + const currentHolder = userProfile.current_holders?.find( + (x: any) => + x.orgRevision?.orgRevisionIsDraft === false && + x.orgRevision?.orgRevisionIsCurrent === true, + ); + + if (currentHolder && currentHolder.orgChild1?.isOfficer) { + data.shortName = + requestBody.isBangkok && requestBody.isBangkok === "BANGKOK" + ? "กทม." + : currentHolder.orgRoot?.orgRootShortName ?? "สนป."; + } + } + data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.lastUpdatedAt = new Date(); @@ -1982,7 +2010,7 @@ export class CommandController extends Controller { if (!["C-PM-21", "C-PM-23"].includes(commandCode)) { _command = { issue: issue, - commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(command.commandNo), + commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(`${command.shortName ?? ""} ${command.commandNo}`.trim()), commandYear: command.commandYear == null ? "" @@ -2235,7 +2263,7 @@ export class CommandController extends Controller { ); _command = { issue: issue, - commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(command.commandNo), + commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(`${command.shortName ?? ""} ${command.commandNo}`.trim()), commandYear: command.commandYear == null ? "" @@ -2374,7 +2402,7 @@ export class CommandController extends Controller { data: { data: _command, issuerOrganizationName: issue, - commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(command.commandNo), + commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(`${command.shortName ?? ""} ${command.commandNo}`.trim()), commandYear: command.commandYear == null ? "" @@ -2588,6 +2616,7 @@ export class CommandController extends Controller { let command = new Command(); let commandCode: string = ""; let _null: any = null; + let userProfile: any = null; if ( requestBody.commandId != undefined && requestBody.commandId != null && @@ -2647,6 +2676,37 @@ export class CommandController extends Controller { command.lastUpdateUserId = request.user.sub; command.lastUpdateFullName = request.user.name; command.lastUpdatedAt = now; + + // Query profile ครั้งเดียว ใช้ร่วมกันทั้ง shortName และ CommandOperator + userProfile = await this.profileRepository.findOne({ + where: { keycloak: request.user.sub }, + relations: { + posLevel: true, + posType: true, + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }, + }); + + // เช็คถ้าไม่ใช่ กสจ. ดึง root.shortName มาปั๊ม + if (userProfile) { + const currentHolder = userProfile.current_holders?.find( + (x: any) => + x.orgRevision?.orgRevisionIsDraft === false && + x.orgRevision?.orgRevisionIsCurrent === true, + ); + + if (currentHolder && !currentHolder.orgChild1?.isOfficer) { + command.shortName = currentHolder.orgRoot?.orgRootShortName ?? null; + } + } + await this.commandRepository.save(command); } // insert commandOperator @@ -2655,24 +2715,28 @@ export class CommandController extends Controller { }); if (!checkCommandOperator) { if (request.user.sub) { - const profile = await this.profileRepository.findOne({ - where: { keycloak: request.user.sub }, - relations: { - posLevel: true, - posType: true, - current_holders: { - orgRevision: true, - orgRoot: true, - orgChild1: true, - orgChild2: true, - orgChild3: true, - orgChild4: true, + // ใช้ userProfile ที่ query ไปแล้วถ้ามี ถ้าไม่มีค่อย query ใหม่ + let profile = userProfile; + if (!profile) { + profile = await this.profileRepository.findOne({ + where: { keycloak: request.user.sub }, + relations: { + posLevel: true, + posType: true, + current_holders: { + orgRevision: true, + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, }, - }, - }); + }); + } if (profile) { const currentHolder = profile!.current_holders?.find( - (x) => + (x: any) => x.orgRevision?.orgRevisionIsDraft === false && x.orgRevision?.orgRevisionIsCurrent === true, ); @@ -8658,7 +8722,7 @@ export class CommandController extends Controller { if (issue == null) issue = "..................................."; const _command = { issue: issue, - commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(command.commandNo), + commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(`${command.shortName ?? ""} ${command.commandNo}`.trim()), commandYear: command.commandYear == null ? "" @@ -8763,7 +8827,7 @@ export class CommandController extends Controller { data: { data: _command, issuerOrganizationName: issue, - commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(command.commandNo), + commandNo: command.commandNo == null ? "" : Extension.ToThaiNumber(`${command.shortName ?? ""} ${command.commandNo}`.trim()), commandYear: command.commandYear == null ? "" diff --git a/src/entities/Command.ts b/src/entities/Command.ts index c6b26626..e6af1be8 100644 --- a/src/entities/Command.ts +++ b/src/entities/Command.ts @@ -34,6 +34,14 @@ export class Command extends EntityBase { }) issue: string; + @Column({ + nullable: true, + comment: "ชื่อย่อหน่วยงานที่ออกคำสั่ง", + length: 16, + default: null, + }) + shortName: string; + @Column({ nullable: true, comment: "เลขที่คำสั่ง", diff --git a/src/migration/1779776860350-update_command_add_shortName.ts b/src/migration/1779776860350-update_command_add_shortName.ts new file mode 100644 index 00000000..8754d42b --- /dev/null +++ b/src/migration/1779776860350-update_command_add_shortName.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateCommandAddShortName1779776860350 implements MigrationInterface { + name = 'UpdateCommandAddShortName1779776860350' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`command\` ADD \`shortName\` varchar(16) NULL COMMENT 'ชื่อย่อหน่วยงานที่ออกคำสั่ง'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`command\` DROP COLUMN \`shortName\``); + } + +}