From 8d010cd389994d07c7daa5df3dd536c9fc3034bb Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 28 Aug 2025 12:54:09 +0700 Subject: [PATCH] migrate && optimize --- src/controllers/ProfileSalaryController.ts | 32 +++++++++---------- src/entities/Registry.ts | 2 +- src/entities/RegistryEmployee.ts | 2 +- ...65498-update_registry_fix_length_prefix.ts | 20 ++++++++++++ 4 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 src/migration/1756357065498-update_registry_fix_length_prefix.ts diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index e40c8b0e..1f3c614b 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -340,17 +340,17 @@ export class ProfileSalaryController extends Controller { @Get("Registry") public async Registry() { await this.registryRepo.clear(); - const profile = await this.profileRepo.find(); - for await (const x of profile) { - const _regis = await AppDataSource.getRepository(viewRegistryOfficer) - .createQueryBuilder("registryOfficer") - .where("registryOfficer.profileId IN (:id)", { id: x.id }) - .getMany(); - - const mapData = _regis.map(x => ({ + const allRegis = await AppDataSource.getRepository(viewRegistryOfficer) + .createQueryBuilder("registryOfficer") + .getMany(); + const profileIds = new Set((await this.profileRepo.find()).map(p => p.id)); + const mapData = allRegis + .filter(x => profileIds.has(x.profileId)) + .map(x => ({ ...x, Educations: x.Educations ? JSON.stringify(x.Educations) : "", })); + if (mapData.length > 0) { await this.registryRepo.save(mapData); } return new HttpSuccess(); @@ -359,17 +359,17 @@ export class ProfileSalaryController extends Controller { @Get("RegistryEmployee") public async RegistryEmployee() { await this.registryEmployeeRepo.clear(); - const profileEmp = await this.profileEmployeeRepo.find(); - for await (const x of profileEmp) { - const _regisEmp = await AppDataSource.getRepository(viewRegistryEmployee) - .createQueryBuilder("registryEmployee") - .where("registryEmployee.profileEmployeeId IN (:id)", { id: x.id }) - .getMany(); - - const mapData = _regisEmp.map(x => ({ + const allRegis = await AppDataSource.getRepository(viewRegistryEmployee) + .createQueryBuilder("registryEmployee") + .getMany(); + const profileEmpIds = new Set((await this.profileEmployeeRepo.find()).map(p => p.id)); + const mapData = allRegis + .filter(x => profileEmpIds.has(x.profileEmployeeId)) + .map(x => ({ ...x, Educations: x.Educations ? JSON.stringify(x.Educations) : "", })); + if (mapData.length > 0) { await this.registryEmployeeRepo.save(mapData); } return new HttpSuccess(); diff --git a/src/entities/Registry.ts b/src/entities/Registry.ts index 6bd03545..95a4d63e 100644 --- a/src/entities/Registry.ts +++ b/src/entities/Registry.ts @@ -24,7 +24,7 @@ export class Registry extends EntityBase { @Column({ nullable: true, - length: 20, + length: 40, comment: "คำนำหน้าชื่อ เช่น นาย นาง นางสาว", default: null, }) diff --git a/src/entities/RegistryEmployee.ts b/src/entities/RegistryEmployee.ts index d84acfde..e4b9dff6 100644 --- a/src/entities/RegistryEmployee.ts +++ b/src/entities/RegistryEmployee.ts @@ -24,7 +24,7 @@ export class RegistryEmployee extends EntityBase { @Column({ nullable: true, - length: 20, + length: 40, comment: "คำนำหน้าชื่อ เช่น นาย นาง นางสาว", default: null, }) diff --git a/src/migration/1756357065498-update_registry_fix_length_prefix.ts b/src/migration/1756357065498-update_registry_fix_length_prefix.ts new file mode 100644 index 00000000..564e3329 --- /dev/null +++ b/src/migration/1756357065498-update_registry_fix_length_prefix.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateRegistryFixLengthPrefix1756357065498 implements MigrationInterface { + name = 'UpdateRegistryFixLengthPrefix1756357065498' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`registryEmployee\` DROP COLUMN \`prefix\``); + await queryRunner.query(`ALTER TABLE \`registryEmployee\` ADD \`prefix\` varchar(40) NULL COMMENT 'คำนำหน้าชื่อ เช่น นาย นาง นางสาว'`); + await queryRunner.query(`ALTER TABLE \`registry\` DROP COLUMN \`prefix\``); + await queryRunner.query(`ALTER TABLE \`registry\` ADD \`prefix\` varchar(40) NULL COMMENT 'คำนำหน้าชื่อ เช่น นาย นาง นางสาว'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`registry\` DROP COLUMN \`prefix\``); + await queryRunner.query(`ALTER TABLE \`registry\` ADD \`prefix\` varchar(20) NULL COMMENT 'คำนำหน้าชื่อ เช่น นาย นาง นางสาว'`); + await queryRunner.query(`ALTER TABLE \`registryEmployee\` DROP COLUMN \`prefix\``); + await queryRunner.query(`ALTER TABLE \`registryEmployee\` ADD \`prefix\` varchar(20) NULL COMMENT 'คำนำหน้าชื่อ เช่น นาย นาง นางสาว'`); + } + +}