From ee77d0c51c723b64cf2812f765f4a768e046152e Mon Sep 17 00:00:00 2001 From: Kittapath Date: Fri, 19 Apr 2024 17:45:56 +0700 Subject: [PATCH] no message --- src/controllers/KpiPlanController.ts | 30 ++++++++++++++++--- src/controllers/KpiRoleController.ts | 30 ++++++++++++++++--- src/entities/kpiPlan.ts | 10 ------- src/entities/kpiRole.ts | 10 ------- ...8564-update_table_kpiPlan_add_kpiPeriod.ts | 28 +++++++++++++++++ 5 files changed, 80 insertions(+), 28 deletions(-) create mode 100644 src/migration/1713523478564-update_table_kpiPlan_add_kpiPeriod.ts diff --git a/src/controllers/KpiPlanController.ts b/src/controllers/KpiPlanController.ts index 128022b..862f977 100644 --- a/src/controllers/KpiPlanController.ts +++ b/src/controllers/KpiPlanController.ts @@ -21,6 +21,7 @@ import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { KpiPlan, createKpiPlan, updateKpiPlan } from "../entities/kpiPlan"; import CallAPI from "../interfaces/call-api"; +import { KpiPeriod } from "../entities/kpiPeriod"; @Route("api/v1/kpi/plan") @Tags("kpiPlan") @@ -32,6 +33,7 @@ import CallAPI from "../interfaces/call-api"; @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiPlanController extends Controller { private kpiPlanRepository = AppDataSource.getRepository(KpiPlan); + private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); /** * สร้างตัวชี้วัดตามแผนฯ * @param requestBody @@ -43,6 +45,14 @@ export class kpiPlanController extends Controller { @Request() request: { user: Record }, ) { const kpiPlan = Object.assign(new KpiPlan(), requestBody); + if (requestBody.kpiPeriodId != null) { + const kpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { id: requestBody.kpiPeriodId }, + }); + if (!kpiPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); + } + } await new CallAPI() .PostData(request, "org/find/all", { node: requestBody.node, @@ -112,6 +122,14 @@ export class kpiPlanController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } + if (requestBody.kpiPeriodId != null) { + const kpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { id: requestBody.kpiPeriodId }, + }); + if (!kpiPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); + } + } Object.assign(kpiPlan, requestBody); await new CallAPI() .PostData(request, "org/find/all", { @@ -170,6 +188,7 @@ export class kpiPlanController extends Controller { async GetKpiPlanById(@Path() id: string) { const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, + relations: { kpiPeriod: true }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); @@ -212,8 +231,9 @@ export class kpiPlanController extends Controller { } const formattedData = { id: kpiPlan.id, - year: kpiPlan.year, - // round: kpiPlan.round, + year: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.year, + round: kpiPlan.kpiPeriod == null ? null : kpiPlan.kpiPeriod.durationKPI, + kpiPeriodId: kpiPlan.kpiPeriodId, including: kpiPlan.including, includingName: kpiPlan.includingName, target: kpiPlan.target, @@ -269,9 +289,11 @@ export class kpiPlanController extends Controller { }, ) .andWhere( - round != undefined && round != null && round != "" ? "kpiPlan.round LIKE :round" : "1=1", + round != undefined && round != null && round != "" + ? "kpiPlan.kpiPeriodId LIKE :round" + : "1=1", { - round: `${round?.trim().toUpperCase()}`, + round: `${round}`, }, ) .select([ diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index 0dfa513..2e7ac9e 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -21,6 +21,7 @@ import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { KpiRole, createKpiRole, updateKpiRole } from "../entities/kpiRole"; import CallAPI from "../interfaces/call-api"; +import { KpiPeriod } from "../entities/kpiPeriod"; @Route("api/v1/kpi/role") @Tags("kpiRole") @@ -32,6 +33,7 @@ import CallAPI from "../interfaces/call-api"; @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiRoleController extends Controller { private kpiRoleRepository = AppDataSource.getRepository(KpiRole); + private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); /** * สร้างตัวชี้วัดตามตำแหน่ง * @param requestBody @@ -43,6 +45,14 @@ export class kpiRoleController extends Controller { @Request() request: { user: Record }, ) { const kpiRole = Object.assign(new KpiRole(), requestBody); + if (requestBody.kpiPeriodId != null) { + const kpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { id: requestBody.kpiPeriodId }, + }); + if (!kpiPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); + } + } await new CallAPI() .PostData(request, "org/find/all", { node: requestBody.node, @@ -93,6 +103,14 @@ export class kpiRoleController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } + if (requestBody.kpiPeriodId != null) { + const kpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { id: requestBody.kpiPeriodId }, + }); + if (!kpiPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); + } + } Object.assign(kpiRole, requestBody); await new CallAPI() .PostData(request, "org/find/all", { @@ -133,6 +151,7 @@ export class kpiRoleController extends Controller { async GetKpiRoleById(@Path() id: string) { const kpiRole = await this.kpiRoleRepository.findOne({ where: { id: id }, + relations: { kpiPeriod: true }, }); if (!kpiRole) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); @@ -157,8 +176,9 @@ export class kpiRoleController extends Controller { } const formattedData = { id: kpiRole.id, - year: kpiRole.year, - // round: kpiRole.round, + year: kpiRole.kpiPeriod == null ? null : kpiRole.kpiPeriod.year, + round: kpiRole.kpiPeriod == null ? null : kpiRole.kpiPeriod.durationKPI, + kpiPeriodId: kpiRole.kpiPeriodId, including: kpiRole.including, includingName: kpiRole.includingName, target: kpiRole.target, @@ -214,9 +234,11 @@ export class kpiRoleController extends Controller { }, ) .andWhere( - round != undefined && round != null && round != "" ? "kpiRole.round LIKE :round" : "1=1", + round != undefined && round != null && round != "" + ? "kpiRole.kpiPeriod LIKE :round" + : "1=1", { - round: `${round?.trim().toUpperCase()}`, + round: `${round}`, }, ) .andWhere(position != undefined ? "kpiRole.position LIKE :position" : "1=1", { diff --git a/src/entities/kpiPlan.ts b/src/entities/kpiPlan.ts index 82f4153..575b688 100644 --- a/src/entities/kpiPlan.ts +++ b/src/entities/kpiPlan.ts @@ -5,12 +5,6 @@ import { KpiPeriod } from "./kpiPeriod"; @Entity("kpiPlan") export class KpiPlan extends EntityBase { - @Column({ - nullable: true, - comment: "ปีงบประมาณ", - }) - year: number; - @Column({ nullable: true, comment: "รหัสตัวชี้วัด", @@ -295,8 +289,6 @@ export class KpiPlan extends EntityBase { kpiPeriod: KpiPeriod; } export class createKpiPlan { - @Column() - year: number | null; @Column() including: string | null; @Column() @@ -336,8 +328,6 @@ export class createKpiPlan { } export class updateKpiPlan { - @Column() - year: number | null; @Column() including: string | null; @Column() diff --git a/src/entities/kpiRole.ts b/src/entities/kpiRole.ts index 3136bb6..b767dca 100644 --- a/src/entities/kpiRole.ts +++ b/src/entities/kpiRole.ts @@ -12,12 +12,6 @@ export class KpiRole extends EntityBase { }) position: string; - @Column({ - nullable: true, - comment: "ปีงบประมาณ", - }) - year: number; - @Column({ nullable: true, comment: "รหัสตัวชี้วัด", @@ -230,8 +224,6 @@ export class createKpiRole { @Column() position: string | null; @Column() - year: number | null; - @Column() including: string | null; @Column() includingName: string | null; @@ -269,8 +261,6 @@ export class updateKpiRole { @Column() position: string | null; @Column() - year: number | null; - @Column() including: string | null; @Column() includingName: string | null; diff --git a/src/migration/1713523478564-update_table_kpiPlan_add_kpiPeriod.ts b/src/migration/1713523478564-update_table_kpiPlan_add_kpiPeriod.ts new file mode 100644 index 0000000..668b557 --- /dev/null +++ b/src/migration/1713523478564-update_table_kpiPlan_add_kpiPeriod.ts @@ -0,0 +1,28 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableKpiPlanAddKpiPeriod1713523478564 implements MigrationInterface { + name = 'UpdateTableKpiPlanAddKpiPeriod1713523478564' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`kpiPlan\` DROP COLUMN \`round\``); + await queryRunner.query(`ALTER TABLE \`kpiPlan\` DROP COLUMN \`year\``); + await queryRunner.query(`ALTER TABLE \`kpiRole\` DROP COLUMN \`round\``); + await queryRunner.query(`ALTER TABLE \`kpiRole\` DROP COLUMN \`year\``); + await queryRunner.query(`ALTER TABLE \`kpiPlan\` ADD \`kpiPeriodId\` varchar(40) NULL COMMENT 'ไอดีรอบ'`); + await queryRunner.query(`ALTER TABLE \`kpiRole\` ADD \`kpiPeriodId\` varchar(40) NULL COMMENT 'ไอดีรอบ'`); + await queryRunner.query(`ALTER TABLE \`kpiPlan\` ADD CONSTRAINT \`FK_7e2320c78b8ae3a68ac771ea369\` FOREIGN KEY (\`kpiPeriodId\`) REFERENCES \`kpiPeriod\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`kpiRole\` ADD CONSTRAINT \`FK_6959e3ae543841bd5808bc58f22\` FOREIGN KEY (\`kpiPeriodId\`) REFERENCES \`kpiPeriod\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`kpiRole\` DROP FOREIGN KEY \`FK_6959e3ae543841bd5808bc58f22\``); + await queryRunner.query(`ALTER TABLE \`kpiPlan\` DROP FOREIGN KEY \`FK_7e2320c78b8ae3a68ac771ea369\``); + await queryRunner.query(`ALTER TABLE \`kpiRole\` DROP COLUMN \`kpiPeriodId\``); + await queryRunner.query(`ALTER TABLE \`kpiPlan\` DROP COLUMN \`kpiPeriodId\``); + await queryRunner.query(`ALTER TABLE \`kpiRole\` ADD \`year\` int NULL COMMENT 'ปีงบประมาณ'`); + await queryRunner.query(`ALTER TABLE \`kpiRole\` ADD \`round\` varchar(255) NULL COMMENT 'รอบการประเมิน'`); + await queryRunner.query(`ALTER TABLE \`kpiPlan\` ADD \`year\` int NULL COMMENT 'ปีงบประมาณ'`); + await queryRunner.query(`ALTER TABLE \`kpiPlan\` ADD \`round\` varchar(255) NULL COMMENT 'รอบการประเมิน'`); + } + +}