import { Controller, Get, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, SuccessResponse, Response, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import { KpiUserSpecial, CreateKpiUserSpecial, UpdateKpiUserSpecial, KpiUserSpecialDataPoint, } from "../entities/kpiUserSpecial"; import HttpError from "../interfaces/http-error"; import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; import { Not } from "typeorm"; @Route("api/v1/kpi/user/achievement/special") @Tags("KpiUserSpecial") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class KpiUserSpecialController extends Controller { private kpiUserSpecialRepository = AppDataSource.getRepository(KpiUserSpecial); private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); /** * API เพิ่มงานที่ได้รับมอบหมายพิเศษ * * @summary - เพิ่มงานที่ได้รับมอบหมายพิเศษ # * */ @Post() async createKpiUserSpecial( @Body() requestBody: CreateKpiUserSpecial, @Request() request: { user: Record }, ) { const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ where: { id: requestBody.kpiUserEvaluationId }, }); if (!chkUserEvaluation) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); } const kpiUserSpecial = Object.assign(new KpiUserSpecial(), requestBody); if (!kpiUserSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const chk_indicator = await this.kpiUserSpecialRepository.findOne({ where: { kpiUserEvaluationId: requestBody.kpiUserEvaluationId, }, }); if ( (chk_indicator && chk_indicator.including == requestBody.including) || (chk_indicator && chk_indicator.includingName == requestBody.includingName) ) { throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", ); } kpiUserSpecial.createdUserId = request.user.sub; kpiUserSpecial.createdFullName = request.user.name; kpiUserSpecial.lastUpdateUserId = request.user.sub; kpiUserSpecial.lastUpdateFullName = request.user.name; await this.kpiUserSpecialRepository.save(kpiUserSpecial); return new HttpSuccess(kpiUserSpecial.id); } /** * API แก้ไขงานที่ได้รับมอบหมายพิเศษ * * @summary - แก้ไขงานที่ได้รับมอบหมายพิเศษ # * * @param {string} id Id งานที่ได้รับมอบหมายพิเศษ */ @Put("{id}") async editKpiUserSpecial( @Path() id: string, @Body() requestBody: UpdateKpiUserSpecial, @Request() request: { user: Record }, ) { const kpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id } }); if (!kpiUserSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้"); } const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ where: { id: requestBody.kpiUserEvaluationId }, }); if (!chkUserEvaluation) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); } const chk_indicator = await this.kpiUserSpecialRepository.findOne({ where: { id: Not(id), kpiUserEvaluationId: requestBody.kpiUserEvaluationId, }, }); if ( (chk_indicator && chk_indicator.including == requestBody.including) || (chk_indicator && chk_indicator.includingName == requestBody.includingName) ) { throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", ); } kpiUserSpecial.lastUpdateUserId = request.user.sub; kpiUserSpecial.lastUpdateFullName = request.user.name; Object.assign(kpiUserSpecial, requestBody); await this.kpiUserSpecialRepository.save(kpiUserSpecial); return new HttpSuccess(kpiUserSpecial.id); } /** * API ลบงานที่ได้รับมอบหมายพิเศษ * * @summary - ลบงานที่ได้รับมอบหมายพิเศษ # * */ @Delete("{id}") async deleteKpiUserSpecial(@Path() id: string) { const delKpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id } }); if (!delKpiUserSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้"); } await this.kpiUserSpecialRepository.remove(delKpiUserSpecial); return new HttpSuccess(); } /** * API รายละเอียดงานที่ได้รับมอบหมายพิเศษ * * @summary - รายละเอียดงานที่ได้รับมอบหมายพิเศษ # * * @param {string} id Id งานที่ได้รับมอบหมายพิเศษ */ @Get("{id}") async GetKpiUserSpecialDetail(@Path() id: string) { const getKpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ relations: ["kpiUserEvaluation"], where: { id: id }, }); if (!getKpiUserSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้"); } const mapKpiUserSpecial = { id: getKpiUserSpecial.id, evaluationId: getKpiUserSpecial.kpiUserEvaluation.id, including: getKpiUserSpecial.including, includingName: getKpiUserSpecial.includingName, target: getKpiUserSpecial.target, weight: getKpiUserSpecial.weight, unit: getKpiUserSpecial.unit, meaning: getKpiUserSpecial.meaning, formula: getKpiUserSpecial.formula, point: getKpiUserSpecial.point, achievement: getKpiUserSpecial.point === 1 ? getKpiUserSpecial.achievement1 : getKpiUserSpecial.point === 2 ? getKpiUserSpecial.achievement2 : getKpiUserSpecial.point === 3 ? getKpiUserSpecial.achievement3 : getKpiUserSpecial.point === 4 ? getKpiUserSpecial.achievement4 : getKpiUserSpecial.point === 5 ? getKpiUserSpecial.achievement5 : null, achievement1: getKpiUserSpecial.achievement1, achievement2: getKpiUserSpecial.achievement2, achievement3: getKpiUserSpecial.achievement3, achievement4: getKpiUserSpecial.achievement4, achievement5: getKpiUserSpecial.achievement5, }; return new HttpSuccess(mapKpiUserSpecial); } /** * API รายการงานที่ได้รับมอบหมายพิเศษ * * @summary - รายการงานที่ได้รับมอบหมายพิเศษ # * */ @Get() async GetKpiUserSpecial(@Query("id") id: string) { const kpiUserSpecial = await this.kpiUserSpecialRepository.find({ where: { kpiUserEvaluationId: id, }, relations: ["kpiUserEvaluation"], order: { createdAt: "ASC" }, }); const mapKpiUserSpecial = kpiUserSpecial.map((item) => ({ id: item.id, evaluationId: item.kpiUserEvaluation.id, including: item.including, includingName: item.includingName, target: item.target, weight: item.weight, unit: item.unit, meaning: item.meaning, formula: item.formula, point: item.point, achievement: item.point === 1 ? item.achievement1 : item.point === 2 ? item.achievement2 : item.point === 3 ? item.achievement3 : item.point === 4 ? item.achievement4 : item.point === 5 ? item.achievement5 : null, achievement1: item.achievement1, achievement2: item.achievement2, achievement3: item.achievement3, achievement4: item.achievement4, achievement5: item.achievement5, })); return new HttpSuccess(mapKpiUserSpecial); } /** * API กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี * * @summary กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี * * */ @Post("point") async CreateKpiUserSpecialPoint( @Body() requestBody: KpiUserSpecialDataPoint[], @Request() request: { user: Record }, ) { for (const item of requestBody) { const kpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id: item.id }, }); if (!kpiUserSpecial) { throw new HttpError( HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้: ${item.id}`, ); } this.kpiUserSpecialRepository.merge(kpiUserSpecial, item); kpiUserSpecial.lastUpdateUserId = request.user.sub; kpiUserSpecial.lastUpdateFullName = request.user.name; await this.kpiUserSpecialRepository.save(kpiUserSpecial); } return new HttpSuccess(); } }