From ad00638b11cc72108988367a32c213ef5b63eaa2 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 18 Apr 2024 17:20:46 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=81=E0=B8=93=E0=B8=91=E0=B9=8C?= =?UTF-8?q?=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B9=80?= =?UTF-8?q?=E0=B8=A1=E0=B8=B4=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/KpiEvaluationController.ts | 68 +++++++++++----------- src/entities/kpiEvaluation.ts | 6 +- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/controllers/KpiEvaluationController.ts b/src/controllers/KpiEvaluationController.ts index d78c22b..73f1ca8 100644 --- a/src/controllers/KpiEvaluationController.ts +++ b/src/controllers/KpiEvaluationController.ts @@ -32,41 +32,39 @@ import { KpiEvaluation, updateKpiEvaluation } from "../entities/kpiEvaluation"; export class kpiEvaluationController extends Controller { private kpiEvaluationRepository = AppDataSource.getRepository(KpiEvaluation); - // /** - // * API แก้ไขเกณฑ์การประเมิน - // * @param id ไอดีของเกณฑ์การประเมิน - // */ - // @Put("{id}") - // async updateKpiEvaluation( - // @Path() id: string, - // @Body() requestBody: updateKpiEvaluation, - // @Request() request: { user: Record }, - // ) { - // const kpiEvaluation = await this.kpiEvaluationRepository.findOne({ - // where: { id: id }, - // }); - // if (!kpiEvaluation) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเกณฑ์การประเมินนี้"); - // } + /** + * API แก้ไขเกณฑ์การประเมิน + * @param id ไอดีของเกณฑ์การประเมิน + */ + @Put() +async updateKpiEvaluations( + @Body() requestBody: updateKpiEvaluation[], + @Request() request: { user: Record }, +) { + const updatedIds: string[] = []; - // // const chkkpinameGroup = await this.kpiGroupRepository.findOne({ - // // where: { - // // nameGroupKPI: requestBody.nameGroupKPI, - // // }, - // // }); - // // if (chkkpinameGroup) { - // // throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว"); - // // } + for (const item of requestBody) { + const kpiEvaluation = await this.kpiEvaluationRepository.findOne({ + where: { id: item.id }, + }); + if (!kpiEvaluation) { + throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลเกณฑ์การประเมินนี้: ${item.id}`); + } + + this.kpiEvaluationRepository.merge(kpiEvaluation, item); + kpiEvaluation.lastUpdateUserId = request.user.sub; + kpiEvaluation.lastUpdateFullName = request.user.name; + await this.kpiEvaluationRepository.save(kpiEvaluation); + + updatedIds.push(item.id); + } + + return new HttpSuccess(); +} - // this.kpiEvaluationRepository.merge(kpiEvaluation, requestBody); - // kpiEvaluation.lastUpdateUserId = request.user.sub; - // kpiEvaluation.lastUpdateFullName = request.user.name; - // await this.kpiEvaluationRepository.save(kpiEvaluation); - // return new HttpSuccess(id); - // } /** - * API list กลุ่มงาน + * API list เกณฑ์การประเมิน * @param page * @param pageSize */ @@ -80,13 +78,17 @@ export class kpiEvaluationController extends Controller { if (keyword !== undefined && keyword !== "") { whereClause = { - where: [{ nameGroupKPI: Like(`%${keyword}%`) }], + where: [{ description: Like(`%${keyword}%`) }], }; - } + whereClause.where.push({ level: Like(`%${keyword}%`) }); + } + const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({ ...whereClause, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), + order:{ + level: "DESC"} }); return new HttpSuccess({ data: kpiEvaluation, total }); diff --git a/src/entities/kpiEvaluation.ts b/src/entities/kpiEvaluation.ts index 8bf65b0..56a3430 100644 --- a/src/entities/kpiEvaluation.ts +++ b/src/entities/kpiEvaluation.ts @@ -24,9 +24,9 @@ export class createKpiEvaluation { } export class updateKpiEvaluation { + @Column("uuid") + id: string; + @Column() description: string; - - @Column() - level: Number; }