From 68accac261679e8b322c78c73f553ea2bb1981e8 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 18 Apr 2024 17:40:49 +0700 Subject: [PATCH] capacity controller --- src/controllers/KpiCapacityController.ts | 174 +++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/controllers/KpiCapacityController.ts diff --git a/src/controllers/KpiCapacityController.ts b/src/controllers/KpiCapacityController.ts new file mode 100644 index 0000000..da75c5f --- /dev/null +++ b/src/controllers/KpiCapacityController.ts @@ -0,0 +1,174 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + Example, + SuccessResponse, + Response, + Query, + ArrayValidator +} from "tsoa"; +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 { KpiCapacity, createKpiCapacity, updateKpiCapacity } from "../entities/kpiCapacity"; +import { KpiCapacityDetail, createKpiCapacityDetail, updateKpiCapacityDetail } from "../entities/kpiCapacityDetail"; +import { Like } from "typeorm/browser"; + +@Route("api/v1/kpi/capacity") +@Tags("kpiCapacity") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class kpiCapacityController extends Controller { + private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity); + private kpiCapacityDetailRepository = AppDataSource.getRepository(KpiCapacityDetail); + + /** + * สร้างรายการสมรรถนะ + * @param requestBody + * @param request + */ + @Post() + @Example({ + type: "HEAD", //ประเภทสมรรถนะ + name: "รายการสมรรถะ 1", //ชื่อสมรรถนะ + description: "รายละเอียดรายการสมรรถะ 1", //คำจำกัดความ + kpiCapacityDetails: [{ + level: 1, //ระดับ + description: "คำอธิบายระดับ" //คำอธิบายระดับ + }] + }) + async createKpiCapacity( + // @Body() requestBody: createKpiCapacity, + @Body() requestBody: { + type: string + name: string + description: string + kpiCapacityDetails: { + level: number; + description: string; + }[]; + }, + @Request() request: { user: Record }, + ) { + const kpiCapacity = Object.assign(new KpiCapacity(), requestBody); + kpiCapacity.createdUserId = request.user.sub; + kpiCapacity.createdFullName = request.user.name; + kpiCapacity.lastUpdateUserId = request.user.sub; + kpiCapacity.lastUpdateFullName = request.user.name; + await this.kpiCapacityRepository.save(kpiCapacity); + return new HttpSuccess(kpiCapacity.id); + } + + // /** + // * API แก้ไขรอบการประเมินผลการปฏิบัติหน้าที่ราชการ + // * @param id + // * @param requestBody + // * @param request + // */ + // @Put("{id}") + // async updateKpiPeriod( + // @Path() id: string, + // @Body() requestBody: updateKpiPeriod, + // @Request() request: { user: Record }, + // ) { + // const kpiPeriod = await this.kpiPeriodRepository.findOne({ + // where: { id: id }, + // }); + // if (!kpiPeriod) { + // throw new HttpError( + // HttpStatusCode.NOT_FOUND, + // "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + // ); + // } + + // requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase(); + // 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); + // } + + // /** + // * API รอบการประเมินผลการปฏิบัติหน้าที่ราชการ + // * @param id Guid, *Id รอบการประเมินผลการปฏิบัติหน้าที่ราชการ + // */ + // @Get("{id}") + // @Example({ + // durationKPI: "string", //รอบเดือนที่สร้าง + // startDate: "datetime", //วันเริ่มต้น + // endDate: "datetime", //วันสิ้นสุด + // }) + // async GetKpiPeriodById(@Path() id: string) { + // const kpiPeriod = await this.kpiPeriodRepository.findOne({ + // where: { id: id }, + // select: ["durationKPI", "startDate", "endDate", "isActive"], + // }); + // if (!kpiPeriod) { + // throw new HttpError( + // HttpStatusCode.NOT_FOUND, + // "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + // ); + // } + // return new HttpSuccess(kpiPeriod); + // } + + // /** + // * API list รอบการประเมินผลการปฏิบัติหน้าที่ราชการ + // * @param page + // * @param pageSize + // * @param keyword + // */ + // @Get() + // async listKpiPeriod( + // @Query("page") page: number = 1, + // @Query("pageSize") pageSize: number = 10, + // @Query("year") year?: string, + // @Query("keyword") keyword?: string, + // ) { + // const [kpiPeriod, total] = await this.kpiPeriodRepository.findAndCount({ + // // where: { + // // durationKPI: Like(`%${keyword}%`), + // // }, + // ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), + // }); + + // return new HttpSuccess({ data: kpiPeriod, total }); + // } + + // /** + // * API ลบรอบการประเมินผลการปฏิบัติหน้าที่ราชการ + // * @param id + // */ + // @Delete("{id}") + // async deleteKpiPerriod(@Path() id: string) { + // const kpiPeriod = await this.kpiPeriodRepository.findOne({ + // where: { id: id }, + // }); + // if (!kpiPeriod) { + // throw new HttpError( + // HttpStatusCode.NOT_FOUND, + // "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", + // ); + // } + + // await this.kpiPeriodRepository.remove(kpiPeriod); + // return new HttpSuccess(); + // } +}