import { Controller, Get, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, Example, SuccessResponse, Response, Query, ArrayValidator } 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 { KpiPeriod } from "../entities/kpiPeriod"; import { KpiUserEvaluation, createKpiUserEvaluation, updateKpiUserEvaluation } from "../entities/kpiUserEvaluation"; import { Like, In } from "typeorm"; import CallAPI from "../interfaces/call-api"; @Route("api/v1/kpi/user/evaluation") @Tags("kpiUserEvaluation") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class KpiUserEvaluationController extends Controller { private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); private kpiUserEvalutionRepository = AppDataSource.getRepository(KpiUserEvaluation); /** * API สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * */ @Post() async CreateKpiUserEvaluation( @Body() requestBody: createKpiUserEvaluation, @Request() request: { user: Record }, ){ const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: requestBody.kpiPeriodId }, }); if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } const kpiUserEvaluation = Object.assign(new KpiUserEvaluation(), requestBody); await new CallAPI() .GetData(request, "org/profile/keycloak/position") .then((x) => { kpiUserEvaluation.profileId = x.profileId kpiUserEvaluation.prefix = x.prefix kpiUserEvaluation.firstName = x.firstName kpiUserEvaluation.lastName = x.lastName }) .catch((x) => {}); kpiUserEvaluation.createdUserId = request.user.sub; kpiUserEvaluation.createdFullName = request.user.name; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary แก้ไขรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) */ @Put("{id}") async updateKpiUserEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserEvaluation, @Request() request: { user: Record }, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!KpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: requestBody.kpiPeriodId }, }); if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } if (kpiUserEvaluation) { kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; this.kpiUserEvalutionRepository.merge(kpiUserEvaluation, requestBody); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); return new HttpSuccess(kpiUserEvaluation.id) } } /** * API รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) */ @Get("{id}") async GetKpiUserEvaluationById(@Path() id: string) { const KpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, select: ["id", "profileId", "prefix", "firstName", "lastName", "kpiPeriodId"], }) if (!KpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } return new HttpSuccess(KpiUserEvaluation); } /** * API รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * */ @Get() async listKpiUserEvaluation( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("period") period?: string, @Query("keyword") keyword?: string, ) { const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod") .andWhere( keyword == undefined ? "1=1" : [ { prefix: Like(`%${keyword}%`) }, { firstName: Like(`%${keyword}%`) }, { lastName: Like(`%${keyword}%`) }, ], ) .andWhere(period ? "kpiPeriod.durationKPI LIKE :period" : "1=1", { period: `%${period}%` }) .orderBy("kpiUserEvaluation.createdAt", "ASC") .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); const mapData = kpiUserEvaluation.map((item) => ({ id: item.id, profileId: item.profileId, prefix: item.prefix, firstname: item.firstName, lastname: item.lastName, kpiPeriodId: item.kpiPeriodId, })); return new HttpSuccess({ data: mapData, total }); } /** * API ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) */ @Delete("{id}") async deleteKpiUserEvaluation(@Path() id: string) { const KpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!KpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } await this.kpiUserEvalutionRepository.remove(KpiUserEvaluation); return new HttpSuccess(); } }