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 HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import Extension from "../interfaces/extension"; import { KpiPeriod } from "../entities/kpiPeriod"; import { KpiUserEvaluation, createKpiUserEvaluation, updateKpiUserCheckEvaluation, updateKpiUserEvaluation, updateKpiUserPointEvaluation, updateKpiUserStatusEvaluation, updateKpiUserReqEditEvaluation, updateKpiUserResultEvaluation, } from "../entities/kpiUserEvaluation"; import { In, Brackets, IsNull, Not, MoreThanOrEqual } from "typeorm"; import CallAPI from "../interfaces/call-api"; import { KpiCapacity } from "../entities/kpiCapacity"; import { Position } from "../entities/position"; import { KpiLink } from "../entities/kpiLink"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; import { KpiUserRejectAgreement } from "../entities/kpiUserRejectAgreement"; import { KpiUserRejectResult } from "../entities/kpiUserRejectResult"; @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); private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity); private kpiPositionRepository = AppDataSource.getRepository(Position); private kpiLinkRepository = AppDataSource.getRepository(KpiLink); private kpiUserRejectAgreementRepository = AppDataSource.getRepository(KpiUserRejectAgreement); private kpiUserRejectResultRepository = AppDataSource.getRepository(KpiUserRejectResult); /** * API * * @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคลทั้งหมด * */ @Post("admin") async listKpiAdminEvaluation( @Request() request: RequestWithUser, @Body() requestBody: { page: number; pageSize: number; kpiPeriodId?: string; keyword?: string; status?: string | null; results?: string | null; reqedit?: string | null; evaluating?: boolean | null; }, ) { // await new permission().PermissionList(request, "SYS_KPI_ROUND"); let profileId: any = null; await new CallAPI() .GetData(request, "/org/profile/keycloak/position") .then((x) => { profileId = x.profileId; }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ"); }); // let role = "EVALUATOR"; // if (profileId == item.commanderId) { // role = "COMMANDER"; // } else if (profileId == item.commanderHighId) { // role = "COMMANDERHIGH"; // } const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .andWhere( requestBody.kpiPeriodId ? "kpiUserEvaluation.kpiPeriodId LIKE :kpiPeriodId" : "1=1", { kpiPeriodId: requestBody.kpiPeriodId, }, ) .andWhere( requestBody.status != null && requestBody.status != undefined ? requestBody.status.trim().toUpperCase() == "NEW" ? `kpiUserEvaluation.evaluationStatus LIKE CASE WHEN kpiUserEvaluation.evaluatorId = "${profileId}" THEN "NEW_EVALUATOR" WHEN kpiUserEvaluation.commanderId = "${profileId}" THEN "NEW_COMMANDER" WHEN kpiUserEvaluation.commanderHighId = "${profileId}" THEN "NEW_COMMANDER_HIGH" ELSE "${requestBody.status.trim().toUpperCase()}" END` : requestBody.status.trim().toUpperCase() == "EVALUATING_EVALUATOR" || requestBody.status.trim().toUpperCase() == "EVALUATING" ? "kpiUserEvaluation.evaluationStatus LIKE :status" : requestBody.status.trim().toUpperCase() == "SUMMARY" ? "kpiUserEvaluation.evaluationStatus LIKE :status" : "kpiUserEvaluation.evaluationStatus LIKE :status" : "1=1", { status: requestBody.status == null || requestBody.status == undefined ? null : requestBody.status.trim().toUpperCase() == "EVALUATING" || requestBody.status.trim().toUpperCase() == "EVALUATING_EVALUATOR" || requestBody.status.trim().toUpperCase() == "SUMMARY" ? requestBody.status.trim().toUpperCase() == "SUMMARY" ? `%${requestBody.status.trim().toUpperCase()}%` : `%EVALUATING%` : requestBody.status.trim().toUpperCase(), }, ) .andWhere( requestBody.results != null && requestBody.results != undefined ? "kpiUserEvaluation.evaluationResults LIKE :results" : "1=1", { results: requestBody.results == null || requestBody.results == undefined ? null : requestBody.results.trim().toUpperCase(), }, ) .andWhere( requestBody.reqedit != null && requestBody.reqedit != undefined ? requestBody.reqedit.trim().toUpperCase() == "NEW" ? `kpiUserEvaluation.evaluationReqEdit LIKE CASE WHEN kpiUserEvaluation.evaluatorId = "${profileId}" THEN "EVALUATOR" WHEN kpiUserEvaluation.commanderId = "${profileId}" THEN "COMMANDER" WHEN kpiUserEvaluation.commanderHighId = "${profileId}" THEN "COMMANDER_HIGH" ELSE "${requestBody.reqedit.trim().toUpperCase()}" END` : "kpiUserEvaluation.evaluationReqEdit LIKE :reqedit" : "1=1", { reqedit: requestBody.reqedit == null || requestBody.reqedit == undefined ? null : requestBody.reqedit.trim().toUpperCase(), }, ) .andWhere( new Brackets((qb) => { qb.orWhere("kpiUserEvaluation.evaluatorId LIKE :profileId", { profileId: `${profileId}`, }) .orWhere("kpiUserEvaluation.commanderId LIKE :profileId", { profileId: `${profileId}`, }) .orWhere("kpiUserEvaluation.commanderHighId LIKE :profileId", { profileId: `${profileId}`, }); }), ) .andWhere( new Brackets((qb) => { qb.orWhere( "CONCAT(kpiUserEvaluation.prefix, kpiUserEvaluation.firstName, ' ', kpiUserEvaluation.lastName) LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }, ); // .orWhere("kpiUserEvaluation.firstName LIKE :keyword", { // keyword: `%${requestBody.keyword}%`, // }) // .orWhere("kpiUserEvaluation.lastName LIKE :keyword", { // keyword: `%${requestBody.keyword}%`, // }); }), ) .orderBy("kpiUserEvaluation.createdAt", "DESC") .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.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, evaluationStatus: item.evaluationStatus, evaluationResults: item.evaluationResults, createdAt: item.createdAt, evaluatorId: item.evaluatorId, commanderId: item.commanderId, commanderHighId: item.commanderHighId, reasonReject: item.reasonReject, isReject: item.reasonReject ? true : false, })); return new HttpSuccess({ data: mapData, total }); } /** * API * * @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคลทั้งหมด * */ @Post("list-announce") async listKpiListEvaluationAnnounce( @Request() request: RequestWithUser, @Body() requestBody: { page: number; pageSize: number; kpiPeriodId?: string; keyword?: string; status?: string | null; results?: string | null; reqedit?: string | null; evaluating?: boolean | null; }, ) { // await new permission().PermissionList(request, "SYS_RESULT"); let conditionFullName = "CONCAT(kpiUserEvaluation.prefix, kpiUserEvaluation.firstName, ' ', kpiUserEvaluation.lastName) LIKE :keyword"; let _data = await new permission().PermissionOrgList(request, "SYS_RESULT"); const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .andWhere( _data.root != undefined && _data.root != null ? _data.root[0] != null ? `kpiUserEvaluation.orgId IN (:...root)` : `kpiUserEvaluation.orgId is null` : "1=1", { root: _data.root, }, ) .andWhere( _data.child1 != undefined && _data.child1 != null ? _data.child1[0] != null ? `kpiUserEvaluation.child1Id IN (:...child1)` : `kpiUserEvaluation.child1Id is null` : "1=1", { child1: _data.child1, }, ) .andWhere( _data.child2 != undefined && _data.child2 != null ? _data.child2[0] != null ? `kpiUserEvaluation.child2Id IN (:...child2)` : `kpiUserEvaluation.child2Id is null` : "1=1", { child2: _data.child2, }, ) .andWhere( _data.child3 != undefined && _data.child3 != null ? _data.child3[0] != null ? `kpiUserEvaluation.child3Id IN (:...child3)` : `kpiUserEvaluation.child3Id is null` : "1=1", { child3: _data.child3, }, ) .andWhere( _data.child4 != undefined && _data.child4 != null ? _data.child4[0] != null ? `kpiUserEvaluation.child4Id IN (:...child4)` : `kpiUserEvaluation.child4Id is null` : "1=1", { child4: _data.child4, }, ) .andWhere( requestBody.kpiPeriodId ? "kpiUserEvaluation.kpiPeriodId LIKE :kpiPeriodId" : "1=1", { kpiPeriodId: requestBody.kpiPeriodId, }, ) .andWhere( requestBody.status != null && requestBody.status != undefined ? "kpiUserEvaluation.evaluationstatus LIKE :status" : "1=1", { status: requestBody.status == null || requestBody.status == undefined ? null : requestBody.status.trim().toUpperCase(), }, ) .andWhere( requestBody.results != null && requestBody.results != undefined ? "kpiUserEvaluation.evaluationResults LIKE :results" : "1=1", { results: requestBody.results == null || requestBody.results == undefined ? null : requestBody.results.trim().toUpperCase(), }, ) .andWhere( new Brackets((qb) => { qb.orWhere("kpiUserEvaluation.prefix LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.firstName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.lastName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.org LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.position LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.posTypeName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.posLevelName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere(conditionFullName, { keyword: `%${requestBody.keyword}%`, }); }), ) .orderBy("kpiUserEvaluation.createdAt", "ASC") .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); const mapData = kpiUserEvaluation.map((item) => { const fullNameParts = [item.child4, item.child3, item.child2, item.child1, item.org]; const organization = fullNameParts .filter((part) => part !== undefined && part !== null) .join("/"); return { id: item.id, profileId: item.profileId, prefix: item.prefix, firstname: item.firstName, lastname: item.lastName, kpiPeriodId: item.kpiPeriodId, evaluationStatus: item.evaluationStatus, evaluationResults: item.evaluationResults, createdAt: item.createdAt, evaluatorId: item.evaluatorId, commanderId: item.commanderId, commanderHighId: item.commanderHighId, root: item.org ? item.org : null, rootId: item.orgId ? item.orgId : null, position: item.position ? item.position : null, // posTypeId: item.posTypeId, posTypeName: item.posTypeName ? item.posTypeName : null, // posLevelId: item.posLevelId, posLevelName: item.posLevelName ? item.posLevelName : null, organization: organization ? organization : null, }; }); return new HttpSuccess({ data: mapData, total }); } /** * API * * @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคลทั้งหมด * */ @Post("list") async listKpiListEvaluation( @Request() request: RequestWithUser, @Body() requestBody: { page: number; pageSize: number; kpiPeriodId?: string; keyword?: string; status?: string | null; results?: string | null; }, ) { await new permission().PermissionList(request, "SYS_KPI_LIST"); let conditionFullName = "CONCAT(kpiUserEvaluation.prefix, kpiUserEvaluation.firstName, ' ', kpiUserEvaluation.lastName) LIKE :keyword"; const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod") .andWhere( requestBody.kpiPeriodId ? "kpiUserEvaluation.kpiPeriodId LIKE :kpiPeriodId" : "1=1", { kpiPeriodId: requestBody.kpiPeriodId, }, ) .andWhere( requestBody.status != null && requestBody.status != undefined ? "kpiUserEvaluation.evaluationstatus LIKE :status" : "1=1", { status: requestBody.status == null || requestBody.status == undefined ? null : requestBody.status.trim().toUpperCase(), }, ) .andWhere( requestBody.results != null && requestBody.results != undefined ? "kpiUserEvaluation.evaluationResults LIKE :results" : "1=1", { results: requestBody.results == null || requestBody.results == undefined ? null : requestBody.results.trim().toUpperCase(), }, ) .andWhere( new Brackets((qb) => { qb.orWhere("kpiUserEvaluation.prefix LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.firstName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.lastName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.org LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.position LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.posTypeName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere("kpiUserEvaluation.posLevelName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, }) .orWhere(conditionFullName, { keyword: `%${requestBody.keyword}%`, }); }), ) .orderBy("kpiPeriod.year", "DESC") .addOrderBy("kpiUserEvaluation.createdAt", "DESC") .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); const mapData = kpiUserEvaluation.map((item) => { const fullNameParts = [item.child4, item.child3, item.child2, item.child1, item.org]; const organization = fullNameParts .filter((part) => part !== undefined && part !== null) .join("/"); return { id: item.id, profileId: item.profileId, prefix: item.prefix, firstname: item.firstName, lastname: item.lastName, kpiPeriodId: item.kpiPeriodId, evaluationStatus: item.evaluationStatus, evaluationResults: item.evaluationResults, createdAt: item.createdAt, evaluatorId: item.evaluatorId, commanderId: item.commanderId, commanderHighId: item.commanderHighId, root: item.org ? item.org : null, rootId: item.orgId ? item.orgId : null, position: item.position ? item.position : null, // posTypeId: item.posTypeId, posTypeName: item.posTypeName ? item.posTypeName : null, // posLevelId: item.posLevelId, posLevelName: item.posLevelName ? item.posLevelName : null, organization: organization ? organization : null, }; }); return new HttpSuccess({ data: mapData, total }); } /** * API สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * */ @Post() async CreateKpiUserEvaluation( @Body() requestBody: createKpiUserEvaluation, @Request() request: RequestWithUser, ) { // await new permission().PermissionCreate(request, "SYS_KPI_LIST"); const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: requestBody.kpiPeriodId }, }); if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } let isProbation = false; const kpiUserEvaluation = Object.assign(new KpiUserEvaluation(), requestBody); if (requestBody.profileId != undefined && requestBody.profileId != null) { await new CallAPI() .GetData(request, "/org/profile/profileid/position/" + requestBody.profileId) .then((x) => { kpiUserEvaluation.profileId = x.profileId; kpiUserEvaluation.prefix = x.prefix; kpiUserEvaluation.firstName = x.firstName; kpiUserEvaluation.lastName = x.lastName; kpiUserEvaluation.position = x.position; kpiUserEvaluation.posLevelName = x.posLevelName; kpiUserEvaluation.posTypeName = x.posTypeName; kpiUserEvaluation.posExecutiveName = x.posExecutiveName; kpiUserEvaluation.isProbation = x.isProbation; kpiUserEvaluation.org = x.root; kpiUserEvaluation.orgId = x.rootId; kpiUserEvaluation.child1 = x.child1; kpiUserEvaluation.child1Id = x.child1Id; kpiUserEvaluation.child2 = x.child2; kpiUserEvaluation.child2Id = x.child2Id; kpiUserEvaluation.child3 = x.child3; kpiUserEvaluation.child3Id = x.child3Id; kpiUserEvaluation.child4 = x.child4; kpiUserEvaluation.child4Id = x.child4Id; isProbation = x.isProbation; }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ"); }); } else { 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; kpiUserEvaluation.position = x.position; kpiUserEvaluation.posLevelName = x.posLevelName; kpiUserEvaluation.posTypeName = x.posTypeName; kpiUserEvaluation.posExecutiveName = x.posExecutiveName; kpiUserEvaluation.isProbation = x.isProbation; kpiUserEvaluation.org = x.root; kpiUserEvaluation.orgId = x.rootId; kpiUserEvaluation.child1 = x.child1; kpiUserEvaluation.child1Id = x.child1Id; kpiUserEvaluation.child2 = x.child2; kpiUserEvaluation.child2Id = x.child2Id; kpiUserEvaluation.child3 = x.child3; kpiUserEvaluation.child3Id = x.child3Id; kpiUserEvaluation.child4 = x.child4; kpiUserEvaluation.child4Id = x.child4Id; isProbation = x.isProbation; }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ"); }); } if (requestBody.evaluatorId) { await new CallAPI() .GetData(request, "/org/profile/profileid/position/" + requestBody.evaluatorId) .then((x) => { kpiUserEvaluation.prefixEvaluator = x.prefix; kpiUserEvaluation.firstNameEvaluator = x.firstName; kpiUserEvaluation.lastNameEvaluator = x.lastName; kpiUserEvaluation.positionEvaluator = x.position; kpiUserEvaluation.posLevelNameEvaluator = x.posLevelName; kpiUserEvaluation.posTypeNameEvaluator = x.posTypeName; kpiUserEvaluation.orgEvaluator = x.root; }); } if (requestBody.commanderId) { await new CallAPI() .GetData(request, "/org/profile/profileid/position/" + requestBody.commanderId) .then((x) => { kpiUserEvaluation.prefixCommander = x.prefix; kpiUserEvaluation.firstNameCommander = x.firstName; kpiUserEvaluation.lastNameCommander = x.lastName; kpiUserEvaluation.positionCommander = x.position; }); } if (requestBody.commanderHighId) { await new CallAPI() .GetData(request, "/org/profile/profileid/position/" + requestBody.commanderHighId) .then((x) => { kpiUserEvaluation.prefixCommanderHigh = x.prefix; kpiUserEvaluation.firstNameCommanderHigh = x.firstName; kpiUserEvaluation.lastNameCommanderHigh = x.lastName; kpiUserEvaluation.positionCommanderHigh = x.position; }); } const before = null; kpiUserEvaluation.evaluationStatus = "NEW"; kpiUserEvaluation.evaluationResults = "PENDING"; kpiUserEvaluation.createdUserId = request.user.sub; kpiUserEvaluation.createdFullName = request.user.name; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.createdAt = new Date(); kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); enum CapacityType { HEAD = "HEAD", GROUP = "GROUP", } // MAIN CAPACITY const mainCapacities = await this.kpiCapacityRepository.find({ where: { type: CapacityType.HEAD }, }); let level: any = null; if (kpiUserEvaluation.posTypeName == "บริหาร" && kpiUserEvaluation.posLevelName == "สูง") level = "5"; if (kpiUserEvaluation.posTypeName == "บริหาร" && kpiUserEvaluation.posLevelName == "ต้น") level = "4"; if (kpiUserEvaluation.posTypeName == "อำนวยการ" && kpiUserEvaluation.posLevelName == "สูง") level = "4"; if (kpiUserEvaluation.posTypeName == "อำนวยการ" && kpiUserEvaluation.posLevelName == "ต้น") level = "3"; if ( kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ทรงคุณวุฒิ" ) level = "5"; if (kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "เชี่ยวชาญ") level = "4"; if ( kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ชำนาญการพิเศษ" ) level = "3"; if (kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ชำนาญการ") level = "2"; if ( kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ปฏิบัติการ" ) level = "1"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "ทักษะพิเศษ") level = "4"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "อาวุโส") level = "3"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "ชำนาญงาน") level = "2"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "ปฏิบัติงาน") level = "1"; for (const capacity of mainCapacities) { await new CallAPI() .PostData(request, "/kpi/user/capacity", { kpiUserEvaluationId: kpiUserEvaluation.id, kpiCapacityId: capacity.id, level: level, weight: 100, }) .catch((x) => { throw new HttpError(HttpStatusCode.NOT_FOUND, x); }); } // GROUP CAPACITY const findPosition = await this.kpiPositionRepository.findOne({ where: { name: kpiUserEvaluation.position, kpiLinkId: Not(IsNull()) }, }); let levelForGourp: any = null; if ( kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ทรงคุณวุฒิ" ) levelForGourp = "5"; if (kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "เชี่ยวชาญ") levelForGourp = "4"; if ( kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ชำนาญการพิเศษ" ) levelForGourp = "4"; if (kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ชำนาญการ") levelForGourp = "3"; if ( kpiUserEvaluation.posTypeName == "วิชาการ" && kpiUserEvaluation.posLevelName == "ปฏิบัติการ" ) levelForGourp = "2"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "ทักษะพิเศษ") levelForGourp = "4"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "อาวุโส") levelForGourp = "3"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "ชำนาญงาน") levelForGourp = "2"; if (kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "ปฏิบัติงาน") levelForGourp = "1"; kpiUserEvaluation.weightPoint1 = 80; kpiUserEvaluation.weightPoint2 = 20; kpiUserEvaluation.summaryWeight = 100; if (findPosition && findPosition.kpiLinkId && levelForGourp != null) { kpiUserEvaluation.weightPoint1 = 70; kpiUserEvaluation.weightPoint2 = 30; const findKpiLink = await this.kpiLinkRepository.findOne({ relations: ["kpiCapacitys"], where: { id: findPosition.kpiLinkId, }, }); if (findKpiLink) { let groupCapacity = findKpiLink.kpiCapacitys; for (const capacity of groupCapacity) { await new CallAPI() .PostData(request, "/kpi/user/capacity", { kpiUserEvaluationId: kpiUserEvaluation.id, kpiCapacityId: capacity.id, level: levelForGourp, weight: 100, }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถสร้างข้อมูลสมรรถนะได้"); }); } } } if (isProbation != false) { kpiUserEvaluation.weightPoint1 = 50; kpiUserEvaluation.weightPoint2 = 50; } await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขคนประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("check/{id}") async updateKpiUserCheckEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserCheckEvaluation, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const before = structuredClone(kpiUserEvaluation); if (requestBody.evaluatorId) { await new CallAPI() .GetData(request, "/org/profile/profileid/position/" + requestBody.evaluatorId) .then((x) => { kpiUserEvaluation.prefixEvaluator = x.prefix; kpiUserEvaluation.firstNameEvaluator = x.firstName; kpiUserEvaluation.lastNameEvaluator = x.lastName; kpiUserEvaluation.positionEvaluator = x.position; kpiUserEvaluation.posLevelNameEvaluator = x.posLevelName; kpiUserEvaluation.posTypeNameEvaluator = x.posTypeName; kpiUserEvaluation.orgEvaluator = x.root; }); } if (requestBody.commanderId) { await new CallAPI() .GetData(request, "/org/profile/profileid/position/" + requestBody.commanderId) .then((x) => { kpiUserEvaluation.prefixCommander = x.prefix; kpiUserEvaluation.firstNameCommander = x.firstName; kpiUserEvaluation.lastNameCommander = x.lastName; kpiUserEvaluation.positionCommander = x.position; }); } if (requestBody.commanderHighId) { await new CallAPI() .GetData(request, "/org/profile/profileid/position/" + requestBody.commanderHighId) .then((x) => { kpiUserEvaluation.prefixCommanderHigh = x.prefix; kpiUserEvaluation.firstNameCommanderHigh = x.firstName; kpiUserEvaluation.lastNameCommanderHigh = x.lastName; kpiUserEvaluation.positionCommanderHigh = x.position; }); } kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); Object.assign(kpiUserEvaluation, requestBody); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขคนประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("point/{id}") async updateKpiUserPointEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserPointEvaluation, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } if (requestBody.summaryPoint != null) { if (requestBody.summaryPoint >= 90) { kpiUserEvaluation.evaluationResults = "EXCELLENT"; } else if (requestBody.summaryPoint >= 80) { kpiUserEvaluation.evaluationResults = "VERY_GOOD"; } else if (requestBody.summaryPoint >= 70) { kpiUserEvaluation.evaluationResults = "GOOD"; } else if (requestBody.summaryPoint >= 60) { kpiUserEvaluation.evaluationResults = "FAIR"; } else { kpiUserEvaluation.evaluationResults = "IMPROVEMENT"; } } else { kpiUserEvaluation.evaluationResults = "IMPROVEMENT"; } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); Object.assign(kpiUserEvaluation, requestBody); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: 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: RequestWithUser, ) { 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, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); Object.assign(kpiUserEvaluation, requestBody); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสถานะประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("edit/{id}") async updateKpiUserReqEditEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserReqEditEvaluation, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationReqEdit = requestBody.status.trim().toUpperCase(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสถานะประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("result/{id}") async updateKpiUserResultEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserResultEvaluation, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationResults = requestBody.status.trim().toUpperCase(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสถานะประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("status/{id}") async updateKpiUserStatusEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserStatusEvaluation, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } if (requestBody.status.trim().toUpperCase() == "NEW_EVALUATOR") { await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ส่งข้อตกลงการประเมินผลการปฏิบัติราชการระดับบุคคลให้อนุมัติ`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ส่งข้อตกลงการประเมินผลการปฏิบัติราชการระดับบุคคลให้อนุมัติ`, receiverUserId: kpiUserEvaluation.evaluatorId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); let _null: any = null; kpiUserEvaluation.reasonReject = _null; } else if (requestBody.status.trim().toUpperCase() == "EVALUATING_EVALUATOR") { await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} สรุปการประเมินผลการปฏิบัติราชการระดับบุคคล`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} สรุปการประเมินผลการปฏิบัติราชการระดับบุคคล`, receiverUserId: kpiUserEvaluation.evaluatorId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); } const before = structuredClone(kpiUserEvaluation); let _null: any = null; kpiUserEvaluation.reasonReject = _null; kpiUserEvaluation.evaluationStatus = requestBody.status.trim().toUpperCase(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสถานะประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("reject-agreement/{id}") async updateKpiUserStatusRejectAgreementEvaluation( @Path() id: string, @Body() requestBody: { reason: string; actor: string }, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ไม่อนุมัติเนื่องจาก: ${requestBody.reason}`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ไม่อนุมัติเนื่องจาก: ${requestBody.reason}`, receiverUserId: kpiUserEvaluation.profileId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(async () => {}) .catch((error) => { console.error("Error details:", error.response.data); }); const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationStatus = "NEW"; kpiUserEvaluation.reasonReject = requestBody.reason; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); let kpiReject = { kpiUserEvaluationId: kpiUserEvaluation.id, reason: requestBody.reason, actor: requestBody.actor, fullname: `${kpiUserEvaluation.prefixEvaluator}${kpiUserEvaluation.firstNameEvaluator} ${kpiUserEvaluation.lastNameEvaluator}`, profileId: kpiUserEvaluation.evaluatorId, createdUserId: request.user.sub, createdFullName: request.user.name, lastUpdateUserId: request.user.sub, lastUpdateFullName: request.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), }; await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); await this.kpiUserRejectAgreementRepository.save(kpiReject); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสถานะประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("reject-result/{id}") async updateKpiUserStatusRejectResultEvaluation( @Path() id: string, @Body() requestBody: { reason: string; actor: string }, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${kpiUserEvaluation.prefixEvaluator}${kpiUserEvaluation.firstNameEvaluator} ${kpiUserEvaluation.lastNameEvaluator} ไม่อนุมัติเนื่องจาก: ${requestBody.reason}`, body: `${kpiUserEvaluation.prefixEvaluator}${kpiUserEvaluation.firstNameEvaluator} ${kpiUserEvaluation.lastNameEvaluator} ไม่อนุมัติเนื่องจาก: ${requestBody.reason}`, receiverUserId: kpiUserEvaluation.evaluatorId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationStatus = "EVALUATING_EVALUATOR"; let _null: any = null; kpiUserEvaluation.isReasonCommander = _null; kpiUserEvaluation.reasonCommander = _null; kpiUserEvaluation.reasonReject = requestBody.reason; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); let kpiReject = { kpiUserEvaluationId: kpiUserEvaluation.id, reason: requestBody.reason, actor: requestBody.actor, fullname: `${kpiUserEvaluation.prefixEvaluator}${kpiUserEvaluation.firstNameEvaluator} ${kpiUserEvaluation.lastNameEvaluator}`, profileId: kpiUserEvaluation.evaluatorId, createdUserId: request.user.sub, createdFullName: request.user.name, lastUpdateUserId: request.user.sub, lastUpdateFullName: request.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), }; await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); await this.kpiUserRejectResultRepository.save(kpiReject); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสถานะประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Get("reject-agreement/{id}") async listKpiUserStatusRejectAgreementEvaluation( @Path() id: string, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, relations: ["kpiUserRejectAgreements"], order: { kpiUserRejectAgreements: { createdAt: "ASC" } }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } kpiUserEvaluation.kpiUserRejectAgreements; return new HttpSuccess(kpiUserEvaluation.kpiUserRejectAgreements); } /** * API แก้ไขสถานะประเมิน (USER) * * @summary แก้ไขคนประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Get("reject-result/{id}") async listKpiUserStatusRejectResultEvaluation( @Path() id: string, @Body() requestBody: { reason: string }, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, relations: ["kpiUserRejectResults"], order: { kpiUserRejectResults: { createdAt: "ASC" } }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } kpiUserEvaluation.kpiUserRejectResults; return new HttpSuccess(kpiUserEvaluation.kpiUserRejectResults); } /** * API รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) */ @Get("{id}") async GetKpiUserEvaluationById(@Path() id: string) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ relations: ["kpiPeriod"], where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const mapData = { id: kpiUserEvaluation.id, profileId: kpiUserEvaluation.profileId, prefix: kpiUserEvaluation.prefix, firstName: kpiUserEvaluation.firstName, lastName: kpiUserEvaluation.lastName, position: kpiUserEvaluation.position, posLevelName: kpiUserEvaluation.posLevelName, posTypeName: kpiUserEvaluation.posTypeName, posExecutiveName: kpiUserEvaluation.posExecutiveName, evaluationStatus: kpiUserEvaluation.evaluationStatus, evaluationResults: kpiUserEvaluation.evaluationResults, evaluationReqEdit: kpiUserEvaluation.evaluationReqEdit, isProbation: kpiUserEvaluation.isProbation, createdAt: kpiUserEvaluation.createdAt, evaluatorId: kpiUserEvaluation.evaluatorId, commanderId: kpiUserEvaluation.commanderId, commanderHighId: kpiUserEvaluation.commanderHighId, // plannedPoint: kpiUserEvaluation.plannedPoint, // rolePoint: kpiUserEvaluation.rolePoint, // specialPoint: kpiUserEvaluation.specialPoint, // capacityPoint: kpiUserEvaluation.capacityPoint, kpiPeriodId: kpiUserEvaluation.kpiPeriodId, totalPoint1: kpiUserEvaluation.totalPoint1, totalPoint2_1: kpiUserEvaluation.totalPoint2_1, totalPoint2_2: kpiUserEvaluation.totalPoint2_2, summaryPoint: kpiUserEvaluation.summaryPoint, isOpen: kpiUserEvaluation.isOpen, openDate: kpiUserEvaluation.openDate, root: kpiUserEvaluation.org, child1: kpiUserEvaluation.child1, child2: kpiUserEvaluation.child2, child3: kpiUserEvaluation.child3, child4: kpiUserEvaluation.child4, reasonReject: kpiUserEvaluation.reasonReject, isReject: kpiUserEvaluation.reasonReject ? true : false, year: kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.year, durationKPI: kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.durationKPI, }; return new HttpSuccess(mapData); } /** * API รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * */ @Get() async listKpiUserEvaluation( @Request() request: { user: Record }, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("kpiPeriodId") kpiPeriodId?: string, @Query("keyword") keyword?: string, @Query("status") status?: string, @Query("results") results?: string, ) { let profileId: any = null; await new CallAPI() .GetData(request, "/org/profile/keycloak/position") .then((x) => { profileId = x.profileId; }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ"); }); const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod") .andWhere(kpiPeriodId ? "kpiUserEvaluation.kpiPeriodId LIKE :kpiPeriodId" : "1=1", { kpiPeriodId: kpiPeriodId, }) // .andWhere({ createdUserId: request.user.sub }) .andWhere({ profileId: profileId }) .andWhere( status == null || status == undefined ? "1=1" : "kpiUserEvaluation.evaluationStatus LIKE :evaluationStatus", { evaluationStatus: status == undefined ? "" : status.trim().toUpperCase(), }, ) .andWhere( results == null || results == undefined ? "1=1" : "kpiUserEvaluation.evaluationResults LIKE :evaluationResults", { evaluationResults: results == undefined ? "" : results.trim().toUpperCase(), }, ) .orderBy("kpiUserEvaluation.createdAt", "DESC") .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, evaluationStatus: item.evaluationStatus, evaluationResults: item.evaluationResults, evaluatorId: item.evaluatorId, commanderId: item.commanderId, commanderHighId: item.commanderHighId, createdAt: item.createdAt, plannedPoint: item.plannedPoint, rolePoint: item.rolePoint, specialPoint: item.specialPoint, capacityPoint: item.capacityPoint, reasonReject: item.reasonReject, isReject: item.reasonReject ? true : false, year: item.kpiPeriod ? item.kpiPeriod.year : null, durationKPI: item.kpiPeriod ? item.kpiPeriod.durationKPI : null, })); return new HttpSuccess({ data: mapData, total }); } /** * API ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @summary ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) * * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) */ @Delete("{id}") async deleteKpiUserEvaluation(@Path() id: string, @Request() request: RequestWithUser) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation, { data: request }); return new HttpSuccess(); } /** * API แก้สถานะแบบประเมินตามรายการที่เลือก * * @summary แก้สถานะแบบประเมินตามรายการที่เลือก * * */ @Post("admin/change-status") async ChangeStatus( @Request() request: RequestWithUser, @Body() requestBody: { status: string; id: string[]; }, ) { let profileId: any = null; await new CallAPI() .GetData(request, "/org/profile/keycloak/position") .then((x) => { profileId = x.profileId; }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ"); }); const list = await this.kpiUserEvalutionRepository.find({ where: { id: In(requestBody.id) }, }); if (!list || list.length === 0) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id)); if (!hasAllData) { throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล"); } await Promise.all( list.map(async (item) => { let role = "EVALUATOR"; if (profileId == item.commanderId) { role = "COMMANDER"; } else if (profileId == item.commanderHighId) { role = "COMMANDERHIGH"; } if (requestBody.status.trim().toUpperCase() == "APPROVE") { if (role == "EVALUATOR") { if (item.evaluationStatus == "NEW_EVALUATOR") { if (item.commanderId == null || item.commanderId == "") { item.evaluationStatus = "APPROVE"; } else { await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${item.prefix}${item.firstName} ${item.lastName} ผู้ประเมินอนุมัติข้อตกลง`, body: `${item.prefix}${item.firstName} ${item.lastName} ผู้ประเมินอนุมัติข้อตกลง`, receiverUserId: item.commanderId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); item.evaluationStatus = "NEW_COMMANDER"; } } } else if (role == "COMMANDER") { if (item.evaluationStatus == "NEW_COMMANDER") { if (item.commanderHighId == null || item.commanderHighId == "") { item.evaluationStatus = "APPROVE"; } else { await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${item.prefix}${item.firstName} ${item.lastName} ผู้บังคับบัญชาเหนือขึ้นไปอนุมัติข้อตกลง`, body: `${item.prefix}${item.firstName} ${item.lastName} ผู้บังคับบัญชาเหนือขึ้นไปอนุมัติข้อตกลง`, receiverUserId: item.commanderHighId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); item.evaluationStatus = "NEW_COMMANDER_HIGH"; } } } else { item.evaluationStatus = requestBody.status.trim().toUpperCase(); } } else { item.evaluationStatus = requestBody.status.trim().toUpperCase(); } const before = null; let _null: any = null; item.reasonReject = _null; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; item.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(item, { data: request }); setLogDataDiff(request, { before, after: item }); }), ); return new HttpSuccess(); } /** * API อนุมัติการขอแก้ไขแบบประเมินตามรายการที่เลือก * * @summary อนุมัติการขอแก้ไขแบบประเมินตามรายการที่เลือก * * */ @Post("admin/req-edit") async RequestEdit( @Request() request: RequestWithUser, @Body() requestBody: { status: string; id: string[]; }, ) { let profileId: any = null; await new CallAPI() .GetData(request, "/org/profile/keycloak/position") .then((x) => { profileId = x.profileId; }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ"); }); const list = await this.kpiUserEvalutionRepository.find({ where: { id: In(requestBody.id) }, }); if (!list || list.length === 0) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id)); if (!hasAllData) { throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล"); } list.forEach(async (item) => { let role = "EVALUATOR"; if (profileId == item.commanderId) { role = "COMMANDER"; } else if (profileId == item.commanderHighId) { role = "COMMANDERHIGH"; } if (requestBody.status.trim().toUpperCase() == "DONE") { if (role == "EVALUATOR") { if (item.evaluationReqEdit == "EVALUATOR") { if (item.commanderId == null || item.commanderId == "") { item.evaluationReqEdit = "DONE"; item.evaluationStatus = "NEW"; } else { item.evaluationReqEdit = "COMMANDER"; } } } else if (role == "COMMANDER") { if (item.evaluationReqEdit == "COMMANDER") { if (item.commanderHighId == null || item.commanderHighId == "") { item.evaluationReqEdit = "DONE"; item.evaluationStatus = "NEW"; } else { item.evaluationReqEdit = "COMMANDER_HIGH"; } } } else { item.evaluationReqEdit = requestBody.status.trim().toUpperCase(); item.evaluationStatus = "NEW"; } } else { item.evaluationReqEdit = requestBody.status.trim().toUpperCase(); } const before = null; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; item.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(item, { data: request }); setLogDataDiff(request, { before, after: item }); }); return new HttpSuccess(); } /** * API แก้ไขสถานะผลการประเมิน * * @summary แก้ไขสถานะผลการประเมิน * * */ @Post("admin/result-status") async ResultStatus( @Request() request: RequestWithUser, @Body() requestBody: { status: string; id: string[]; }, ) { let profileId: any = null; await new CallAPI() .GetData(request, "/org/profile/keycloak/position") .then((x) => { profileId = x.profileId; }) .catch(() => { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ"); }); const list = await this.kpiUserEvalutionRepository.find({ where: { id: In(requestBody.id) }, }); if (!list || list.length === 0) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id)); if (!hasAllData) { throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล"); } list.forEach(async (item) => { let role = "EVALUATOR"; if (profileId == item.commanderId) { role = "COMMANDER"; } else if (profileId == item.commanderHighId) { role = "COMMANDERHIGH"; } // if (requestBody.status.trim().toUpperCase() == "DONE") { if (role == "EVALUATOR") { if (item.evaluationStatus == "EVALUATING_EVALUATOR") { if (item.commanderId == null || item.commanderId == "") { item.evaluationStatus = "SUMMARY"; } else { item.evaluationStatus = "EVALUATING_COMMANDER"; } item.evaluationResults = requestBody.status.trim().toUpperCase(); } } else if (role == "COMMANDER") { if (item.evaluationStatus == "EVALUATING_COMMANDER") { if (item.commanderHighId == null || item.commanderHighId == "") { item.evaluationStatus = "SUMMARY"; } else { item.evaluationStatus = "EVALUATING_COMMANDER_HIGH"; } } } else { item.evaluationStatus = "SUMMARY"; } // } else { // // item.evaluationStatus = requestBody.status.trim().toUpperCase(); // } const before = null; let _null: any = null; item.reasonReject = _null; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; item.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(item, { data: request }); setLogDataDiff(request, { before, after: item }); }); return new HttpSuccess(); } /** * API รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (ADMIN) * * @summary รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (ADMIN) * * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (ADMIN) */ @Get("admin/{id}") async GetKpiAdminEvaluationById(@Path() id: string, @Request() req: RequestWithUser) { let _workflow = await new permission().Workflow(req, id, "SYS_KPI_LIST"); if (_workflow == false) await new permission().PermissionGet(req, "SYS_KPI_LIST"); const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ relations: ["kpiPeriod"], where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const mapData = { id: kpiUserEvaluation.id, profileId: kpiUserEvaluation.profileId, prefix: kpiUserEvaluation.prefix, firstName: kpiUserEvaluation.firstName, lastName: kpiUserEvaluation.lastName, position: kpiUserEvaluation.position, posLevelName: kpiUserEvaluation.posLevelName, posTypeName: kpiUserEvaluation.posTypeName, posExecutiveName: kpiUserEvaluation.posExecutiveName, evaluationStatus: kpiUserEvaluation.evaluationStatus, evaluationResults: kpiUserEvaluation.evaluationResults, evaluationReqEdit: kpiUserEvaluation.evaluationReqEdit, isProbation: kpiUserEvaluation.isProbation, createdAt: kpiUserEvaluation.createdAt, evaluatorId: kpiUserEvaluation.evaluatorId, commanderId: kpiUserEvaluation.commanderId, commanderHighId: kpiUserEvaluation.commanderHighId, kpiPeriodId: kpiUserEvaluation.kpiPeriodId, totalPoint1: kpiUserEvaluation.totalPoint1, totalPoint2_1: kpiUserEvaluation.totalPoint2_1, totalPoint2_2: kpiUserEvaluation.totalPoint2_2, summaryPoint: kpiUserEvaluation.summaryPoint, isOpen: kpiUserEvaluation.isOpen, openDate: kpiUserEvaluation.openDate, root: kpiUserEvaluation.org, child1: kpiUserEvaluation.child1, child2: kpiUserEvaluation.child2, child3: kpiUserEvaluation.child3, child4: kpiUserEvaluation.child4, year: kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.year, durationKPI: kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.durationKPI, }; return new HttpSuccess(mapData); } /** * * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Get("open/{id}") async openKpiUserEvaluation(@Path() id: string, @Request() request: RequestWithUser) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.isOpen = true; kpiUserEvaluation.openDate = new Date(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสรุปผลการประเมิน (USER) * * @summary แก้ไขสรุปผลการประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("reason/user/{id}") async updateReasonKpiUserEvaluation( @Path() id: string, @Body() requestBody: { topicEvaluator?: string | null; developEvaluator?: string | null; timeEvaluator?: string | null; reasonEvaluator?: string | null; }, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const _null: any = null; if (kpiUserEvaluation.commanderId == null) { await new CallAPI() .PostData(request, "/placement/noti/keycloak", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, receiverUserId: "aec26ac3-417c-4cf9-9cbe-874939f99ecc", payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); kpiUserEvaluation.evaluationStatus = "COMPLETE"; } else { await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, receiverUserId: kpiUserEvaluation.commanderId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); kpiUserEvaluation.evaluationStatus = "SUMMARY_COMMANDER"; } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.topicEvaluator = requestBody.topicEvaluator == null ? _null : requestBody.topicEvaluator; kpiUserEvaluation.developEvaluator = requestBody.developEvaluator == null ? _null : requestBody.developEvaluator; kpiUserEvaluation.timeEvaluator = requestBody.timeEvaluator == null ? _null : requestBody.timeEvaluator; kpiUserEvaluation.reasonEvaluator = requestBody.reasonEvaluator == null ? _null : requestBody.reasonEvaluator; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสรุปผลการประเมินผู้บังคับบัญชาเหนือขึ้นไป (USER) * * @summary แก้ไขสรุปผลการประเมินผู้บังคับบัญชาเหนือขึ้นไป (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("reason/commander/{id}") async updateReasonKpiCommanderEvaluation( @Path() id: string, @Body() requestBody: { isReason: boolean; reason?: string | null; }, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const _null: any = null; if (kpiUserEvaluation.commanderHighId == null) { await new CallAPI() .PostData(request, "/placement/noti/keycloak", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, receiverUserId: "aec26ac3-417c-4cf9-9cbe-874939f99ecc", payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); kpiUserEvaluation.evaluationStatus = "COMPLETE"; } else { await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, receiverUserId: kpiUserEvaluation.commanderHighId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); kpiUserEvaluation.evaluationStatus = "SUMMARY_COMMANDER_HIGH"; } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.isReasonCommander = requestBody.isReason; kpiUserEvaluation.reasonCommander = requestBody.reason == null ? _null : requestBody.reason; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API แก้ไขสรุปผลการประเมินผู้บังคับบัญชาเหนือขึ้นไปอีกหนึ่งขั้น (USER) * * @summary แก้ไขสรุปผลการประเมินผู้บังคับบัญชาเหนือขึ้นไปอีกหนึ่งขั้น (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Put("reason/commanderHigh/{id}") async updateReasonKpiCommanderHighEvaluation( @Path() id: string, @Body() requestBody: { isReason: boolean; reason?: string | null; }, @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const _null: any = null; kpiUserEvaluation.evaluationStatus = "COMPLETE"; await new CallAPI() .PostData(request, "/placement/noti/keycloak", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ผู้ประเมินแสดงความเห็น`, receiverUserId: "aec26ac3-417c-4cf9-9cbe-874939f99ecc", payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(() => {}) .catch(() => {}); const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.isReasonCommanderHigh = requestBody.isReason; kpiUserEvaluation.reasonCommanderHigh = requestBody.reason == null ? _null : requestBody.reason; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } /** * API บันทึกข้อมูลลง kp7 (USER) * * @summary บันทึกข้อมูลลง kp7 (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Post("done/kp7") async updateKpiEvaluationTokp7( @Body() requestBody: { id: string[]; }, @Request() request: RequestWithUser, ) { // await new permission().PermissionCreate(request, "SYS_RESULT");//USER const kpiUserEvaluations = await this.kpiUserEvalutionRepository.find({ where: { id: In(requestBody.id) }, relations: ["kpiUserDevelopments", "kpiUserDevelopments.developmentProjects"], }); await Promise.all( kpiUserEvaluations.map(async (kpiUserEvaluation) => { kpiUserEvaluation.evaluationStatus = "KP7"; await new CallAPI() .PostData(request, "/org/profile/assessments", { date: new Date(), name: kpiUserEvaluation.evaluationResults, point1Total: kpiUserEvaluation.weightPoint1, point1: kpiUserEvaluation.totalPoint1, point2Total: kpiUserEvaluation.weightPoint2, point2: kpiUserEvaluation.totalPoint2_1 + kpiUserEvaluation.totalPoint2_2, pointSumTotal: kpiUserEvaluation.summaryWeight, pointSum: kpiUserEvaluation.summaryPoint, profileId: kpiUserEvaluation.profileId, }) .then(async () => {}) .catch((error) => { console.error("Error details:", error.response.data); }); kpiUserEvaluation.kpiUserDevelopments.map(async (kpiUserDevelopment) => { await new CallAPI() .PostData(request, "/org/profile/development", { type: "KPI", profileId: kpiUserEvaluation.profileId, name: kpiUserDevelopment.name, developmentTarget: kpiUserDevelopment.target, achievement10: kpiUserDevelopment.achievement10, achievement5: kpiUserDevelopment.achievement5, achievement0: kpiUserDevelopment.achievement0, kpiDevelopmentId: kpiUserDevelopment.id, reasonDevelopment70: kpiUserDevelopment.reasonDevelopment70, reasonDevelopment20: kpiUserDevelopment.reasonDevelopment20, reasonDevelopment10: kpiUserDevelopment.reasonDevelopment10, isDevelopment70: kpiUserDevelopment.isDevelopment70, isDevelopment20: kpiUserDevelopment.isDevelopment20, isDevelopment10: kpiUserDevelopment.isDevelopment10, developmentResults: `${kpiUserDevelopment.achievement10}(10), ${kpiUserDevelopment.achievement5}(5), ${kpiUserDevelopment.achievement0}(0)`, developmentReport: kpiUserDevelopment.point != null ? kpiUserDevelopment.point.toString() : null, developmentProjects: kpiUserDevelopment.developmentProjects.map((x) => x.name), }) .then(async () => {}) .catch((error) => { console.error("Error details:", error.response.data); }); }); await new CallAPI() .PostData(request, "/placement/noti/profile", { subject: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ระดับผลการประเมินการปฏิบัติราชการระดับบุคคลของคุณอยู่ในเกณฑ์ ${kpiUserEvaluation.summaryPoint}`, body: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName} ระดับผลการประเมินการปฏิบัติราชการระดับบุคคลของคุณอยู่ในเกณฑ์ ${kpiUserEvaluation.summaryPoint}`, receiverUserId: kpiUserEvaluation.profileId, payload: "", isSendMail: true, isSendInbox: true, isSendNotification: true, }) .then(async () => {}) .catch((error) => { console.error("Error details:", error.response.data); }); const before = null; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); }), ); return new HttpSuccess(); } /** * API รายละเอียดสรุปผลการประเมิน (USER) * * @summary รายละเอียดสรุปผลการประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Get("reason/{id}") async getReasonKpiEvaluation( @Path() id: string, @Request() request: { user: Record }, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, select: [ "topicEvaluator", "developEvaluator", "timeEvaluator", "reasonEvaluator", "isReasonCommander", "reasonCommander", "isReasonCommanderHigh", "reasonCommanderHigh", "plannedPoint", "rolePoint", "specialPoint", "capacityPoint", "totalPoint1", "totalPoint2_1", "totalPoint2_2", "summaryPoint", "weightPoint1", "weightPoint2", "summaryWeight", "evaluationResults", "isOpen", ], }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } return new HttpSuccess(kpiUserEvaluation); } /** * API รายละเอียดสรุปผลการประเมิน (USER) * * @summary รายละเอียดสรุปผลการประเมิน (USER) * * @param {string} id Guid, *Id คนประเมิน (USER) */ @Get("summary/{id}") async getSummaryKpiEvaluation(@Path() id: string, @Request() request: RequestWithUser) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, select: ["id", "evaluationStatus", "lastUpdateUserId", "lastUpdateFullName"], }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationStatus = "SUMMARY"; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; kpiUserEvaluation.lastUpdatedAt = new Date(); await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation); } /** * API รายการผลการประเมินภายใน 5 ปี * * @summary รายการผลการประเมินภายใน 5 ปี * * @param {string} id Guid, *Id คนประเมิน */ @Get("summaryFiveYear/{id}") async getsummaryFiveYear(@Path() id: string, @Request() request: RequestWithUser) { const year = new Date().getFullYear(); const kpiUserEvaluation = await this.kpiUserEvalutionRepository.find({ relations: ["kpiPeriod"], where: { profileId: id, evaluationResults: Not("PENDING"), evaluationStatus: "KP7", kpiPeriod: { year: MoreThanOrEqual(year - 5), }, }, select: ["id", "evaluationStatus", "evaluationResults", "kpiPeriod"], }); if (!kpiUserEvaluation) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } return new HttpSuccess(kpiUserEvaluation); } }