From 28b5408d5be725f45fa1faae25c3cd9fcdc11ab8 Mon Sep 17 00:00:00 2001 From: adisak Date: Mon, 20 Apr 2026 08:05:16 +0700 Subject: [PATCH] #2427 and migration --- src/controllers/CommandController.ts | 23 +++++++-- src/controllers/PositionController.ts | 10 +++- src/entities/Profile.ts | 48 +++++++++++++++++++ ...08026834-add_position_fields_to_profile.ts | 37 ++++++++++++++ src/services/rabbitmq.ts | 7 +++ src/utils/org-formatting.ts | 39 +++++++++++++++ 6 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 src/migration/1776308026834-add_position_fields_to_profile.ts diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index da0ed3fe..e505aa3a 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -48,6 +48,7 @@ import { import { Position } from "../entities/Position"; import { PosMaster } from "../entities/PosMaster"; import { EmployeePosition } from "../entities/EmployeePosition"; +import { getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { ProfileDiscipline } from "../entities/ProfileDiscipline"; import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; @@ -3660,6 +3661,7 @@ export class CommandController extends Controller { const posMaster = await this.posMasterRepository.findOne({ where: { id: item.posmasterId }, + relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], }); if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -3715,6 +3717,7 @@ export class CommandController extends Controller { id: item.positionId, posMasterId: item.posmasterId, }, + relations: ["posExecutive"], }); // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if (positionNew != null) { @@ -3723,6 +3726,12 @@ export class CommandController extends Controller { profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId; profile.position = positionNew.positionName; + profile.positionField = positionNew.positionField ?? null; + profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null; + profile.positionArea = positionNew.positionArea ?? null; + profile.positionExecutiveField = positionNew.positionExecutiveField ?? null; + profile.posMasterNo = getPosMasterNo(posMaster); + profile.org = getOrgFullName(posMaster); } profile.amount = item.amount ?? null; profile.amountSpecial = item.amountSpecial ?? null; @@ -6876,7 +6885,7 @@ export class CommandController extends Controller { where: { id: item.bodyPosition.posmasterId, }, - relations: { orgRevision: true } + relations: { orgRevision: true, orgRoot: true, orgChild1: true, orgChild2: true, orgChild3: true, orgChild4: true } }); // เช็คว่า posMaster ที่หามาอยู่ในโครงสร้างปัจจุบันหรือไม่ @@ -6893,9 +6902,8 @@ export class CommandController extends Controller { orgRevisionIsDraft: false } }, - relations: { orgRevision: true } - }); - } + relations: { orgRevision: true, orgRoot: true, orgChild1: true, orgChild2: true, orgChild3: true, orgChild4: true } + }); } if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -6983,6 +6991,7 @@ export class CommandController extends Controller { id: item.bodyPosition.positionId, posMasterId: posMaster.id, }, + relations: ["posExecutive"], }); } @@ -6993,6 +7002,12 @@ export class CommandController extends Controller { profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId; profile.position = positionNew.positionName; + profile.positionField = positionNew.positionField ?? null; + profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null; + profile.positionArea = positionNew.positionArea ?? null; + profile.positionExecutiveField = positionNew.positionExecutiveField ?? null; + profile.posMasterNo = getPosMasterNo(posMaster); + profile.org = getOrgFullName(posMaster); // profile.dateStart = new Date(); await this.profileRepository.save(profile, { data: req }); setLogDataDiff(req, { before, after: profile }); diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 75d6c2a0..23806e6c 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -39,6 +39,7 @@ import { AuthRole } from "../entities/AuthRole"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { resolveNodeLevel, setLogDataDiff } from "../interfaces/utils"; +import { getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; import { PosMasterAssign } from "../entities/PosMasterAssign"; import { Assign } from "../entities/Assign"; import { ProfileEmployee } from "../entities/ProfileEmployee"; @@ -3793,7 +3794,7 @@ export class PositionController extends Controller { await new permission().PermissionUpdate(request, "SYS_ORG"); const dataMaster = await this.posMasterRepository.findOne({ where: { id: requestBody.posMaster }, - relations: ["positions"], + relations: ["positions", "orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], }); if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -3825,6 +3826,7 @@ export class PositionController extends Controller { if (_profile) { let _position = await this.positionRepository.findOne({ where: { id: requestBody.position, posMasterId: requestBody.posMaster }, + relations: ["posExecutive"], }); if (_position) { // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ @@ -3832,6 +3834,12 @@ export class PositionController extends Controller { _profile.position = _position.positionName; _profile.posTypeId = _position.posTypeId; _profile.posLevelId = _position.posLevelId; + _profile.positionField = _position.positionField ?? undefined; + _profile.posExecutive = _position.posExecutive?.posExecutiveName ?? undefined; + _profile.positionArea = _position.positionArea ?? undefined; + _profile.positionExecutiveField = _position.positionExecutiveField ?? undefined; + _profile.posMasterNo = getPosMasterNo(dataMaster); + _profile.org = getOrgFullName(dataMaster); await this.profileRepository.save(_profile); setLogDataDiff(request, { before, after: _profile }); } diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 72a1d505..a875a969 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -140,6 +140,54 @@ export class Profile extends EntityBase { }) posTypeId: string | null; + @Column({ + nullable: true, + comment: "สายงาน", + length: 45, + default: null, + }) + positionField: string; + + @Column({ + nullable: true, + comment: "ตำแหน่งทางการบริหาร", + length: 255, + default: null, + }) + posExecutive?: string; + + @Column({ + nullable: true, + comment: "ด้าน/สาขา", + length: 255, + default: null, + }) + positionArea?: string; + + @Column({ + nullable: true, + comment: "ด้านทางการบริหาร", + length: 255, + default: null, + }) + positionExecutiveField?: string; + + @Column({ + nullable: true, + comment: "เลขที่ตำแหน่ง", + length: 255, + default: null, + }) + posMasterNo?: string; + + @Column({ + nullable: true, + comment: "สังกัด", + type: "text", + default: null, + }) + org?: string; + @Column({ nullable: true, length: 255, diff --git a/src/migration/1776308026834-add_position_fields_to_profile.ts b/src/migration/1776308026834-add_position_fields_to_profile.ts new file mode 100644 index 00000000..9b214460 --- /dev/null +++ b/src/migration/1776308026834-add_position_fields_to_profile.ts @@ -0,0 +1,37 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddPositionFieldsToProfile1776308026834 implements MigrationInterface { + name = 'AddPositionFieldsToProfile1776308026834' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`positionField\` varchar(45) NULL COMMENT 'สายงาน'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`posExecutive\` varchar(255) NULL COMMENT 'ตำแหน่งทางการบริหาร'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`positionArea\` varchar(255) NULL COMMENT 'ด้าน/สาขา'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`positionExecutiveField\` varchar(255) NULL COMMENT 'ด้านทางการบริหาร'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`posMasterNo\` varchar(255) NULL COMMENT 'เลขที่ตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`org\` text NULL COMMENT 'สังกัด'`); + + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`positionField\` varchar(45) NULL COMMENT 'สายงาน'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`posExecutive\` varchar(255) NULL COMMENT 'ตำแหน่งทางการบริหาร'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`positionArea\` varchar(255) NULL COMMENT 'ด้าน/สาขา'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`positionExecutiveField\` varchar(255) NULL COMMENT 'ด้านทางการบริหาร'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`posMasterNo\` varchar(255) NULL COMMENT 'เลขที่ตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`org\` text NULL COMMENT 'สังกัด'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`org\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`posMasterNo\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`positionExecutiveField\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`positionArea\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`posExecutive\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`positionField\``); + + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`org\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`posMasterNo\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`positionExecutiveField\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`positionArea\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`posExecutive\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`positionField\``); + } +} diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index a8011900..5341d03c 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -3,6 +3,7 @@ import { AppDataSource } from "../database/data-source"; import { Command } from "../entities/Command"; import { chunkArray, commandTypePath } from "../interfaces/utils"; import CallAPI from "../interfaces/call-api"; +import { getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { PosMaster } from "../entities/PosMaster"; @@ -668,6 +669,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { profile.posLevelId = position?.posLevelId ?? _null; profile.posTypeId = position?.posTypeId ?? _null; profile.position = position?.positionName ?? _null; + profile.positionField = position?.positionField ?? _null; + profile.posExecutive = position?.posExecutive?.posExecutiveName ?? _null; + profile.positionArea = position?.positionArea ?? _null; + profile.positionExecutiveField = position?.positionExecutiveField ?? _null; + profile.posMasterNo = getPosMasterNo(item) ?? _null; + profile.org = getOrgFullName(item) ?? _null; await repoProfile.save(profile); } } diff --git a/src/utils/org-formatting.ts b/src/utils/org-formatting.ts index 701fb478..8b460a2f 100644 --- a/src/utils/org-formatting.ts +++ b/src/utils/org-formatting.ts @@ -68,3 +68,42 @@ export function filterPosMasters( ): PosMaster[] { return posMasters.filter((x) => x[childLevelIdKey] == null && x.isDirector === true); } + +/** + * สร้าง orgShortName จาก posMaster (ต้อง load org relations มาก่อน) + */ +export function getOrgShortName(posMaster: PosMaster): string { + if (posMaster.orgChild1Id === null) { + return posMaster.orgRoot?.orgRootShortName ?? ""; + } else if (posMaster.orgChild2Id === null) { + return posMaster.orgChild1?.orgChild1ShortName ?? ""; + } else if (posMaster.orgChild3Id === null) { + return posMaster.orgChild2?.orgChild2ShortName ?? ""; + } else if (posMaster.orgChild4Id === null) { + return posMaster.orgChild3?.orgChild3ShortName ?? ""; + } else { + return posMaster.orgChild4?.orgChild4ShortName ?? ""; + } +} + +/** + * สร้างชื่อสังกัดเต็ม จาก posMaster (join ด้วย \n) + */ +export function getOrgFullName(posMaster: PosMaster): string { + const parts = [ + posMaster.orgChild4?.orgChild4Name, + posMaster.orgChild3?.orgChild3Name, + posMaster.orgChild2?.orgChild2Name, + posMaster.orgChild1?.orgChild1Name, + posMaster.orgRoot?.orgRootName, + ]; + return parts.filter((part) => part !== undefined && part !== null).join("\n"); +} + +/** + * สร้างเลขที่ตำแหน่ง เช่น "กทม. 1234" + */ +export function getPosMasterNo(posMaster: PosMaster): string { + const orgShortName = getOrgShortName(posMaster); + return `${orgShortName} ${posMaster.posMasterNo}`; +}