From c86a88d32b8d0e98df9f7136e206faf0b154c87d Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Thu, 18 Apr 2024 10:24:18 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84=E0=B8=82?= =?UTF-8?q?=20KpiGroup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/KpiGroupController.ts | 88 +++++++++++++++++------- src/controllers/KpiPeriodController.ts | 68 +++++++++--------- src/entities/{kpiGrop.ts => kpiGroup.ts} | 0 src/entities/{kpi.ts => kpiPeriod.ts} | 0 tsoa.json | 6 ++ 5 files changed, 103 insertions(+), 59 deletions(-) rename src/entities/{kpiGrop.ts => kpiGroup.ts} (100%) rename src/entities/{kpi.ts => kpiPeriod.ts} (100%) diff --git a/src/controllers/KpiGroupController.ts b/src/controllers/KpiGroupController.ts index 4db8e51..a59ec34 100644 --- a/src/controllers/KpiGroupController.ts +++ b/src/controllers/KpiGroupController.ts @@ -19,9 +19,9 @@ 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 { KpiGroup, creatKpiGroup, updateKpiGroup } from "../entities/kpiGrop"; +import { KpiGroup, creatKpiGroup, updateKpiGroup } from "../entities/kpiGroup"; @Route("api/v1/kpi/group") -@Tags("kpi") +@Tags("kpiGroup") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, @@ -45,14 +45,21 @@ export class kpiGroupController extends Controller { @Body() requestBody: creatKpiGroup, @Request() request: { user: Record }, ) { - const kpi = Object.assign(new KpiGroup(), requestBody); - kpi.nameGroupKPI = requestBody.nameGroupKPI; - kpi.createdUserId = request.user.sub; - kpi.createdFullName = request.user.name; - kpi.lastUpdateUserId = request.user.sub; - kpi.lastUpdateFullName = request.user.name; - await this.kpiGroupRepository.save(kpi); - return new HttpSuccess(kpi.id); + const kpiGroup = Object.assign(new KpiGroup(), requestBody); + const ChkkpinameGroup = await this.kpiGroupRepository.findOne({ + where: { + nameGroupKPI: requestBody.nameGroupKPI, + }, + }); + if (ChkkpinameGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว"); + } + kpiGroup.createdUserId = request.user.sub; + kpiGroup.createdFullName = request.user.name; + kpiGroup.lastUpdateUserId = request.user.sub; + kpiGroup.lastUpdateFullName = request.user.name; + await this.kpiGroupRepository.save(kpiGroup); + return new HttpSuccess(kpiGroup.id); } /** @@ -65,20 +72,27 @@ export class kpiGroupController extends Controller { @Body() requestBody: updateKpiGroup, @Request() request: { user: Record }, ) { - const kpiUpdate = await this.kpiGroupRepository.findOne({ + const kpiGroup = await this.kpiGroupRepository.findOne({ where: { id: id }, }); - if (!kpiUpdate) { + if (!kpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } - requestBody.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase(); - this.kpiGroupRepository.merge(kpiUpdate, requestBody); - kpiUpdate.createdUserId = request.user.sub; - kpiUpdate.createdFullName = request.user.name; - kpiUpdate.lastUpdateUserId = request.user.sub; - kpiUpdate.lastUpdateFullName = request.user.name; - await this.kpiGroupRepository.save(kpiUpdate); + const ChkkpinameGroup = await this.kpiGroupRepository.findOne({ + where: { + nameGroupKPI: requestBody.nameGroupKPI, + }, + }); + if (ChkkpinameGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว"); + } + + requestBody.nameGroupKPI = requestBody.nameGroupKPI; + this.kpiGroupRepository.merge(kpiGroup, requestBody); + kpiGroup.lastUpdateUserId = request.user.sub; + kpiGroup.lastUpdateFullName = request.user.name; + await this.kpiGroupRepository.save(kpiGroup); return new HttpSuccess(id); } @@ -91,14 +105,14 @@ export class kpiGroupController extends Controller { nameGroupKPI: "string", //ชื่อกลุ่มงาน }) async KpiGroupById(@Path() id: string) { - const kpi = await this.kpiGroupRepository.findOne({ + const kpiGroup = await this.kpiGroupRepository.findOne({ where: { id: id }, select: ["nameGroupKPI"], }); - if (!kpi) { + if (!kpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } - return new HttpSuccess(kpi); + return new HttpSuccess(kpiGroup); } /** @@ -107,14 +121,38 @@ export class kpiGroupController extends Controller { */ @Delete("{id}") async deleteKpiGroup(@Path() id: string) { - const chkKpiGroup = await this.kpiGroupRepository.findOne({ + const kpiGroup = await this.kpiGroupRepository.findOne({ where: { id: id }, }); - if (!chkKpiGroup) { + if (!kpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้"); } - await this.kpiGroupRepository.remove(chkKpiGroup); + await this.kpiGroupRepository.remove(kpiGroup); return new HttpSuccess(); } + + /** + * API list กลุ่มงาน + * @param page + * @param pageSize + * @param year + * @param keyword + */ + @Get() + async listKpiGroup( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("year") year?: string, + @Query("keyword") keyword?: string, + ) { + const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({ + // where: { + // name: Like(`%${keyword}%`), + // }, + ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), + }); + + return new HttpSuccess({ data: kpiGroup, total }); + } } diff --git a/src/controllers/KpiPeriodController.ts b/src/controllers/KpiPeriodController.ts index 9156ef7..d580399 100644 --- a/src/controllers/KpiPeriodController.ts +++ b/src/controllers/KpiPeriodController.ts @@ -19,10 +19,10 @@ 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 { KpiPeriod, createKpiPeriod, updateKpiPeriod } from "../entities/kpi"; +import { KpiPeriod, createKpiPeriod, updateKpiPeriod } from "../entities/kpiPeriod"; @Route("api/v1/kpi/period") -@Tags("kpi") +@Tags("kpiPeriod") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, @@ -46,14 +46,14 @@ export class kpiController extends Controller { @Body() requestBody: createKpiPeriod, @Request() request: { user: Record }, ) { - const kpi = Object.assign(new KpiPeriod(), requestBody); - kpi.durationKPI = requestBody.durationKPI.trim().toUpperCase(); - kpi.createdUserId = request.user.sub; - kpi.createdFullName = request.user.name; - kpi.lastUpdateUserId = request.user.sub; - kpi.lastUpdateFullName = request.user.name; - await this.kpiPeriodRepository.save(kpi); - return new HttpSuccess(kpi.id); + const kpiPeriod = Object.assign(new KpiPeriod(), requestBody); + kpiPeriod.durationKPI = requestBody.durationKPI.trim().toUpperCase(); + kpiPeriod.createdUserId = request.user.sub; + kpiPeriod.createdFullName = request.user.name; + kpiPeriod.lastUpdateUserId = request.user.sub; + kpiPeriod.lastUpdateFullName = request.user.name; + await this.kpiPeriodRepository.save(kpiPeriod); + return new HttpSuccess(kpiPeriod.id); } /** @@ -68,10 +68,10 @@ export class kpiController extends Controller { @Body() requestBody: updateKpiPeriod, @Request() request: { user: Record }, ) { - const kpiUpdate = await this.kpiPeriodRepository.findOne({ + const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: id }, }); - if (!kpiUpdate) { + if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", @@ -79,12 +79,12 @@ export class kpiController extends Controller { } requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase(); - this.kpiPeriodRepository.merge(kpiUpdate, requestBody); - kpiUpdate.createdUserId = request.user.sub; - kpiUpdate.createdFullName = request.user.name; - kpiUpdate.lastUpdateUserId = request.user.sub; - kpiUpdate.lastUpdateFullName = request.user.name; - await this.kpiPeriodRepository.save(kpiUpdate); + this.kpiPeriodRepository.merge(kpiPeriod, requestBody); + kpiPeriod.createdUserId = request.user.sub; + kpiPeriod.createdFullName = request.user.name; + kpiPeriod.lastUpdateUserId = request.user.sub; + kpiPeriod.lastUpdateFullName = request.user.name; + await this.kpiPeriodRepository.save(kpiPeriod); return new HttpSuccess(id); } @@ -99,18 +99,18 @@ export class kpiController extends Controller { endDate: "datetime", //วันสิ้นสุด }) async CloseKpiPeriodById(@Path() id: string) { - const kpi = await this.kpiPeriodRepository.findOne({ + const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: id }, }); - if (!kpi) { + if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } - kpi.isActive = false; - await this.kpiPeriodRepository.save(kpi); - return new HttpSuccess(kpi); + kpiPeriod.isActive = false; + await this.kpiPeriodRepository.save(kpiPeriod); + return new HttpSuccess(kpiPeriod); } /** @@ -124,18 +124,18 @@ export class kpiController extends Controller { endDate: "datetime", //วันสิ้นสุด }) async OpenKpiPeriodById(@Path() id: string) { - const kpi = await this.kpiPeriodRepository.findOne({ + const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: id }, }); - if (!kpi) { + if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } - kpi.isActive = true; - await this.kpiPeriodRepository.save(kpi); - return new HttpSuccess(kpi); + kpiPeriod.isActive = true; + await this.kpiPeriodRepository.save(kpiPeriod); + return new HttpSuccess(kpiPeriod); } /** @@ -149,17 +149,17 @@ export class kpiController extends Controller { endDate: "datetime", //วันสิ้นสุด }) async GetKpiPeriodById(@Path() id: string) { - const kpi = await this.kpiPeriodRepository.findOne({ + const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: id }, select: ["durationKPI", "startDate", "endDate", "isActive"], }); - if (!kpi) { + if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } - return new HttpSuccess(kpi); + return new HttpSuccess(kpiPeriod); } /** @@ -191,17 +191,17 @@ export class kpiController extends Controller { */ @Delete("{id}") async deleteKpiPerriod(@Path() id: string) { - const chkKpiPeriod = await this.kpiPeriodRepository.findOne({ + const kpiPeriod = await this.kpiPeriodRepository.findOne({ where: { id: id }, }); - if (!chkKpiPeriod) { + if (!kpiPeriod) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } - await this.kpiPeriodRepository.remove(chkKpiPeriod); + await this.kpiPeriodRepository.remove(kpiPeriod); return new HttpSuccess(); } } diff --git a/src/entities/kpiGrop.ts b/src/entities/kpiGroup.ts similarity index 100% rename from src/entities/kpiGrop.ts rename to src/entities/kpiGroup.ts diff --git a/src/entities/kpi.ts b/src/entities/kpiPeriod.ts similarity index 100% rename from src/entities/kpi.ts rename to src/entities/kpiPeriod.ts diff --git a/tsoa.json b/tsoa.json index c31f37c..caa5ec3 100644 --- a/tsoa.json +++ b/tsoa.json @@ -28,6 +28,12 @@ "tags": [ { "name": "Test", "description": "สำหรับทดสอบ" + }, + { + "name": "kpiGroup", "description": "ชื่อกลุ่มงาน" + }, + { + "name": "kpiPeriod", "description": "รอบKpi" } ] },