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 { KpiUserRole, CreateKpiUserRole, UpdateKpiUserRole, KpiUserRoleDataPoint, } from "../entities/kpiUserRole"; import HttpError from "../interfaces/http-error"; import { Not } from "typeorm"; import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; import { KpiRole } from "../entities/kpiRole"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { addLogSequence, setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/kpi/user/achievement/role") @Tags("KpiUserRole") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class KpiUserRoleController extends Controller { private kpiUserRoleRepository = AppDataSource.getRepository(KpiUserRole); private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); private kpiRoleRepository = AppDataSource.getRepository(KpiRole); /** * API เพิ่มงานตามหน้าที่ความรับผิดชอบหลัก * * @summary - เพิ่มงานตามหน้าที่ความรับผิดชอบหลัก #48 * */ @Post() async createKpiUserRole( @Body() requestBody: CreateKpiUserRole, @Request() request: RequestWithUser, ) { await new permission().PermissionCreate(request, "SYS_KPI_LIST"); const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ where: { id: requestBody.kpiUserEvaluationId }, }); if (!chkUserEvaluation) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); } const chkKpiRole = await this.kpiRoleRepository.findOne({ where: { id: requestBody.kpiRoleId }, }); if (!chkKpiRole) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก", ); } const kpiUserRole = Object.assign(new KpiUserRole(), requestBody); if (!kpiUserRole) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const chk_indicator = await this.kpiUserRoleRepository.findOne({ where: { kpiUserEvaluationId: requestBody.kpiUserEvaluationId, kpiRoleId: requestBody.kpiRoleId, }, }); if (chk_indicator) { throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", ); } const before = null; kpiUserRole.startDate = requestBody.startDate == undefined ? null : requestBody.startDate; kpiUserRole.endDate = requestBody.endDate == undefined ? null : requestBody.endDate; kpiUserRole.createdUserId = request.user.sub; kpiUserRole.createdFullName = request.user.name; kpiUserRole.lastUpdateUserId = request.user.sub; kpiUserRole.lastUpdateFullName = request.user.name; await this.kpiUserRoleRepository.save(kpiUserRole, { data: request }); setLogDataDiff(request, { before, after: kpiUserRole }); return new HttpSuccess(kpiUserRole.id); } /** * API แก้ไขงานตามหน้าที่ความรับผิดชอบหลัก * * @summary - แก้ไขงานตามหน้าที่ความรับผิดชอบหลัก #49 * * @param {string} id Id งานตามหน้าที่ความรับผิดชอบหลัก */ @Put("{id}") async editKpiUserRole( @Path() id: string, @Body() requestBody: UpdateKpiUserRole, @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "SYS_KPI_LIST"); const kpiUserRole = await this.kpiUserRoleRepository.findOne({ where: { id } }); if (!kpiUserRole) { 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 chkKpiRole = await this.kpiRoleRepository.findOne({ where: { id: requestBody.kpiRoleId }, }); if (!chkKpiRole) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก", ); } const chk_indicator = await this.kpiUserRoleRepository.findOne({ where: { id: Not(id), kpiUserEvaluationId: requestBody.kpiUserEvaluationId, kpiRoleId: requestBody.kpiRoleId, }, }); if (chk_indicator) { throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", ); } const before = structuredClone(kpiUserRole); kpiUserRole.lastUpdateUserId = request.user.sub; kpiUserRole.lastUpdateFullName = request.user.name; Object.assign(kpiUserRole, requestBody); kpiUserRole.startDate = requestBody.startDate == undefined ? null : requestBody.startDate; kpiUserRole.endDate = requestBody.endDate == undefined ? null : requestBody.endDate; await this.kpiUserRoleRepository.save(kpiUserRole, { data: request }); setLogDataDiff(request, { before, after: kpiUserRole }); return new HttpSuccess(kpiUserRole.id); } /** * API ลบงานตามหน้าที่ความรับผิดชอบหลัก * * @summary - ลบงานตามหน้าที่ความรับผิดชอบหลัก #50 * * @param {string} id Id ตำแหน่ง */ @Delete("{id}") async deleteKpiUserRole(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionDelete(request, "SYS_KPI_LIST"); const delKpiUserRole = await this.kpiUserRoleRepository.findOne({ where: { id } }); if (!delKpiUserRole) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้"); } await this.kpiUserRoleRepository.remove(delKpiUserRole, { data: request }); return new HttpSuccess(); } /** * API รายละเอียดงานตามหน้าที่ความรับผิดชอบหลัก * * @summary - รายละเอียดงานตามหน้าที่ความรับผิดชอบหลัก #51 * * @param {string} id Id งานตามหน้าที่ความรับผิดชอบหลัก */ @Get("{id}") async GetKpiUserRoleDetail(@Request() request: RequestWithUser, @Path() id: string) { await new permission().PermissionGet(request, "SYS_KPI_LIST"); const getKpiUserRole = await this.kpiUserRoleRepository.findOne({ relations: ["kpiRole", "kpiUserEvaluation"], where: { id: id }, }); if (!getKpiUserRole) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้"); } const mapKpiUserRole = { id: getKpiUserRole.id, evaluationId: getKpiUserRole.kpiUserEvaluation.id, kpiRoleId: getKpiUserRole.kpiRole.id, including: getKpiUserRole.kpiRole.including, includingName: getKpiUserRole.kpiRole.includingName, target: getKpiUserRole.target, weight: getKpiUserRole.weight, unit: getKpiUserRole.unit, meaning: getKpiUserRole.meaning, formula: getKpiUserRole.formula, summary: getKpiUserRole.summary, point: getKpiUserRole.point, achievement1: getKpiUserRole.achievement1, achievement2: getKpiUserRole.achievement2, achievement3: getKpiUserRole.achievement3, achievement4: getKpiUserRole.achievement4, achievement5: getKpiUserRole.achievement5, documentInfoEvidence: getKpiUserRole.documentInfoEvidence, endDate: getKpiUserRole.endDate, startDate: getKpiUserRole.startDate, }; return new HttpSuccess(mapKpiUserRole); } /** * API รายการงานตามหน้าที่ความรับผิดชอบหลัก * * @summary - รายการงานตามหน้าที่ความรับผิดชอบหลัก #29 * */ @Get() async GetKpiUserRole(@Request() request: RequestWithUser, @Query("id") id: string) { await new permission().PermissionGet(request, "SYS_KPI_LIST"); const kpiUserRole = await this.kpiUserRoleRepository.find({ where: { kpiUserEvaluationId: id, }, relations: ["kpiRole", "kpiUserEvaluation"], order: { createdAt: "ASC" }, }); const mapKpiUserRole = kpiUserRole.map((item) => ({ id: item.id, evaluationId: item.kpiUserEvaluation.id, kpiRoleId: item.kpiRole.id, including: item.kpiRole.including, includingName: item.kpiRole.includingName, target: item.target, weight: item.weight, unit: item.unit, meaning: item.meaning, formula: item.formula, summary: item.summary, point: item.point, achievement: item.point === 1 ? item.kpiRole.achievement1 : item.point === 2 ? item.kpiRole.achievement2 : item.point === 3 ? item.kpiRole.achievement3 : item.point === 4 ? item.kpiRole.achievement4 : item.point === 5 ? item.kpiRole.achievement5 : null, achievement1: item.kpiRole.achievement1, achievement2: item.kpiRole.achievement2, achievement3: item.kpiRole.achievement3, achievement4: item.kpiRole.achievement4, achievement5: item.kpiRole.achievement5, })); return new HttpSuccess(mapKpiUserRole); } /** * API กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี * * @summary กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี * * */ @Post("point") async CreateKpiUserRolePoint( @Body() requestBody: KpiUserRoleDataPoint[], @Request() request: RequestWithUser, ) { await new permission().PermissionCreate(request, "SYS_KPI_LIST"); for (const item of requestBody) { const kpiUserRole = await this.kpiUserRoleRepository.findOne({ where: { id: item.id }, }); if (!kpiUserRole) { throw new HttpError( HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้: ${item.id}`, ); } const before = null; this.kpiUserRoleRepository.merge(kpiUserRole, item); kpiUserRole.lastUpdateUserId = request.user.sub; kpiUserRole.lastUpdateFullName = request.user.name; await this.kpiUserRoleRepository.save(kpiUserRole, { data: request }); setLogDataDiff(request, { before, after: kpiUserRole }); } return new HttpSuccess(); } }