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 { KpiPlan, createKpiPlan, updateKpiPlan } from "../entities/kpiPlan"; import CallAPI from "../interfaces/call-api"; @Route("api/v1/kpi/plan") @Tags("kpiPlan") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiPlanController extends Controller { private kpiPlanRepository = AppDataSource.getRepository(KpiPlan); /** * สร้างตัวชี้วัดตามแผนฯ * @param requestBody * @param request */ @Post() async createKpiPlan( @Body() requestBody: createKpiPlan, @Request() request: { user: Record }, ) { const kpiPlan = Object.assign(new KpiPlan(), requestBody); await new CallAPI() .PostData(request, "org/find/all", { node: requestBody.node, nodeId: requestBody.nodeId, }) .then((x) => { kpiPlan.root = x.root; kpiPlan.rootId = x.rootId; kpiPlan.rootShortName = x.rootShortName; kpiPlan.child1 = requestBody.node <= 0 ? null : x.child1; kpiPlan.child1Id = requestBody.node <= 0 ? null : x.child1Id; kpiPlan.child1ShortName = requestBody.node <= 0 ? null : x.child1ShortName; kpiPlan.child2 = requestBody.node <= 1 ? null : x.child2; kpiPlan.child2Id = requestBody.node <= 1 ? null : x.child2Id; kpiPlan.child2ShortName = requestBody.node <= 1 ? null : x.child2ShortName; kpiPlan.child3 = requestBody.node <= 2 ? null : x.child3; kpiPlan.child3Id = requestBody.node <= 2 ? null : x.child3Id; kpiPlan.child3ShortName = requestBody.node <= 2 ? null : x.child3ShortName; kpiPlan.child4 = requestBody.node <= 3 ? null : x.child4; kpiPlan.child4Id = requestBody.node <= 3 ? null : x.child4Id; kpiPlan.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName; }) .catch((x) => {}); await new CallAPI() .PostData(request, "development/strategy/find/all", { strategy: requestBody.strategy, strategyId: requestBody.strategyId, }) .then((x) => { kpiPlan.strategyChild1 = x.strategyChild1; kpiPlan.strategyChild1Id = x.strategyChild1Id; kpiPlan.strategyChild2 = requestBody.strategy <= 1 ? null : x.strategyChild2; kpiPlan.strategyChild2Id = requestBody.strategy <= 1 ? null : x.strategyChild2Id; kpiPlan.strategyChild3 = requestBody.strategy <= 2 ? null : x.strategyChild3; kpiPlan.strategyChild3Id = requestBody.strategy <= 2 ? null : x.strategyChild3Id; kpiPlan.strategyChild4 = requestBody.strategy <= 3 ? null : x.strategyChild4; kpiPlan.strategyChild4Id = requestBody.strategy <= 3 ? null : x.strategyChild4Id; kpiPlan.strategyChild5 = requestBody.strategy <= 4 ? null : x.strategyChild5; kpiPlan.strategyChild5Id = requestBody.strategy <= 4 ? null : x.strategyChild5Id; }) .catch((x) => {}); kpiPlan.round = requestBody.round.trim().toUpperCase(); kpiPlan.createdUserId = request.user.sub; kpiPlan.createdFullName = request.user.name; kpiPlan.lastUpdateUserId = request.user.sub; kpiPlan.lastUpdateFullName = request.user.name; await this.kpiPlanRepository.save(kpiPlan); return new HttpSuccess(kpiPlan.id); } /** * API แก้ไขตัวชี้วัดตามแผนฯ * @param id * @param requestBody * @param request */ @Put("{id}") async updateKpiPlan( @Path() id: string, @Body() requestBody: updateKpiPlan, @Request() request: { user: Record }, ) { const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } kpiPlan.round = requestBody.round.trim().toUpperCase(); Object.assign(kpiPlan, requestBody); await new CallAPI() .PostData(request, "org/find/all", { node: requestBody.node, nodeId: requestBody.nodeId, }) .then((x) => { kpiPlan.root = x.root; kpiPlan.rootId = x.rootId; kpiPlan.rootShortName = x.rootShortName; kpiPlan.child1 = requestBody.node <= 0 ? null : x.child1; kpiPlan.child1Id = requestBody.node <= 0 ? null : x.child1Id; kpiPlan.child1ShortName = requestBody.node <= 0 ? null : x.child1ShortName; kpiPlan.child2 = requestBody.node <= 1 ? null : x.child2; kpiPlan.child2Id = requestBody.node <= 1 ? null : x.child2Id; kpiPlan.child2ShortName = requestBody.node <= 1 ? null : x.child2ShortName; kpiPlan.child3 = requestBody.node <= 2 ? null : x.child3; kpiPlan.child3Id = requestBody.node <= 2 ? null : x.child3Id; kpiPlan.child3ShortName = requestBody.node <= 2 ? null : x.child3ShortName; kpiPlan.child4 = requestBody.node <= 3 ? null : x.child4; kpiPlan.child4Id = requestBody.node <= 3 ? null : x.child4Id; kpiPlan.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName; }) .catch((x) => {}); await new CallAPI() .PostData(request, "development/strategy/find/all", { strategy: requestBody.strategy, strategyId: requestBody.strategyId, }) .then((x) => { kpiPlan.strategyChild1 = x.strategyChild1; kpiPlan.strategyChild1Id = x.strategyChild1Id; kpiPlan.strategyChild2 = requestBody.strategy <= 1 ? null : x.strategyChild2; kpiPlan.strategyChild2Id = requestBody.strategy <= 1 ? null : x.strategyChild2Id; kpiPlan.strategyChild3 = requestBody.strategy <= 2 ? null : x.strategyChild3; kpiPlan.strategyChild3Id = requestBody.strategy <= 2 ? null : x.strategyChild3Id; kpiPlan.strategyChild4 = requestBody.strategy <= 3 ? null : x.strategyChild4; kpiPlan.strategyChild4Id = requestBody.strategy <= 3 ? null : x.strategyChild4Id; kpiPlan.strategyChild5 = requestBody.strategy <= 4 ? null : x.strategyChild5; kpiPlan.strategyChild5Id = requestBody.strategy <= 4 ? null : x.strategyChild5Id; }) .catch((x) => {}); kpiPlan.createdUserId = request.user.sub; kpiPlan.createdFullName = request.user.name; kpiPlan.lastUpdateUserId = request.user.sub; kpiPlan.lastUpdateFullName = request.user.name; await this.kpiPlanRepository.save(kpiPlan); return new HttpSuccess(id); } /** * API ตัวชี้วัดตามแผนฯ * @param id Guid, *Id ตัวชี้วัดตามแผนฯ */ @Get("{id}") async GetKpiPlanById(@Path() id: string) { const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } let node = null; let nodeId = null; if (kpiPlan.child4Id != null) { node = 4; nodeId = kpiPlan.child4Id; } else if (kpiPlan.child3Id != null) { node = 3; nodeId = kpiPlan.child3Id; } else if (kpiPlan.child2Id != null) { node = 2; nodeId = kpiPlan.child2Id; } else if (kpiPlan.child1Id != null) { node = 1; nodeId = kpiPlan.child1Id; } else if (kpiPlan.rootId != null) { node = 0; nodeId = kpiPlan.rootId; } let strategy = null; let strategyId = null; if (kpiPlan.strategyChild5Id != null) { strategy = 5; strategyId = kpiPlan.strategyChild5Id; } else if (kpiPlan.strategyChild4Id != null) { strategy = 4; strategyId = kpiPlan.strategyChild4Id; } else if (kpiPlan.strategyChild3Id != null) { strategy = 3; strategyId = kpiPlan.strategyChild3Id; } else if (kpiPlan.strategyChild2Id != null) { strategy = 2; strategyId = kpiPlan.strategyChild2Id; } else if (kpiPlan.strategyChild1Id != null) { strategy = 1; strategyId = kpiPlan.strategyChild1Id; } const formattedData = { id: kpiPlan.id, year: kpiPlan.year, round: kpiPlan.round, including: kpiPlan.including, includingName: kpiPlan.includingName, target: kpiPlan.target, unit: kpiPlan.unit, weight: kpiPlan.weight, achievement1: kpiPlan.achievement1, achievement2: kpiPlan.achievement2, achievement3: kpiPlan.achievement3, achievement4: kpiPlan.achievement4, achievement5: kpiPlan.achievement5, meaning: kpiPlan.meaning, formula: kpiPlan.formula, node: node, nodeId: nodeId, orgRevisionId: kpiPlan.orgRevisionId, strategy: strategy, strategyId: strategyId, }; return new HttpSuccess(formattedData); } /** * API list ตัวชี้วัดตามแผนฯ * @param page * @param pageSize * @param keyword */ @Get() async listKpiPlan( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("round") round?: string, @Query("nodeId") nodeId?: string | null, @Query("node") node?: number | null, @Query("keyword") keyword?: string, ) { const [kpiPlan, total] = await AppDataSource.getRepository(KpiPlan) .createQueryBuilder("kpiPlan") .andWhere( node != undefined && node != null ? node == 4 ? "kpiPlan.child4Id LIKE :nodeId" : node == 3 ? "kpiPlan.child3Id LIKE :nodeId" : node == 2 ? "kpiPlan.child2Id LIKE :nodeId" : node == 1 ? "kpiPlan.child1Id LIKE :nodeId" : "kpiPlan.rootId LIKE :nodeId" : "1=1", { nodeId: `${nodeId}`, }, ) .andWhere( round != undefined && round != null && round != "" ? "kpiPlan.round LIKE :round" : "1=1", { round: `${round?.trim().toUpperCase()}`, }, ) .andWhere(keyword != undefined ? "kpiPlan.projectName LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }) .select([ "kpiPlan.id", "kpiPlan.year", "kpiPlan.round", "kpiPlan.including", "kpiPlan.includingName", ]) .orderBy("kpiPlan.createdAt", "DESC") .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); return new HttpSuccess({ data: kpiPlan, total }); } /** * API ลบตัวชี้วัดตามแผนฯ * @param id */ @Delete("{id}") async deleteKpiPlan(@Path() id: string) { const kpiPlan = await this.kpiPlanRepository.findOne({ where: { id: id }, }); if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } await this.kpiPlanRepository.remove(kpiPlan); return new HttpSuccess(); } }