From ba00645cb85b484ab8d9b6c7822a62c07fd0cb3e Mon Sep 17 00:00:00 2001 From: Kittapath Date: Fri, 26 Apr 2024 12:40:43 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20query=20total?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/KpiPeriodController.ts | 82 ++++++++++++++-- src/controllers/KpiPlanController.ts | 4 +- src/controllers/KpiRoleController.ts | 4 +- src/controllers/KpiUserCapacityController.ts | 95 +++++++++---------- .../KpiUserEvaluationController.ts | 4 +- 5 files changed, 124 insertions(+), 65 deletions(-) diff --git a/src/controllers/KpiPeriodController.ts b/src/controllers/KpiPeriodController.ts index db26d08..06b2094 100644 --- a/src/controllers/KpiPeriodController.ts +++ b/src/controllers/KpiPeriodController.ts @@ -20,8 +20,14 @@ import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { KpiPeriod, createKpiPeriod, updateKpiPeriod } from "../entities/kpiPeriod"; -import { Like } from "typeorm/browser"; +import { In, Not } from "typeorm"; import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; +import { KpiPlan } from "../entities/kpiPlan"; +import { KpiRole } from "../entities/kpiRole"; +import { KpiUserRole } from "../entities/kpiUserRole"; +import { KpiUserPlanned } from "../entities/kpiUserPlanned"; +import { KpiUserCapacity } from "../entities/kpiUserCapacity"; +import { KpiUserSpecial } from "../entities/kpiUserSpecial"; @Route("api/v1/kpi/period") @Tags("kpiPeriod") @@ -34,6 +40,12 @@ import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; export class kpiPeriodController extends Controller { private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); + private kpiRoleRepository = AppDataSource.getRepository(KpiRole); + private kpiPlanRepository = AppDataSource.getRepository(KpiPlan); + private kpiUserRoleRepository = AppDataSource.getRepository(KpiUserRole); + private kpiUserPlannedRepository = AppDataSource.getRepository(KpiUserPlanned); + private kpiUserCapacityRepository = AppDataSource.getRepository(KpiUserCapacity); + private kpiUserSpecialRepository = AppDataSource.getRepository(KpiUserSpecial); /** * สร้างรอบการประเมินผลการปฏิบัติหน้าที่ราชการ * @param requestBody @@ -49,6 +61,15 @@ export class kpiPeriodController extends Controller { @Body() requestBody: createKpiPeriod, @Request() request: { user: Record }, ) { + const chkkpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { + durationKPI: requestBody.durationKPI, + year: requestBody.year, + }, + }); + if (chkkpiPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "รอบการประเมินผลนี้มีอยู่ในระบบแล้ว"); + } const kpiPeriod = Object.assign(new KpiPeriod(), requestBody); kpiPeriod.durationKPI = requestBody.durationKPI.trim().toUpperCase(); kpiPeriod.createdUserId = request.user.sub; @@ -80,6 +101,16 @@ export class kpiPeriodController extends Controller { "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } + const chkkpiPeriod = await this.kpiPeriodRepository.findOne({ + where: { + id: Not(id), + durationKPI: requestBody.durationKPI, + year: requestBody.year, + }, + }); + if (chkkpiPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "รอบการประเมินผลนี้มีอยู่ในระบบแล้ว"); + } requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase(); this.kpiPeriodRepository.merge(kpiPeriod, requestBody); @@ -207,15 +238,48 @@ export class kpiPeriodController extends Controller { "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } - const chkKpiUserEvaluation = await this.kpiUserEvaluationRepository.find({ + const kpiRole = await this.kpiRoleRepository.find({ where: { kpiPeriodId: id }, - }) - if (chkKpiUserEvaluation) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่สามารถลบข้อมูลได้", - ); - } + }); + const kpiPlan = await this.kpiPlanRepository.find({ + where: { kpiPeriodId: id }, + }); + const kpiUserEvaluation = await this.kpiUserEvaluationRepository.find({ + where: { kpiPeriodId: id }, + }); + + const kpiUserRole = await this.kpiUserRoleRepository.find({ + where: { kpiRoleId: In(kpiRole.map((x) => x.id)) }, + }); + + const kpiUserPlanned = await this.kpiUserPlannedRepository.find({ + where: { kpiPlanId: In(kpiPlan.map((x) => x.id)) }, + }); + + const _kpiUserRole = await this.kpiUserRoleRepository.find({ + where: { kpiUserEvaluationId: In(kpiRole.map((x) => x.id)) }, + }); + const _kpiUserPlanned = await this.kpiUserPlannedRepository.find({ + where: { kpiUserEvaluationId: In(kpiPlan.map((x) => x.id)) }, + }); + const _kpiUserCapacity = await this.kpiUserCapacityRepository.find({ + where: { kpiUserEvaluationId: In(kpiPlan.map((x) => x.id)) }, + }); + const _kpiUserSpecial = await this.kpiUserSpecialRepository.find({ + where: { kpiUserEvaluationId: In(kpiPlan.map((x) => x.id)) }, + }); + + await this.kpiUserRoleRepository.remove(kpiUserRole); + await this.kpiUserPlannedRepository.remove(kpiUserPlanned); + + await this.kpiUserRoleRepository.remove(_kpiUserRole); + await this.kpiUserPlannedRepository.remove(_kpiUserPlanned); + await this.kpiUserCapacityRepository.remove(_kpiUserCapacity); + await this.kpiUserSpecialRepository.remove(_kpiUserSpecial); + + await this.kpiRoleRepository.remove(kpiRole); + await this.kpiPlanRepository.remove(kpiPlan); + await this.kpiUserEvaluationRepository.remove(kpiUserEvaluation); await this.kpiPeriodRepository.remove(kpiPeriod); return new HttpSuccess(); } diff --git a/src/controllers/KpiPlanController.ts b/src/controllers/KpiPlanController.ts index e8e4874..fdefa3f 100644 --- a/src/controllers/KpiPlanController.ts +++ b/src/controllers/KpiPlanController.ts @@ -309,7 +309,7 @@ export class kpiPlanController extends Controller { : "kpiPlan.rootId LIKE :nodeId" : "1=1", { - nodeId: `${nodeId}`, + nodeId: nodeId, }, ) .andWhere( @@ -317,7 +317,7 @@ export class kpiPlanController extends Controller { ? "kpiPlan.kpiPeriodId LIKE :kpiPeriodId" : "1=1", { - kpiPeriodId: `${kpiPeriodId}`, + kpiPeriodId: kpiPeriodId, }, ) .select([ diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index c60274d..21d4254 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -243,7 +243,7 @@ export class kpiRoleController extends Controller { : "kpiRole.rootId LIKE :nodeId" : "1=1", { - nodeId: `${nodeId}`, + nodeId: nodeId, }, ) .andWhere( @@ -251,7 +251,7 @@ export class kpiRoleController extends Controller { ? "kpiRole.kpiPeriod LIKE :kpiPeriodId" : "1=1", { - kpiPeriodId: `${kpiPeriodId}`, + kpiPeriodId: kpiPeriodId, }, ) .andWhere(position != undefined ? "kpiRole.position LIKE :position" : "1=1", { diff --git a/src/controllers/KpiUserCapacityController.ts b/src/controllers/KpiUserCapacityController.ts index 3d80fa8..75210b0 100644 --- a/src/controllers/KpiUserCapacityController.ts +++ b/src/controllers/KpiUserCapacityController.ts @@ -14,7 +14,7 @@ import { SuccessResponse, Response, Query, - ArrayValidator + ArrayValidator, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -22,7 +22,7 @@ import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { KpiCapacity } from "../entities/kpiCapacity"; import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; -import { KpiUserCapacity, KpiUserCapacityDataPoint} from "../entities/kpiUserCapacity"; +import { KpiUserCapacity, KpiUserCapacityDataPoint } from "../entities/kpiUserCapacity"; import { Like, In, Not } from "typeorm"; import { Double } from "typeorm/browser"; @@ -41,23 +41,24 @@ export class KpiUserCapacityController extends Controller { /** * API สร้างองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) - * + * * @summary สร้างองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) - * - * + * + * */ @Post() async CreateKpiUserCapacity( - @Body() requestBody: { + @Body() + requestBody: { kpiUserEvaluationId: string; kpiCapacityId: string; level: string; // point: number; weight: number; - summary: Double + summary: Double; }, @Request() request: { user: Record }, - ){ + ) { const kpiUserEvalution = await this.kpiUserEvalutionRepository.findOne({ where: { id: requestBody.kpiUserEvaluationId }, }); @@ -71,18 +72,15 @@ export class KpiUserCapacityController extends Controller { where: { id: requestBody.kpiCapacityId }, }); if (!kpiCapacity) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลรายการสมรรถนะนี้", - ); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้"); } const chkRepleat = await this.kpiUserCapacityRepository.find({ where: { kpiUserEvaluationId: requestBody.kpiUserEvaluationId, - kpiCapacityId: requestBody.kpiCapacityId - } - }) - if(chkRepleat.length > 0){ + kpiCapacityId: requestBody.kpiCapacityId, + }, + }); + if (chkRepleat.length > 0) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากรายการสมรรถนะซ้ำ", @@ -95,30 +93,29 @@ export class KpiUserCapacityController extends Controller { kpiUserCapacity.lastUpdateFullName = request.user.name; await this.kpiUserCapacityRepository.save(kpiUserCapacity); return new HttpSuccess(kpiUserCapacity.id); - } /** * API แก้ไของค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) - * + * * @summary แก้ไของค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) - * + * * @param {string} id Guid, *Id องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) */ @Put("{id}") async updateKpiUserCapacity( @Path() id: string, - @Body() requestBody: { + @Body() + requestBody: { kpiUserEvaluationId: string; kpiCapacityId: string; level: string; // point: number; weight: number; - summary: Double + summary: Double; }, @Request() request: { user: Record }, ) { - const kpiUserEvalution = await this.kpiUserEvalutionRepository.findOne({ where: { id: requestBody.kpiUserEvaluationId }, }); @@ -132,10 +129,7 @@ export class KpiUserCapacityController extends Controller { where: { id: requestBody.kpiCapacityId }, }); if (!kpiCapacity) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลรายการสมรรถนะนี้", - ); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้"); } const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({ where: { id: id }, @@ -150,10 +144,10 @@ export class KpiUserCapacityController extends Controller { where: { kpiUserEvaluationId: requestBody.kpiUserEvaluationId, kpiCapacityId: requestBody.kpiCapacityId, - id: Not(id) - } - }) - if(chkRepleat.length > 0){ + id: Not(id), + }, + }); + if (chkRepleat.length > 0) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถแก้ไขข้อมูลได้เนื่องจากรายการสมรรถนะซ้ำ", @@ -165,7 +159,6 @@ export class KpiUserCapacityController extends Controller { this.kpiUserCapacityRepository.merge(kpiUserCapacity, _kpiUserCapacity); await this.kpiUserCapacityRepository.save(kpiUserCapacity); return new HttpSuccess(kpiUserCapacity.id); - } /** @@ -179,8 +172,8 @@ export class KpiUserCapacityController extends Controller { async GetKpiUserCapacityById(@Path() id: string) { const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({ where: { id: id }, - relations: ["kpiCapacity"] - }) + relations: ["kpiCapacity"], + }); if (!kpiUserCapacity) { throw new HttpError( HttpStatusCode.NOT_FOUND, @@ -194,8 +187,8 @@ export class KpiUserCapacityController extends Controller { level: kpiUserCapacity.level, point: kpiUserCapacity.point, weight: kpiUserCapacity.weight, - summary: kpiUserCapacity.summary - } + summary: kpiUserCapacity.summary, + }; return new HttpSuccess(mapData); } @@ -208,15 +201,15 @@ export class KpiUserCapacityController extends Controller { @Get() async listKpiUserCapacity( @Query("id") id: string, //kpiUserEvaluationId - @Query("type") type: string + @Query("type") type: string, ) { const [kpiUserCapacity, total] = await AppDataSource.getRepository(KpiUserCapacity) - .createQueryBuilder("kpiUserCapacity") - .leftJoinAndSelect("kpiUserCapacity.kpiCapacity", "kpiCapacity") - .andWhere("kpiUserCapacity.kpiUserEvaluationId = :id", { id: id }) - .andWhere(type ? "kpiCapacity.type LIKE :type" : "1=1", { type: `%${type.toLocaleUpperCase()}%` }) - .orderBy("kpiUserCapacity.createdAt", "ASC") - .getManyAndCount(); + .createQueryBuilder("kpiUserCapacity") + .leftJoinAndSelect("kpiUserCapacity.kpiCapacity", "kpiCapacity") + .andWhere("kpiUserCapacity.kpiUserEvaluationId = :id", { id: id }) + .andWhere(type ? "kpiCapacity.type LIKE :type" : "1=1", { type: type.toLocaleUpperCase() }) + .orderBy("kpiUserCapacity.createdAt", "ASC") + .getManyAndCount(); const mapData = kpiUserCapacity.map((item) => ({ id: item.id, @@ -232,9 +225,9 @@ export class KpiUserCapacityController extends Controller { /** * API ลบองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) - * + * * @summary ลบองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) - * + * * @param {string} id Guid, *Id องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) */ @Delete("{id}") @@ -254,23 +247,25 @@ export class KpiUserCapacityController extends Controller { /** * API กรอกระดับคะแนนตามเกณฑ์การประเมิน (สมรรถนะ) (USER) - * + * * @summary กรอกระดับคะแนนตามเกณฑ์การประเมิน (สมรรถนะ) (USER) - * - * + * + * */ @Post("point") async CreateKpiUserCapacityPoint( @Body() requestBody: KpiUserCapacityDataPoint[], @Request() request: { user: Record }, - ){ - + ) { for (const item of requestBody) { const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({ where: { id: item.id }, }); if (!kpiUserCapacity) { - throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลพฤติกรรมการปฎิบัติราชการ (สมรรถนะ): ${item.id}`); + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `ไม่พบข้อมูลพฤติกรรมการปฎิบัติราชการ (สมรรถนะ): ${item.id}`, + ); } this.kpiUserCapacityRepository.merge(kpiUserCapacity, item); kpiUserCapacity.lastUpdateUserId = request.user.sub; diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index 36fc508..4fb7d10 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -57,7 +57,7 @@ export class KpiUserEvaluationController extends Controller { const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", { - kpiPeriodId: `%${kpiPeriodId}%`, + kpiPeriodId: kpiPeriodId, }) .orderBy("kpiUserEvaluation.createdAt", "ASC") .skip((page - 1) * pageSize) @@ -212,7 +212,7 @@ export class KpiUserEvaluationController extends Controller { const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", { - kpiPeriodId: `%${kpiPeriodId}%`, + kpiPeriodId: kpiPeriodId, }) .orderBy("kpiUserEvaluation.createdAt", "ASC") .skip((page - 1) * pageSize)