diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index b070790..4e09d40 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -33,6 +33,10 @@ import { } from "../entities/kpiUserEvaluation"; import { Like, In, Brackets } from "typeorm"; import CallAPI from "../interfaces/call-api"; +import { KpiCapacity } from "../entities/kpiCapacity"; +import { Position } from "../entities/position"; +import { KpiLink } from "../entities/kpiLink"; +import { KpiGroup } from "../entities/kpiGroup"; @Route("api/v1/kpi/user/evaluation") @Tags("kpiUserEvaluation") @@ -45,6 +49,10 @@ import CallAPI from "../interfaces/call-api"; 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 kpiGroupRepository = AppDataSource.getRepository(KpiGroup); /** * API @@ -200,7 +208,6 @@ export class KpiUserEvaluationController extends Controller { evaluating?: boolean | null; }, ) { - const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .andWhere(requestBody.kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", { @@ -315,6 +322,155 @@ export class KpiUserEvaluationController extends Controller { kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + + enum CapacityType { + HEAD = "HEAD", + GROUP = "GROUP", + } + + // MAIN CAPACITY + const mainCapacities = await this.kpiCapacityRepository.find({ + where: { type: CapacityType.HEAD }, + }); + + let level: any; + switch (level) { + case kpiUserEvaluation.posTypeName == "บริหาร" && kpiUserEvaluation.posLevelName == "สูง": + level = 5; + break; + case kpiUserEvaluation.posTypeName == "บริหาร" && kpiUserEvaluation.posLevelName == "ต้น": + level = 4; + break; + case kpiUserEvaluation.posTypeName == "อำนวยการ" && kpiUserEvaluation.posLevelName == "สูง": + level = 4; + break; + case kpiUserEvaluation.posTypeName == "อำนวยการ" && kpiUserEvaluation.posLevelName == "ต้น": + level = 3; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ทรงคุณวุฒิ": + level = 5; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "เชี่ยวชาญ": + level = 4; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ชำนาญการพิเศษ": + level = 3; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ชำนาญการ": + level = 2; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ปฏิบัติการ": + level = 1; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && + kpiUserEvaluation.posLevelName == "ทักษะพิเศษ": + level = 4; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && kpiUserEvaluation.posLevelName == "อาวุโส": + level = 3; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && + kpiUserEvaluation.posLevelName == "ชำนาญงาน": + level = 2; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && + kpiUserEvaluation.posLevelName == "ปฏิบัติงาน": + level = 1; + break; + default: + level = null; + break; + } + for (const capacity of mainCapacities) { + await new CallAPI() + .PostData(request, "kpi/user/capacity", { + kpiUserEvaluationId: kpiUserEvaluation.id, + kpiCapacityId: capacity.id, + level: level, + weight: 100, + }) + .catch((error) => { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถสร้างข้อมูลสมรรถนะได้"); + }); + } + + // GROUP CAPACITY + const findPosition = await this.kpiPositionRepository.findOne({ + where: { name: kpiUserEvaluation.position }, + }); + + let levelForGourp: any; + switch (levelForGourp) { + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ทรงคุณวุฒิ": + level = 5; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "เชี่ยวชาญ": + level = 4; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ชำนาญการพิเศษ": + level = 4; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ชำนาญการ": + level = 3; + break; + case kpiUserEvaluation.posTypeName == "วิชาการ" && + kpiUserEvaluation.posLevelName == "ปฏิบัติการ": + level = 2; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && + kpiUserEvaluation.posLevelName == "ทักษะพิเศษ": + level = 4; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && + kpiUserEvaluation.posLevelName == "อาวุโส": + level = 3; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && + kpiUserEvaluation.posLevelName == "ชำนาญงาน": + level = 2; + break; + case kpiUserEvaluation.posTypeName == "ทั่วไป" && + kpiUserEvaluation.posLevelName == "ปฏิบัติงาน": + level = 1; + break; + default: + level = null; + break; + } + + if (findPosition && findPosition.kpiLinkId && level != null) { + const findKpiLink = await this.kpiLinkRepository.findOne({ + relations: ["kpiCapacities"], + 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((x) => { + throw new HttpError(HttpStatusCode.NOT_FOUND, x); + }); + } + } + } + return new HttpSuccess(kpiUserEvaluation.id); }