From 1c7d62e1490ca1cfd87d52d07fbff59f3db0161b Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 22 Apr 2024 13:23:31 +0700 Subject: [PATCH] api userEvaluation --- .../KpiUserEvaluationController.ts | 209 ++++++++++++++++++ ...13757864608-add_table_kpiUserEvaluation.ts | 16 ++ tsoa.json | 3 + 3 files changed, 228 insertions(+) create mode 100644 src/controllers/KpiUserEvaluationController.ts create mode 100644 src/migration/1713757864608-add_table_kpiUserEvaluation.ts diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts new file mode 100644 index 0000000..d227bf1 --- /dev/null +++ b/src/controllers/KpiUserEvaluationController.ts @@ -0,0 +1,209 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + Example, + SuccessResponse, + Response, + Query, + ArrayValidator +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpError from "../interfaces/http-error"; +import HttpStatusCode from "../interfaces/http-status"; +import { KpiPeriod } from "../entities/kpiPeriod"; +import { KpiUserEvaluation, createKpiUserEvaluation, updateKpiUserEvaluation } from "../entities/kpiUserEvaluation"; +import { Like, In } from "typeorm"; +import CallAPI from "../interfaces/call-api"; +import { Any } from "typeorm/browser"; + +@Route("api/v1/kpi/user/evaluation") +@Tags("kpiUserEvaluation") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class KpiUserEvaluationController extends Controller { + private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); + private kpiUserEvalutionRepository = AppDataSource.getRepository(KpiUserEvaluation); + + /** + * API สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @summary สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * + */ + @Post() + async CreateKpiUserEvaluation( + @Body() requestBody: createKpiUserEvaluation, + @Request() request: { user: Record }, + ){ + const kpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { id: requestBody.kpiPeriodId }, + }); + if (!kpiPeriod) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + ); + } + + const kpiUserEvaluation = Object.assign(new KpiUserEvaluation(), requestBody); + await new CallAPI() + .GetData(request, "org/profile/keycloak/position") + .then((x) => { + kpiUserEvaluation.profileId = x.profileId + kpiUserEvaluation.prefix = x.prefix + kpiUserEvaluation.firstName = x.firstName + kpiUserEvaluation.lastName = x.lastName + }) + .catch((x) => {}); + kpiUserEvaluation.createdUserId = request.user.sub; + kpiUserEvaluation.createdFullName = request.user.name; + kpiUserEvaluation.lastUpdateUserId = request.user.sub; + kpiUserEvaluation.lastUpdateFullName = request.user.name; + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + return new HttpSuccess(kpiUserEvaluation.id); + } + + /** + * API แก้ไขรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @summary แก้ไขรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + */ + @Put("{id}") + async updateKpiUserEvaluation( + @Path() id: string, + @Body() requestBody: updateKpiUserEvaluation, + @Request() request: { user: Record }, + ) { + const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ + where: { id: id }, + }); + if (!KpiUserEvaluation) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", + ); + } + + const kpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { id: requestBody.kpiPeriodId }, + }); + if (!kpiPeriod) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + ); + } + + if (kpiUserEvaluation) { + this.kpiUserEvalutionRepository.merge(kpiUserEvaluation, requestBody); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + return new HttpSuccess(kpiUserEvaluation.id) + } + } + + /** + * API รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @summary รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + */ + @Get("{id}") + async GetKpiUserEvaluationById(@Path() id: string) { + const KpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ + where: { id: id }, + select: ["id", "profileId", "prefix", "firstName", "lastName", "kpiPeriodId"], + }) + if (!KpiUserEvaluation) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", + ); + } + return new HttpSuccess(KpiUserEvaluation); + } + + /** + * API รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + */ + @Get() + async listKpiUserEvaluation( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("period") period?: string, + @Query("keyword") keyword?: string, + ) { + + const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) + .createQueryBuilder("kpiUserEvaluation") + .leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod") + .andWhere( + keyword == undefined + ? "1=1" + : [ + { prefix: Like(`%${keyword}%`) }, + { firstName: Like(`%${keyword}%`) }, + { lastName: Like(`%${keyword}%`) }, + ], + ) + .andWhere(period == undefined && period == null && period == "" + ? "1=1" + : { "kpiPeriod.durationKPI" : In([period]) }) + .orderBy("kpiUserEvaluation.createdAt", "ASC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + const mapData = kpiUserEvaluation.map((item) => ({ + id: item.id, + profileId: item.profileId, + prefix: item.prefix, + firstname: item.firstName, + lastname: item.lastName, + kpiPeriodId: item.kpiPeriodId, + })); + return new HttpSuccess({ data: mapData, total }); + } + + /** + * API ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @summary ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + * + * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) + */ + @Delete("{id}") + async deleteKpiUserEvaluation(@Path() id: string) { + const KpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ + where: { id: id }, + }); + if (!KpiUserEvaluation) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", + ); + } + await this.kpiUserEvalutionRepository.remove(KpiUserEvaluation); + return new HttpSuccess(); + } +} diff --git a/src/migration/1713757864608-add_table_kpiUserEvaluation.ts b/src/migration/1713757864608-add_table_kpiUserEvaluation.ts new file mode 100644 index 0000000..149dccb --- /dev/null +++ b/src/migration/1713757864608-add_table_kpiUserEvaluation.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableKpiUserEvaluation1713757864608 implements MigrationInterface { + name = 'AddTableKpiUserEvaluation1713757864608' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE \`kpiUserEvaluation\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`prefix\` varchar(255) NULL COMMENT 'คำนำหน้า', \`firstName\` varchar(255) NULL COMMENT 'ชื่อ', \`lastName\` varchar(255) NULL COMMENT 'สกุล', \`kpiPeriodId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง kpiPeriodId', \`profileId\` varchar(40) NULL COMMENT 'ไอดีโปรไฟล์', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`ALTER TABLE \`kpiUserEvaluation\` ADD CONSTRAINT \`FK_ec9dcf722db1e7d96b21b6b85aa\` 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 \`kpiUserEvaluation\` DROP FOREIGN KEY \`FK_ec9dcf722db1e7d96b21b6b85aa\``); + await queryRunner.query(`DROP TABLE \`kpiUserEvaluation\``); + } + +} diff --git a/tsoa.json b/tsoa.json index 8b22a25..30d92cd 100644 --- a/tsoa.json +++ b/tsoa.json @@ -40,6 +40,9 @@ }, { "name": "kpiCapacity", "description": "สมรรถนะ" + }, + { + "name": "kpiUserEvaluation", "description": "รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)" } ] },