diff --git a/src/controllers/KpiGruopController.ts b/src/controllers/KpiGruopController.ts new file mode 100644 index 0000000..d80c09b --- /dev/null +++ b/src/controllers/KpiGruopController.ts @@ -0,0 +1,120 @@ +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 { KpiGroup, creatGroupKpi, updateGroupKpi } from "../entities/kpiGrop"; +@Route("api/v1/kpi/group") +@Tags("kpi") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class kpiGroupController extends Controller { + private kpiRepository = AppDataSource.getRepository(KpiGroup); + + /** + * API สร้างกลุ่มงาน + * @param requestBody + * @param request + * @returns + */ + @Post() + @Example({ + nameGroupKPI: "string", //ชื่อกลุ่มงาน + }) + async createKpi( + @Body() requestBody: creatGroupKpi, + @Request() request: { user: Record }, + ) { + const kpi = Object.assign(new KpiGroup(), requestBody); + kpi.nameGroupKPI = requestBody.nameGroupKPI.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 ไอดีของกลุ่มงาน + */ + @Put("{id}") + async update_kpi( + @Path() id: string, + @Body() requestBody: updateGroupKpi, + @Request() request: { user: Record }, + ) { + const kpi_update = await this.kpiRepository.findOne({ + where: { id: id }, + }); + if (!kpi_update) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); + } + + requestBody.nameGroupKPI = requestBody.nameGroupKPI.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 + */ + @Get("{id}") + @Example({ + nameGroupKPI: "string", //ชื่อกลุ่มงาน + }) + async CloseSalaryById(@Path() id: string) { + const kpi = await this.kpiRepository.findOne({ + where: { id: id }, + select: ["nameGroupKPI"], + }); + if (!kpi) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); + } + return new HttpSuccess(kpi); + } + + /** + * API ลบกลุ่มงาน + * @param id + */ + @Delete("{id}") + async delete_salary(@Path() id: string) { + const chk_KpiGroup = await this.kpiRepository.findOne({ + where: { id: id }, + }); + if (!chk_KpiGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); + } + + await this.kpiRepository.remove(chk_KpiGroup); + return new HttpSuccess(); + } +} diff --git a/src/entities/kpiGrop.ts b/src/entities/kpiGrop.ts new file mode 100644 index 0000000..f1871cb --- /dev/null +++ b/src/entities/kpiGrop.ts @@ -0,0 +1,21 @@ +import { Entity, Column } from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("kpiGroup") +export class KpiGroup extends EntityBase { + @Column({ + nullable: true, + comment: "ชื่อกลุ่มงาน", + default: null, + }) + nameGroupKPI: string; +} +export class creatGroupKpi { + @Column() + nameGroupKPI: string; +} + +export class updateGroupKpi { + @Column() + nameGroupKPI: string; +} diff --git a/src/migration/1713347203601-add_table_kpiGroup.ts b/src/migration/1713347203601-add_table_kpiGroup.ts new file mode 100644 index 0000000..3d252e8 --- /dev/null +++ b/src/migration/1713347203601-add_table_kpiGroup.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableKpiGroup1713347203601 implements MigrationInterface { + name = 'AddTableKpiGroup1713347203601' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE \`kpiGroup\` (\`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', \`nameGroupKPI\` varchar(255) NULL COMMENT 'ชื่อกลุ่มงาน', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE \`kpiGroup\``); + } + +}