From 4914a0d733b1c5e2180c0f489cf1ec543d2d0b71 Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Fri, 5 Apr 2024 11:43:14 +0700 Subject: [PATCH] kpi period --- src/controllers/KpiPeriodController.ts | 153 ++++++++++++++++++ src/entities/kpi.ts | 45 ++++++ .../1712285860812-add_table_kpiperiod.ts | 15 ++ 3 files changed, 213 insertions(+) create mode 100644 src/controllers/KpiPeriodController.ts create mode 100644 src/entities/kpi.ts create mode 100644 src/migration/1712285860812-add_table_kpiperiod.ts diff --git a/src/controllers/KpiPeriodController.ts b/src/controllers/KpiPeriodController.ts new file mode 100644 index 0000000..190c285 --- /dev/null +++ b/src/controllers/KpiPeriodController.ts @@ -0,0 +1,153 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + Example, + SuccessResponse, + Response, + Query, +} 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, createKpi, updateKpi } from "../entities/kpi"; + +@Route("api/v1/kpi/period") +@Tags("kpi") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class kpiController extends Controller { + private kpiRepository = AppDataSource.getRepository(KpiPeriod); + /** + * สร้างรอบการประเมินผลการปฏิบัติหน้าที่ราชการ + * @param requestBody + * @param request + */ + @Post() + @Example({ + durationKPI: "string", //รอบเดือนที่สร้าง + startDate: "datetime", //วันเริ่มต้น + endDate: "datetime", //วันสิ้นสุด + }) + async createKpi( + @Body() requestBody: createKpi, + @Request() request: { user: Record }, + ) { + const kpi = Object.assign(new KpiPeriod(), requestBody); + kpi.durationKPI = requestBody.durationKPI.trim().toUpperCase(); + kpi.createdUserId = request.user.sub; + kpi.createdFullName = request.user.name; + kpi.lastUpdateUserId = request.user.sub; + kpi.lastUpdateFullName = request.user.name; + await this.kpiRepository.save(kpi); + return new HttpSuccess(kpi.id); + } + + /** + * API แก้ไขรอบการประเมินผลการปฏิบัติหน้าที่ราชการ + * @param id + * @param requestBody + * @param request + */ + @Put("{id}") + async update_kpi( + @Path() id: string, + @Body() requestBody: updateKpi, + @Request() request: { user: Record }, + ) { + const kpi_update = await this.kpiRepository.findOne({ + where: { id: id }, + }); + if (!kpi_update) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + ); + } + + requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase(); + this.kpiRepository.merge(kpi_update, requestBody); + kpi_update.createdUserId = request.user.sub; + kpi_update.createdFullName = request.user.name; + kpi_update.lastUpdateUserId = request.user.sub; + kpi_update.lastUpdateFullName = request.user.name; + await this.kpiRepository.save(kpi_update); + return new HttpSuccess(id); + } + + /** + * API รอบการประเมินผลการปฏิบัติหน้าที่ราชการ + * @param id Guid, *Id รอบการประเมินผลการปฏิบัติหน้าที่ราชการ + */ + @Get("{id}") + @Example({ + durationKPI: "string", //รอบเดือนที่สร้าง + startDate: "datetime", //วันเริ่มต้น + endDate: "datetime", //วันสิ้นสุด + }) + async GetSalaryById(@Path() id: string) { + const kpi = await this.kpiRepository.findOne({ + where: { id: id }, + select: ["durationKPI", "startDate", "endDate"], + }); + if (!kpi) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + ); + } + return new HttpSuccess(kpi); + } + + /** + * API list รอบการประเมินผลการปฏิบัติหน้าที่ราชการ + * @param page + * @param pageSize + * @param keyword + */ + @Get() + async listKpiPeriod( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("keyword") keyword?: string, + ) { + const [kpiPeriod, total] = await this.kpiRepository.findAndCount({ + ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), + }); + + return new HttpSuccess({ data: kpiPeriod, total }); + } + + /** + * API ลบรอบการประเมินผลการปฏิบัติหน้าที่ราชการ + * @param id + */ + @Delete("{id}") + async delete_salary(@Path() id: string) { + const chk_KpiPeriod = await this.kpiRepository.findOne({ + where: { id: id }, + }); + if (!chk_KpiPeriod) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + ); + } + + await this.kpiRepository.remove(chk_KpiPeriod); + return new HttpSuccess(); + } +} diff --git a/src/entities/kpi.ts b/src/entities/kpi.ts new file mode 100644 index 0000000..e9ccfd4 --- /dev/null +++ b/src/entities/kpi.ts @@ -0,0 +1,45 @@ +import { Entity, Column } from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("kpiPeriod") +export class KpiPeriod extends EntityBase { + @Column({ + nullable: true, + comment: "รอบการประเมิน", + default: null, + }) + durationKPI: string; + + @Column({ + nullable: true, + type: "datetime", + comment: "วันเริ่มต้น", + default: null, + }) + startDate: Date; + + @Column({ + nullable: true, + type: "datetime", + comment: "วันสิ้นสุด", + default: null, + }) + endDate: Date; +} +export class createKpi { + @Column() + durationKPI: string; + @Column() + startDate: Date; + @Column() + endDate: Date; +} + +export class updateKpi { + @Column() + durationKPI: string; + @Column() + startDate: Date; + @Column() + endDate: Date; +} diff --git a/src/migration/1712285860812-add_table_kpiperiod.ts b/src/migration/1712285860812-add_table_kpiperiod.ts new file mode 100644 index 0000000..fe95e76 --- /dev/null +++ b/src/migration/1712285860812-add_table_kpiperiod.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableKpiperiod1712285860812 implements MigrationInterface { + name = "AddTableKpiperiod1712285860812"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE \`kpiPeriod\` (\`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', \`durationKPI\` varchar(255) NULL COMMENT 'รอบการประเมิน', \`startDate\` datetime NULL COMMENT 'วันเริ่มต้น', \`endDate\` datetime NULL COMMENT 'วันสิ้นสุด', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE \`kpiPeriod\``); + } +}