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 { KpiSpecial, CreateKpiSpecial, UpdateKpiSpecial } from "../entities/kpiSpecial"; import CallAPI from "../interfaces/call-api"; import { Brackets, IsNull, Like, Not } from "typeorm"; @Route("api/v1/kpi/special") @Tags("kpiSpecial") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiSpecialController extends Controller { private kpiSpecialRepository = AppDataSource.getRepository(KpiSpecial); /** * สร้างตัวชี้วัด Special * @param requestBody * @param request */ @Post() async createKpiSpecial( @Body() requestBody: CreateKpiSpecial, @Request() request: { user: Record }, ) { const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ where: { including: String(requestBody.including), includingName: String(requestBody.includingName), }, }); if(chk_kpiSpecial){ throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", ); } const kpiSpecial = Object.assign(new KpiSpecial(), requestBody); if (!kpiSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } kpiSpecial.createdUserId = request.user.sub; kpiSpecial.createdFullName = request.user.name; kpiSpecial.lastUpdateUserId = request.user.sub; kpiSpecial.lastUpdateFullName = request.user.name; await this.kpiSpecialRepository.save(kpiSpecial); return new HttpSuccess(kpiSpecial.id); } /** * API แก้ไขตัวชี้วัด Special * @param id * @param requestBody * @param request */ @Put("{id}") async updateKpiSpecial( @Path() id: string, @Body() requestBody: UpdateKpiSpecial, @Request() request: { user: Record }, ) { const kpiSpecial = await this.kpiSpecialRepository.findOne({ where: { id: id }, }); if (!kpiSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); } const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ where: { id: Not(id), including: String(requestBody.including), includingName: String(requestBody.includingName), }, }); if(chk_kpiSpecial){ throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", ); } kpiSpecial.lastUpdateUserId = request.user.sub; kpiSpecial.lastUpdateFullName = request.user.name; this.kpiSpecialRepository.merge(kpiSpecial, requestBody); await this.kpiSpecialRepository.save(kpiSpecial); return new HttpSuccess(id); } /** * API ตัวชี้วัด Special * @param id Guid, *Id ตัวชี้วัด Special */ @Get("{id}") async GetKpiSpecialById(@Path() id: string) { const KpiSpecial = await this.kpiSpecialRepository.findOne({ where: { id: id } }); if (!KpiSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); } const mapData = { id: KpiSpecial.id, period: KpiSpecial.period, year: KpiSpecial.year, including: KpiSpecial.including, includingName: KpiSpecial.includingName, target: KpiSpecial.target, unit: KpiSpecial.unit, weight: KpiSpecial.weight, point: KpiSpecial.point, summary: KpiSpecial.summary, documentInfoEvidence: KpiSpecial.documentInfoEvidence, startDate: KpiSpecial.startDate, endDate: KpiSpecial.endDate, achievement1: KpiSpecial.achievement1, achievement2: KpiSpecial.achievement2, achievement3: KpiSpecial.achievement3, achievement4: KpiSpecial.achievement4, achievement5: KpiSpecial.achievement5, meaning: KpiSpecial.meaning, formula: KpiSpecial.formula, }; return new HttpSuccess(mapData); } /** * API list ตัวชี้วัด Special * @param page * @param pageSize * @param keyword */ @Get() async listKpiSpecial( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { let whereClause: any = {}; if (keyword !== undefined && keyword !== "") { whereClause = { where: [{ including: Like(`%${keyword}%`), includingName: Like(`%${keyword}%`), }], }; } const [kpiSpecial, total] = await this.kpiSpecialRepository.findAndCount({ ...whereClause, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), order: { createdAt: "ASC" }, }); const mapData = kpiSpecial.map((KpiSpecial) => ({ id: KpiSpecial.id, period: KpiSpecial.period, year: KpiSpecial.year, including: KpiSpecial.including, includingName: KpiSpecial.includingName, target: KpiSpecial.target, unit: KpiSpecial.unit, weight: KpiSpecial.weight, point: KpiSpecial.point, summary: KpiSpecial.summary, documentInfoEvidence: KpiSpecial.documentInfoEvidence, startDate: KpiSpecial.startDate, endDate: KpiSpecial.endDate, achievement1: KpiSpecial.achievement1, achievement2: KpiSpecial.achievement2, achievement3: KpiSpecial.achievement3, achievement4: KpiSpecial.achievement4, achievement5: KpiSpecial.achievement5, meaning: KpiSpecial.meaning, formula: KpiSpecial.formula, })); return new HttpSuccess({ data: mapData, total }); } /** * API ลบตัวชี้วัด Special * @param id */ @Delete("{id}") async deleteKpiSpecial(@Path() id: string) { const kpiSpecial = await this.kpiSpecialRepository.findOne({ where: { id: id }, }); if (!kpiSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); } await this.kpiSpecialRepository.remove(kpiSpecial); return new HttpSuccess(); } }