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, creatKpiGroup, updateKpiGroup } 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 kpiGroupRepository = AppDataSource.getRepository(KpiGroup); /** * API สร้างกลุ่มงาน * @param requestBody * @param request * @returns */ @Post() @Example({ nameGroupKPI: "string", //ชื่อกลุ่มงาน }) async createKpiGroup( @Body() requestBody: creatKpiGroup, @Request() request: { user: Record }, ) { const kpi = Object.assign(new KpiGroup(), requestBody); kpi.nameGroupKPI = requestBody.nameGroupKPI; kpi.createdUserId = request.user.sub; kpi.createdFullName = request.user.name; kpi.lastUpdateUserId = request.user.sub; kpi.lastUpdateFullName = request.user.name; await this.kpiGroupRepository.save(kpi); return new HttpSuccess(kpi.id); } /** * API แก้ไขชื่อกลุ่มงาน * @param id ไอดีของกลุ่มงาน */ @Put("{id}") async updateKpiGroup( @Path() id: string, @Body() requestBody: updateKpiGroup, @Request() request: { user: Record }, ) { const kpiUpdate = await this.kpiGroupRepository.findOne({ where: { id: id }, }); if (!kpiUpdate) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } requestBody.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase(); this.kpiGroupRepository.merge(kpiUpdate, requestBody); kpiUpdate.createdUserId = request.user.sub; kpiUpdate.createdFullName = request.user.name; kpiUpdate.lastUpdateUserId = request.user.sub; kpiUpdate.lastUpdateFullName = request.user.name; await this.kpiGroupRepository.save(kpiUpdate); return new HttpSuccess(id); } /** * API ชื่อกลุ่มงาน * @param id */ @Get("{id}") @Example({ nameGroupKPI: "string", //ชื่อกลุ่มงาน }) async KpiGroupById(@Path() id: string) { const kpi = await this.kpiGroupRepository.findOne({ where: { id: id }, select: ["nameGroupKPI"], }); if (!kpi) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } return new HttpSuccess(kpi); } /** * API ลบกลุ่มงาน * @param id */ @Delete("{id}") async deleteKpiGroup(@Path() id: string) { const chkKpiGroup = await this.kpiGroupRepository.findOne({ where: { id: id }, }); if (!chkKpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } await this.kpiGroupRepository.remove(chkKpiGroup); return new HttpSuccess(); } }