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 c467ae5..30f347d 100644 --- a/src/entities/kpiEvaluation.ts +++ b/src/entities/kpiEvaluation.ts @@ -26,9 +26,9 @@ export class createKpiEvaluation { } export class updateKpiEvaluation { - @Column() - description: string; + @Column("uuid") + id: string; @Column() - level: Number; + description: string; }