import { Controller, Get, Post, Put, Delete, Patch, 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 HttpStatusCode from "../interfaces/http-status"; import { KpiUserPlanned, CreateKpiUserPlanned, UpdateKpiUserPlanned } from "../entities/kpiUserPlanned"; import HttpError from "../interfaces/http-error"; import { Not } from "typeorm"; import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; import { KpiPlan } from "../entities/kpiPlan"; @Route("api/v1/kpi/user/achievement/planned") @Tags("KpiUserPlanned") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class KpiUserPlannedController extends Controller { private kpiUserPlannedRepository = AppDataSource.getRepository(KpiUserPlanned); private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); private kpiPlanRepository = AppDataSource.getRepository(KpiPlan); /** * API เพิ่มงานตามแผนปฏิบัติราชการประจำปี * * @summary - เพิ่มงานตามแผนปฏิบัติราชการประจำปี #48 * */ @Post() async createKpiUserPlanned( @Body() requestBody: CreateKpiUserPlanned, @Request() request: { user: Record }, ) { const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ where:{id:requestBody.kpiUserEvaluationId}, }) if (!chkUserEvaluation) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); } const chkKpiPlan = await this.kpiPlanRepository.findOne({ where:{id:requestBody.kpiPlanId}, }) if (!chkKpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามแผน"); } const kpiUserPlanned = Object.assign(new KpiUserPlanned(), requestBody); if (!kpiUserPlanned) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } kpiUserPlanned.createdUserId = request.user.sub; kpiUserPlanned.createdFullName = request.user.name; kpiUserPlanned.lastUpdateUserId = request.user.sub; kpiUserPlanned.lastUpdateFullName = request.user.name; await this.kpiUserPlannedRepository.save(kpiUserPlanned); return new HttpSuccess(kpiUserPlanned.id); } /** * API แก้ไขงานตามแผนปฏิบัติราชการประจำปี * * @summary - แก้ไขงานตามแผนปฏิบัติราชการประจำปี #49 * * @param {string} id Id งานตามแผนปฏิบัติราชการประจำปี */ @Put("{id}") async editKpiUserPlanned( @Path() id: string, @Body() requestBody: UpdateKpiUserPlanned, @Request() request: { user: Record }, ) { const kpiUserPlanned = await this.kpiUserPlannedRepository.findOne({ where: { id } }); if (!kpiUserPlanned) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้"); } kpiUserPlanned.lastUpdateUserId = request.user.sub; kpiUserPlanned.lastUpdateFullName = request.user.name; this.kpiUserPlannedRepository.merge(kpiUserPlanned, requestBody); await this.kpiUserPlannedRepository.save(kpiUserPlanned); return new HttpSuccess(kpiUserPlanned.id); } /** * API ลบงานตามแผนปฏิบัติราชการประจำปี * * @summary - ลบงานตามแผนปฏิบัติราชการประจำปี #50 * * @param {string} id Id ตำแหน่ง */ @Delete("{id}") async deleteKpiUserPlanned(@Path() id: string) { const delKpiUserPlanned = await this.kpiUserPlannedRepository.findOne({ where: { id } }); if (!delKpiUserPlanned) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้"); } await this.kpiUserPlannedRepository.remove(delKpiUserPlanned); return new HttpSuccess(); } // /** // * API รายละเอียดงานตามแผนปฏิบัติราชการประจำปี // * // * @summary - รายละเอียดงานตามแผนปฏิบัติราชการประจำปี #51 // * // * @param {string} id Id งานตามแผนปฏิบัติราชการประจำปี // */ // @Get("{id}") // async GetKpiUserPlannedDetail(@Path() id: string) { // const getKpiUserPlanned = await this.kpiUserPlannedRepository.findOne({ // select: ["id", "kpiUserPlannedName", "kpiUserPlannedRank"], // relations: ["posLevels"], // where: { id: id }, // }); // if (!getKpiUserPlanned) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้"); // } // const mapGetKpiUserPlanned = { // id: getKpiUserPlanned.id, // kpiUserPlannedName: getKpiUserPlanned.kpiUserPlannedName, // kpiUserPlannedRank: getKpiUserPlanned.kpiUserPlannedRank, // posLevels: getKpiUserPlanned.posLevels // .sort((a, b) => a.posLevelRank - b.posLevelRank) // .map((posLevel) => ({ // id: posLevel.id, // posLevelName: posLevel.posLevelName, // posLevelRank: posLevel.posLevelRank, // posLevelAuthority: posLevel.posLevelAuthority, // })), // }; // return new HttpSuccess(mapGetKpiUserPlanned); // } /** * API รายการงานตามแผนปฏิบัติราชการประจำปี * * @summary - รายการงานตามแผนปฏิบัติราชการประจำปี #29 * */ @Get() async GetKpiUserPlanned() { const kpiUserPlanned = await this.kpiUserPlannedRepository.find({ relations: ["kpiPlan","kpiUserEvaluation"], order: { createdAt: "ASC" }, }); // if (!kpiUserPlanned) { // return new HttpSuccess([]); // } const mapKpiUserPlanned = kpiUserPlanned.map((item) => ({ id: item.id, evaluationId: item.kpiUserEvaluation.id, indicatorId: item.kpiPlan.id, includingName: item.kpiPlan.includingName, target: item.target, weight: item.weight, unit: item.unit, meaning: item.meaning, formula: item.formula, point: item.point, achievement: item.point === 1 ? "ระดับ 1" : item.point === 2 ? "ระดับ 2" : item.point === 3 ? "ระดับ 3" : item.point === 3 ? "ระดับ 4" : item.point === 3 ? "ระดับ 5" : null, achievement1: item.kpiPlan.achievement1, achievement2: item.kpiPlan.achievement2, achievement3: item.kpiPlan.achievement3, achievement4: item.kpiPlan.achievement4, achievement5: item.kpiPlan.achievement5, })); return new HttpSuccess(mapKpiUserPlanned); } }