diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index f2325bc8..7a7500eb 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -712,7 +712,15 @@ export class PositionController extends Controller { const [posMaster, total] = await this.posMasterRepository.findAndCount({ where: keywordConditions, order: { posMasterOrder: "ASC" }, - relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], + relations: [ + "orgRoot", + "orgChild1", + "orgChild2", + "orgChild3", + "orgChild4", + "current_holder", + "next_holder", + ], skip: (body.page - 1) * body.pageSize, take: body.pageSize, }); @@ -736,6 +744,16 @@ export class PositionController extends Controller { posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, + fullNameNextHolder: + posMaster.current_holder.prefix + + posMaster.current_holder.firstName + + " " + + posMaster.current_holder.lastName, + fullNameCurrentHolder: + posMaster.next_holder.prefix + + posMaster.next_holder.firstName + + " " + + posMaster.next_holder.lastName, orgShortname: body.type === 0 ? posMaster.orgRoot.orgRootShortName diff --git a/src/entities/PosMaster.ts b/src/entities/PosMaster.ts index 8191239e..461a97e1 100644 --- a/src/entities/PosMaster.ts +++ b/src/entities/PosMaster.ts @@ -137,15 +137,13 @@ export class PosMaster extends EntityBase { default: null, }) profileIdNextHolder: string; - + @Column({ length: 40, comment: "คีย์นอก(FK)ของตาราง orgRevision", }) orgRevisionId: string; //fk - - @ManyToOne(() => OrgRevision, (orgRevision) => orgRevision.posMasters) @JoinColumn({ name: "orgRevisionId" }) orgRevision: OrgRevision; @@ -170,12 +168,12 @@ export class PosMaster extends EntityBase { @JoinColumn({ name: "orgChild4Id" }) orgChild4: OrgChild4; - @ManyToOne(() => Profile, (profile) => profile.posMasters) - @JoinColumn({ name: "profileIdCurrentHolder" }) - profile: Profile; + @OneToOne(() => Profile) + @JoinColumn() + current_holder: Profile; - @ManyToOne(() => Profile, (profile) => profile.next_holder_posMasters) - @JoinColumn({ name: "profileIdNextHolder" }) + @OneToOne(() => Profile) + @JoinColumn() next_holder: Profile; @OneToMany(() => Position, (position) => position.posMaster) diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 38e328a7..44457737 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -1,4 +1,4 @@ -import { Entity, Column, OneToMany} from "typeorm"; +import { Entity, Column, OneToMany, OneToOne, JoinColumn} from "typeorm"; import { EntityBase } from "./base/Base"; import { PosMaster } from "./PosMaster"; @@ -36,11 +36,13 @@ export class Profile extends EntityBase { }) citizenId: string; - @OneToMany(() => PosMaster, (posMaster) => posMaster.profile) - posMasters: PosMaster[]; + @OneToOne(() => PosMaster) + @JoinColumn() + current_holder: PosMaster; - @OneToMany(() => PosMaster, (posMaster) => posMaster.next_holder) - next_holder_posMasters: PosMaster[]; + @OneToOne(() => PosMaster) + @JoinColumn() + next_holder: PosMaster; } export class CreateProfile { diff --git a/src/migration/1707194386583-add_table_profile1.ts b/src/migration/1707194386583-add_table_profile1.ts new file mode 100644 index 00000000..06405daf --- /dev/null +++ b/src/migration/1707194386583-add_table_profile1.ts @@ -0,0 +1,52 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableProfile11707194386583 implements MigrationInterface { + name = 'AddTableProfile11707194386583' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_1acc4ba5b4af308f0d8a1b7b249\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_9d29f52dcf499db3a5e8209b6dc\``); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`currentHolderId\` varchar(36) NULL`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD UNIQUE INDEX \`IDX_b5bfc34771c02a8078b9bf8944\` (\`currentHolderId\`)`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`nextHolderId\` varchar(36) NULL`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD UNIQUE INDEX \`IDX_49d2dcf6c8bd789ca99df7dc0d\` (\`nextHolderId\`)`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD \`currentHolderId\` varchar(36) NULL`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD UNIQUE INDEX \`IDX_dca00872043f09a74af1d5172f\` (\`currentHolderId\`)`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD \`nextHolderId\` varchar(36) NULL`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD UNIQUE INDEX \`IDX_d15696d0e541ead772db817b36\` (\`nextHolderId\`)`); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`citizenId\``); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`citizenId\` varchar(13) NULL COMMENT 'เลขประจำตัวประชาชน'`); + await queryRunner.query(`CREATE UNIQUE INDEX \`REL_b5bfc34771c02a8078b9bf8944\` ON \`profile\` (\`currentHolderId\`)`); + await queryRunner.query(`CREATE UNIQUE INDEX \`REL_49d2dcf6c8bd789ca99df7dc0d\` ON \`profile\` (\`nextHolderId\`)`); + await queryRunner.query(`CREATE UNIQUE INDEX \`REL_dca00872043f09a74af1d5172f\` ON \`posMaster\` (\`currentHolderId\`)`); + await queryRunner.query(`CREATE UNIQUE INDEX \`REL_d15696d0e541ead772db817b36\` ON \`posMaster\` (\`nextHolderId\`)`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_b5bfc34771c02a8078b9bf89445\` FOREIGN KEY (\`currentHolderId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_49d2dcf6c8bd789ca99df7dc0d4\` FOREIGN KEY (\`nextHolderId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_dca00872043f09a74af1d5172f3\` FOREIGN KEY (\`currentHolderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_d15696d0e541ead772db817b36a\` FOREIGN KEY (\`nextHolderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_d15696d0e541ead772db817b36a\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_dca00872043f09a74af1d5172f3\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_49d2dcf6c8bd789ca99df7dc0d4\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_b5bfc34771c02a8078b9bf89445\``); + await queryRunner.query(`DROP INDEX \`REL_d15696d0e541ead772db817b36\` ON \`posMaster\``); + await queryRunner.query(`DROP INDEX \`REL_dca00872043f09a74af1d5172f\` ON \`posMaster\``); + await queryRunner.query(`DROP INDEX \`REL_49d2dcf6c8bd789ca99df7dc0d\` ON \`profile\``); + await queryRunner.query(`DROP INDEX \`REL_b5bfc34771c02a8078b9bf8944\` ON \`profile\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`citizenId\``); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`citizenId\` int NULL COMMENT 'เลขประจำตัวประชาชน'`); + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP INDEX \`IDX_d15696d0e541ead772db817b36\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP COLUMN \`nextHolderId\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP INDEX \`IDX_dca00872043f09a74af1d5172f\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP COLUMN \`currentHolderId\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP INDEX \`IDX_49d2dcf6c8bd789ca99df7dc0d\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`nextHolderId\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP INDEX \`IDX_b5bfc34771c02a8078b9bf8944\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`currentHolderId\``); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_9d29f52dcf499db3a5e8209b6dc\` FOREIGN KEY (\`profileIdNextHolder\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_1acc4ba5b4af308f0d8a1b7b249\` FOREIGN KEY (\`profileIdCurrentHolder\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + +}