Migrate update_command_add_shortName #242
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m2s

This commit is contained in:
harid 2026-05-26 15:30:51 +07:00
parent 136a4c562e
commit 81e8dadd9b
3 changed files with 108 additions and 22 deletions

View file

@ -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
? ""

View file

@ -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: "เลขที่คำสั่ง",

View file

@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class UpdateCommandAddShortName1779776860350 implements MigrationInterface {
name = 'UpdateCommandAddShortName1779776860350'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`command\` ADD \`shortName\` varchar(16) NULL COMMENT 'ชื่อย่อหน่วยงานที่ออกคำสั่ง'`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`command\` DROP COLUMN \`shortName\``);
}
}