import { Controller, Get, Put, Route, Security, Tags, Body, Request, 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 } from "typeorm"; import HttpStatusCode from "../interfaces/http-status"; import { KpiEvaluation, updateKpiEvaluation } from "../entities/kpiEvaluation"; import permission from "../interfaces/permission"; import { RequestWithUser } from "../middlewares/user"; import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/kpi/evaluation") @Tags("kpiEvaluation") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiEvaluationController extends Controller { private kpiEvaluationRepository = AppDataSource.getRepository(KpiEvaluation); /** * API แก้ไขเกณฑ์การประเมิน * @param id ไอดีของเกณฑ์การประเมิน */ @Put() async updateKpiEvaluations( @Body() requestBody: updateKpiEvaluation[], @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "SYS_EVA_COMPETENCY"); const updatedIds: string[] = []; for (const item of requestBody) { const kpiEvaluation = await this.kpiEvaluationRepository.findOne({ where: { id: item.id }, }); if (!kpiEvaluation) { throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลเกณฑ์การประเมินนี้: ${item.id}`); } const before = structuredClone(kpiEvaluation); this.kpiEvaluationRepository.merge(kpiEvaluation, item); kpiEvaluation.lastUpdateUserId = request.user.sub; kpiEvaluation.lastUpdateFullName = request.user.name; kpiEvaluation.lastUpdatedAt = new Date(); await this.kpiEvaluationRepository.save(kpiEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiEvaluation }); updatedIds.push(item.id); } return new HttpSuccess(); } /** * API list เกณฑ์การประเมิน */ @Get("edit") async listKpiEvaluationEdit( @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_COMPETENCY"); let whereClause: any = {}; if (keyword !== undefined && keyword !== "") { whereClause = { where: [{ description: Like(`%${keyword}%`) }], }; whereClause.where.push({ level: Like(`%${keyword}%`) }); } const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({ ...whereClause, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), order: { level: "DESC", }, }); const formatted = kpiEvaluation.map((item) => ({ id: item.id, level: item.level, description: item.description, })); return new HttpSuccess({ data: formatted, total }); } /** * API เกณฑ์การประเมินในเมนูรายการการประเมิน */ @Get("criteria") async listKpiEvaluationEditInKpiList( @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { await new permission().PermissionGet(request, "SYS_KPI_LIST"); let whereClause: any = {}; if (keyword !== undefined && keyword !== "") { whereClause = { where: [{ description: Like(`%${keyword}%`) }], }; whereClause.where.push({ level: Like(`%${keyword}%`) }); } const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({ ...whereClause, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), order: { level: "DESC", }, }); const formatted = kpiEvaluation.map((item) => ({ id: item.id, level: item.level, description: item.description, })); return new HttpSuccess({ data: formatted, total }); } /** * API list เกณฑ์การประเมิน */ @Get() async listKpiEvaluation( @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { let whereClause: any = {}; if (keyword !== undefined && keyword !== "") { whereClause = { where: [{ description: Like(`%${keyword}%`) }], }; whereClause.where.push({ level: Like(`%${keyword}%`) }); } const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({ ...whereClause, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), order: { level: "DESC", }, }); const formatted = kpiEvaluation.map((item) => ({ id: item.id, level: item.level, description: item.description, })); return new HttpSuccess({ data: formatted, total }); } }