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 */ @Post("search") async listKpiSpecial( @Body() requestBody: { page: number; pageSize: number; year?: string | null; period?: string | null; keyword?: string | null; }, ) { // let condition: any = {}; // if (requestBody.keyword !== undefined && requestBody.keyword !== "") { // condition = { // where: [ // { // including: Like(`%${requestBody.keyword}%`), // includingName: Like(`%${requestBody.keyword}%`), // }, // ], // }; // } const [kpiSpecial, total] = await AppDataSource.getRepository(KpiSpecial) .createQueryBuilder("kpiSpecial") // .andWhere(condition) .andWhere(requestBody.year ? "kpiSpecial.year LIKE :year" : "1=1", { year: `%${requestBody.year}%`, }) .andWhere(requestBody.period ? "kpiSpecial.period LIKE :period" : "1=1", { period: `%${requestBody.period}%`, }) .andWhere( new Brackets((qb) => { qb.orWhere("kpiSpecial.including LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }).orWhere("kpiSpecial.includingName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }); }), ) .select([ "kpiSpecial.id", "kpiSpecial.year", "kpiSpecial.period", "kpiSpecial.including", "kpiSpecial.includingName", "kpiSpecial.createdAt", ]) .orderBy("kpiSpecial.createdAt", "DESC") .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); return new HttpSuccess({ data: kpiSpecial, 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(); } }