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 { Like, Not } from "typeorm"; import HttpStatusCode from "../interfaces/http-status"; import { KpiGroup, creatKpiGroup, updateKpiGroup } from "../entities/kpiGroup"; @Route("api/v1/kpi/group") @Tags("kpiGroup") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiGroupController extends Controller { private kpiGroupRepository = AppDataSource.getRepository(KpiGroup); /** * API สร้างกลุ่มงาน * @param requestBody * @returns */ @Post() @Example({ nameGroupKPI: "string", //ชื่อกลุ่มงาน }) async createKpiGroup( @Body() requestBody: creatKpiGroup, @Request() request: { user: Record }, ) { const kpiGroup = Object.assign(new KpiGroup(), requestBody); const chkkpinameGroup = await this.kpiGroupRepository.findOne({ where: { nameGroupKPI: requestBody.nameGroupKPI, }, }); if (chkkpinameGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว"); } kpiGroup.createdUserId = request.user.sub; kpiGroup.createdFullName = request.user.name; kpiGroup.lastUpdateUserId = request.user.sub; kpiGroup.lastUpdateFullName = request.user.name; await this.kpiGroupRepository.save(kpiGroup); return new HttpSuccess(kpiGroup.id); } /** * API แก้ไขชื่อกลุ่มงาน * @param id ไอดีของกลุ่มงาน */ @Put("{id}") async updateKpiGroup( @Path() id: string, @Body() requestBody: updateKpiGroup, @Request() request: { user: Record }, ) { const kpiGroup = await this.kpiGroupRepository.findOne({ where: { id: id }, }); if (!kpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } const chkkpinameGroup = await this.kpiGroupRepository.findOne({ where: { nameGroupKPI: requestBody.nameGroupKPI, }, }); if (chkkpinameGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว"); } this.kpiGroupRepository.merge(kpiGroup, requestBody); kpiGroup.lastUpdateUserId = request.user.sub; kpiGroup.lastUpdateFullName = request.user.name; await this.kpiGroupRepository.save(kpiGroup); return new HttpSuccess(id); } /** * API ชื่อกลุ่มงาน * @param id */ @Get("{id}") @Example({ nameGroupKPI: "string", //ชื่อกลุ่มงาน }) async KpiGroupById(@Path() id: string) { const kpiGroup = await this.kpiGroupRepository.findOne({ where: { id: id }, select: ["nameGroupKPI"], }); if (!kpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } return new HttpSuccess(kpiGroup); } /** * API ลบกลุ่มงาน * @param id */ @Delete("{id}") async deleteKpiGroup(@Path() id: string) { const kpiGroup = await this.kpiGroupRepository.findOne({ where: { id: id }, }); if (!kpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } await this.kpiGroupRepository.remove(kpiGroup); return new HttpSuccess(); } /** * API list กลุ่มงาน * @param page * @param pageSize */ @Get() async listKpiGroup( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { let whereClause: any = {}; if (keyword !== undefined && keyword !== "") { whereClause = { where: [{ nameGroupKPI: Like(`%${keyword}%`) }], }; } const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({ ...whereClause, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), }); return new HttpSuccess({ data: kpiGroup, total }); } }