From 8339c554723d8d866fe439863ae1075915f776f4 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 18 Mar 2024 17:52:25 +0700 Subject: [PATCH 1/3] fix: update update interface cannot be null --- src/entities/ProfileEducation.ts | 65 +++++++++++--------------------- src/entities/ProfileHonor.ts | 28 ++++++-------- 2 files changed, 34 insertions(+), 59 deletions(-) diff --git a/src/entities/ProfileEducation.ts b/src/entities/ProfileEducation.ts index 974d68ab..c7cfa61c 100644 --- a/src/entities/ProfileEducation.ts +++ b/src/entities/ProfileEducation.ts @@ -177,68 +177,49 @@ export class ProfileEducation extends EntityBase { } export class CreateProfileEducation { - @Column("uuid") profileId: string | null; - - @Column() isActive: boolean; - - @Column() country: string | null; - - @Column() degree: string | null; - - @Column() duration: string | null; - - @Column() durationYear: number; - - @Column() field: string | null; - - @Column() finishDate: Date | null; - - @Column() fundName: string | null; - - @Column() gpa: string | null; - - @Column() institute: string | null; - - @Column() other: string | null; - - @Column() startDate: Date | null; - - @Column() endDate: Date | null; - - @Column() educationLevel: string | null; - - @Column("uuid") educationLevelId: string | null; - - @Column() positionPath: string | null; - - @Column("uuid") positionPathId: string | null; - - @Column() isDate: boolean | null; - - @Column() isEducation: boolean | null; - - @Column() note: string | null; } -export type UpdateProfileEducation = Partial; +export type UpdateProfileEducation = { + profileId?: string | null; + isActive?: boolean; + country?: string | null; + degree?: string | null; + duration?: string | null; + durationYear?: number; + field?: string | null; + finishDate?: Date | null; + fundName?: string | null; + gpa?: string | null; + institute?: string | null; + other?: string | null; + startDate?: Date | null; + endDate?: Date | null; + educationLevel?: string | null; + educationLevelId?: string | null; + positionPath?: string | null; + positionPathId?: string | null; + isDate?: boolean | null; + isEducation?: boolean | null; + note?: string | null; +}; diff --git a/src/entities/ProfileHonor.ts b/src/entities/ProfileHonor.ts index ab9cf936..d58614f1 100644 --- a/src/entities/ProfileHonor.ts +++ b/src/entities/ProfileHonor.ts @@ -75,29 +75,23 @@ export class ProfileHonor extends EntityBase { } export class CreateProfileHonor { - @Column("uuid") profileId: string | null; - - @Column() isActive: boolean; - - @Column() detail: string | null; - - @Column() issueDate: Date | null; - - @Column() issuer: string | null; - - @Column() - refCommandDate: Date | null; - - @Column() + refCommandDate: Date | null; refCommandNo: string | null; - - @Column() isDate: boolean | null; } -export type UpdateProfileHonor = Partial; +export type UpdateProfileHonor = { + profileId?: string | null; + isActive?: boolean; + detail?: string | null; + issueDate?: Date | null; + issuer?: string | null; + refCommandDate?: Date | null; + refCommandNo?: string | null; + isDate?: boolean | null; +}; From b0292f392198de5fce0347db91c96f93d8d09436 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:03:01 +0700 Subject: [PATCH 2/3] fix: insignia relations --- src/controllers/ProfileInsigniaController.ts | 20 ++- src/entities/Insignia.ts | 12 +- src/entities/ProfileInsignia.ts | 78 ++++------ src/entities/ProfileInsigniaHistory.ts | 150 ++----------------- 4 files changed, 74 insertions(+), 186 deletions(-) diff --git a/src/controllers/ProfileInsigniaController.ts b/src/controllers/ProfileInsigniaController.ts index bdd04120..d1d396b7 100644 --- a/src/controllers/ProfileInsigniaController.ts +++ b/src/controllers/ProfileInsigniaController.ts @@ -66,7 +66,14 @@ export class ProfileInsigniaController extends Controller { ], }) public async getInsignia(@Path() profileId: string) { - const record = await this.insigniaRepo.findBy({ profileId }); + const record = await this.insigniaRepo.find({ + relations: { + insignia: { + insigniaType: true, + }, + }, + where: { profileId }, + }); return new HttpSuccess(record); } @@ -128,8 +135,15 @@ export class ProfileInsigniaController extends Controller { ], }) public async getInsigniaHistory(@Path() InsigniaId: string) { - const record = await this.insigniaHistoryRepo.findBy({ - profileInsigniaId: InsigniaId, + const record = await this.insigniaHistoryRepo.find({ + relations: { + insignia: { + insigniaType: true, + }, + }, + where: { + profileInsigniaId: InsigniaId, + }, }); return new HttpSuccess(record); } diff --git a/src/entities/Insignia.ts b/src/entities/Insignia.ts index 40771f37..c1b1c02d 100644 --- a/src/entities/Insignia.ts +++ b/src/entities/Insignia.ts @@ -1,6 +1,8 @@ -import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; +import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm"; import { EntityBase } from "./base/Base"; import { InsigniaType } from "./InsigniaType"; +import { ProfileInsignia } from "./ProfileInsignia"; +import { ProfileInsigniaHistory } from "./ProfileInsigniaHistory"; @Entity("insignia") export class Insignia extends EntityBase { @@ -50,6 +52,14 @@ export class Insignia extends EntityBase { @ManyToOne(() => InsigniaType, (insigniaType) => insigniaType.insignias) @JoinColumn({ name: "insigniaTypeId" }) insigniaType: InsigniaType; + + @OneToMany(() => ProfileInsignia, (v) => v.insignia) + @JoinColumn({ name: "insigniaId" }) + profileInsignias: ProfileInsignia; + + @OneToMany(() => ProfileInsigniaHistory, (v) => v.insignia) + @JoinColumn({ name: "insigniaHistoryId" }) + profileInsigniaHistories: ProfileInsigniaHistory; } export class CreateInsignias { diff --git a/src/entities/ProfileInsignia.ts b/src/entities/ProfileInsignia.ts index cc7c1298..283b2c40 100644 --- a/src/entities/ProfileInsignia.ts +++ b/src/entities/ProfileInsignia.ts @@ -2,6 +2,7 @@ import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm"; import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; import { ProfileInsigniaHistory } from "./ProfileInsigniaHistory"; +import { Insignia } from "./Insignia"; @Entity("profileInsignia") export class ProfileInsignia extends EntityBase { @@ -64,22 +65,6 @@ export class ProfileInsignia extends EntityBase { }) receiveDate: Date; - @Column({ - nullable: true, - length: 40, - comment: "คีย์นอก(FK)ของตาราง Insignia", - default: null, - }) - insigniaId: string; - - @Column({ - nullable: true, - comment: "ประเภท", - type: "text", - default: null, - }) - insigniaType: string; - @Column({ nullable: true, type: "datetime", @@ -127,6 +112,17 @@ export class ProfileInsignia extends EntityBase { }) note: string; + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง Insignia", + default: null, + }) + insigniaId: string; + + @ManyToOne(() => Insignia, (v) => v.profileInsignias) + insignia: Insignia; + @OneToMany( () => ProfileInsigniaHistory, (profileInsigniaHistory) => profileInsigniaHistory.histories, @@ -139,53 +135,39 @@ export class ProfileInsignia extends EntityBase { } export class CreateProfileInsignia { - @Column("uuid") profileId: string | null; - - @Column() isActive: boolean; - - @Column() year: number; - - @Column() no: string | null; - - @Column() volume: string | null; - - @Column() section: string | null; - - @Column() page: string | null; - - @Column() receiveDate: Date | null; - - @Column("uuid") insigniaId: string | null; - - @Column() insigniaType: string | null; - - @Column() dateAnnounce: Date | null; - - @Column() issue: string | null; - - @Column() volumeNo: string | null; - - @Column() refCommandDate: Date | null; - - @Column() refCommandNo: string | null; - - @Column() note: string | null; } -export type UpdateProfileInsignia = Partial; +export type UpdateProfileInsignia = { + profileId?: string | null; + isActive?: boolean; + year?: number; + no?: string | null; + volume?: string | null; + section?: string | null; + page?: string | null; + receiveDate?: Date | null; + insigniaId?: string | null; + insigniaType?: string | null; + dateAnnounce?: Date | null; + issue?: string | null; + volumeNo?: string | null; + refCommandDate?: Date | null; + refCommandNo?: string | null; + note?: string | null; +}; diff --git a/src/entities/ProfileInsigniaHistory.ts b/src/entities/ProfileInsigniaHistory.ts index 7ca99f56..5e8584aa 100644 --- a/src/entities/ProfileInsigniaHistory.ts +++ b/src/entities/ProfileInsigniaHistory.ts @@ -1,7 +1,7 @@ import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm"; import { EntityBase } from "./base/Base"; -import { Profile } from "./Profile"; import { ProfileInsignia } from "./ProfileInsignia"; +import { Insignia } from "./Insignia"; @Entity("profileInsigniaHistory") export class ProfileInsigniaHistory extends EntityBase { @@ -11,112 +11,46 @@ export class ProfileInsigniaHistory extends EntityBase { }) isActive: boolean; - @Column({ - comment: "ปีที่ยื่นขอ", - }) + @Column({ comment: "ปีที่ยื่นขอ" }) year: number; - @Column({ - nullable: true, - length: 20, - comment: "ลำดับที่", - default: null, - }) + @Column({ nullable: true, length: 20, comment: "ลำดับที่", default: null }) no: string; - @Column({ - nullable: true, - length: 30, - comment: "เล่ม", - default: null, - }) + @Column({ nullable: true, length: 30, comment: "เล่ม", default: null }) volume: string; - @Column({ - nullable: true, - length: 30, - comment: "ตอน", - default: null, - }) + @Column({ nullable: true, length: 30, comment: "ตอน", default: null }) section: string; - @Column({ - nullable: true, - length: 30, - comment: "หน้า", - default: null, - }) + @Column({ nullable: true, length: 30, comment: "หน้า", default: null }) page: string; - @Column({ - nullable: true, - type: "datetime", - comment: "ลงวันที่", - default: null, - }) + @Column({ nullable: true, type: "datetime", comment: "ลงวันที่", default: null }) receiveDate: Date; - @Column({ - nullable: true, - length: 40, - comment: "คีย์นอก(FK)ของตาราง Insignia", - default: null, - }) + @Column({ nullable: true, length: 40, comment: "คีย์นอก(FK)ของตาราง Insignia", default: null }) insigniaId: string; - @Column({ - nullable: true, - comment: "ประเภท", - type: "text", - default: null, - }) - insigniaType: string; + @ManyToOne(() => Insignia, (v) => v.profileInsigniaHistories) + insignia: Insignia; - @Column({ - nullable: true, - type: "datetime", - comment: "วันที่ประกาศในราชกิจจาฯ", - default: null, - }) + @Column({ nullable: true, type: "datetime", comment: "วันที่ประกาศในราชกิจจาฯ", default: null }) dateAnnounce: Date; - @Column({ - nullable: true, - length: 300, - comment: "ราชกิจจาฯ ฉบับที่", - default: null, - }) + @Column({ nullable: true, length: 300, comment: "ราชกิจจาฯ ฉบับที่", default: null }) issue: string; - @Column({ - nullable: true, - length: 30, - comment: "เล่มที่", - default: null, - }) + @Column({ nullable: true, length: 30, comment: "เล่มที่", default: null }) volumeNo: string; - @Column({ - nullable: true, - type: "datetime", - comment: "เอกสารอ้างอิง (ลงวันที่)", - default: null, - }) + @Column({ nullable: true, type: "datetime", comment: "เอกสารอ้างอิง (ลงวันที่)", default: null }) refCommandDate: Date; - @Column({ - nullable: true, - comment: "เอกสารอ้างอิง (เลขที่คำสั่ง)", - type: "text", - default: null, - }) + @Column({ nullable: true, comment: "เอกสารอ้างอิง (เลขที่คำสั่ง)", type: "text", default: null }) refCommandNo: string; - @Column({ - nullable: true, - comment: "หมายเหตุ", - default: null, - }) + @Column({ nullable: true, comment: "หมายเหตุ", default: null }) note: string; @Column({ @@ -131,55 +65,3 @@ export class ProfileInsigniaHistory extends EntityBase { @JoinColumn({ name: "profileInsigniaId" }) histories: ProfileInsignia; } - -export class CreateProfileInsigniaHistory { - @Column() - isActive: boolean; - - @Column() - year: number; - - @Column() - no: string | null; - - @Column() - volume: string | null; - - @Column() - section: string | null; - - @Column() - page: string | null; - - @Column() - receiveDate: Date | null; - - @Column("uuid") - insigniaId: string | null; - - @Column() - insigniaType: string | null; - - @Column() - dateAnnounce: Date | null; - - @Column() - issue: string | null; - - @Column() - volumeNo: string | null; - - @Column() - refCommandDate: Date | null; - - @Column() - refCommandNo: string | null; - - @Column() - note: string | null; - - @Column("uuid") - profileInsigniaId: string | null; -} - -export type UpdateProfileInsigniaHistory = Partial; From be8147706804b0f17eaa98425a240f55dcae5f65 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:03:09 +0700 Subject: [PATCH 3/3] chore: add migrations --- ...-update_table_profile_insignia_relation.ts | 16 +++++++++++++++ ...table_profile_insignia_history_relation.ts | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/migration/1710758420804-update_table_profile_insignia_relation.ts create mode 100644 src/migration/1710759276845-update_table_profile_insignia_history_relation.ts diff --git a/src/migration/1710758420804-update_table_profile_insignia_relation.ts b/src/migration/1710758420804-update_table_profile_insignia_relation.ts new file mode 100644 index 00000000..ea6744eb --- /dev/null +++ b/src/migration/1710758420804-update_table_profile_insignia_relation.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileInsigniaRelation1710758420804 implements MigrationInterface { + name = 'UpdateTableProfileInsigniaRelation1710758420804' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileInsignia\` DROP COLUMN \`insigniaType\``); + await queryRunner.query(`ALTER TABLE \`profileInsignia\` ADD CONSTRAINT \`FK_7048ee3f58edbb05c9ab4e36a8d\` FOREIGN KEY (\`insigniaId\`) REFERENCES \`insignia\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileInsignia\` DROP FOREIGN KEY \`FK_7048ee3f58edbb05c9ab4e36a8d\``); + await queryRunner.query(`ALTER TABLE \`profileInsignia\` ADD \`insigniaType\` text NULL COMMENT 'ประเภท'`); + } + +} diff --git a/src/migration/1710759276845-update_table_profile_insignia_history_relation.ts b/src/migration/1710759276845-update_table_profile_insignia_history_relation.ts new file mode 100644 index 00000000..f00707f1 --- /dev/null +++ b/src/migration/1710759276845-update_table_profile_insignia_history_relation.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileInsigniaHistoryRelation1710759276845 implements MigrationInterface { + name = 'UpdateTableProfileInsigniaHistoryRelation1710759276845' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`insignia\` DROP FOREIGN KEY \`FK_440663b355747c7041d0f57b18f\``); + await queryRunner.query(`ALTER TABLE \`insignia\` DROP COLUMN \`insigniaId\``); + await queryRunner.query(`ALTER TABLE \`profileInsigniaHistory\` DROP COLUMN \`insigniaType\``); + await queryRunner.query(`ALTER TABLE \`profileInsigniaHistory\` ADD CONSTRAINT \`FK_262ceb7a87af7800f4da2c8f17d\` FOREIGN KEY (\`insigniaId\`) REFERENCES \`insignia\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileInsigniaHistory\` DROP FOREIGN KEY \`FK_262ceb7a87af7800f4da2c8f17d\``); + await queryRunner.query(`ALTER TABLE \`profileInsigniaHistory\` ADD \`insigniaType\` text NULL COMMENT 'ประเภท'`); + await queryRunner.query(`ALTER TABLE \`insignia\` ADD \`insigniaId\` varchar(36) NULL`); + await queryRunner.query(`ALTER TABLE \`insignia\` ADD CONSTRAINT \`FK_440663b355747c7041d0f57b18f\` FOREIGN KEY (\`insigniaId\`) REFERENCES \`profileInsignia\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + +}