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, KpiUserPlannedDataPoint, } 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({ relations: ["kpiPlan", "kpiUserEvaluation"], where: { id: id }, }); if (!getKpiUserPlanned) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้"); } const mapGetKpiUserPlanned = { id: getKpiUserPlanned.id, evaluationId: getKpiUserPlanned.kpiUserEvaluation.id, kpiPlanId: getKpiUserPlanned.kpiPlan.id, including: getKpiUserPlanned.kpiPlan.including, //รหัสตัวชี้วัด includingName: getKpiUserPlanned.kpiPlan.includingName, //ชื่อตัวชี้วัด target: getKpiUserPlanned.target, weight: getKpiUserPlanned.weight, unit: getKpiUserPlanned.unit, meaning: getKpiUserPlanned.meaning, formula: getKpiUserPlanned.formula, }; return new HttpSuccess(mapGetKpiUserPlanned); } /** * API รายการงานตามแผนปฏิบัติราชการประจำปี * * @summary - รายการงานตามแผนปฏิบัติราชการประจำปี #29 * */ @Get() async GetKpiUserPlanned(@Query("id") id: string) { const kpiUserPlanned = await this.kpiUserPlannedRepository.find({ where: { kpiUserEvaluationId: id, }, relations: ["kpiPlan", "kpiUserEvaluation"], order: { createdAt: "ASC" }, }); 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 ? item.kpiPlan.achievement1 : item.point === 2 ? item.kpiPlan.achievement2 : item.point === 3 ? item.kpiPlan.achievement3 : item.point === 4 ? item.kpiPlan.achievement4 : item.point === 5 ? item.kpiPlan.achievement5 : 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); } /** * API กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี * * @summary กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี * * */ @Post("point") async CreateKpiUserPlannedPoint( @Body() requestBody: KpiUserPlannedDataPoint[], @Request() request: { user: Record }, ) { for (const item of requestBody) { const kpiUserPlanned = await this.kpiUserPlannedRepository.findOne({ where: { id: item.id }, }); if (!kpiUserPlanned) { throw new HttpError( HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้: ${item.id}`, ); } this.kpiUserPlannedRepository.merge(kpiUserPlanned, item); kpiUserPlanned.lastUpdateUserId = request.user.sub; kpiUserPlanned.lastUpdateFullName = request.user.name; await this.kpiUserPlannedRepository.save(kpiUserPlanned); } return new HttpSuccess(); } }