From f7fc9b6c23df4e660f48e176cf25b6fd56ba1f0c Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 23 Apr 2024 12:35:03 +0700 Subject: [PATCH] userCapacity Controller --- src/controllers/KpiUserCapacityController.ts | 226 +++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 src/controllers/KpiUserCapacityController.ts diff --git a/src/controllers/KpiUserCapacityController.ts b/src/controllers/KpiUserCapacityController.ts new file mode 100644 index 0000000..6b16350 --- /dev/null +++ b/src/controllers/KpiUserCapacityController.ts @@ -0,0 +1,226 @@ +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 } from "../entities/kpiCapacity"; +import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; +import { KpiUserCapacity, } from "../entities/kpiUserCapacity"; +import { Like, In } from "typeorm"; + +@Route("api/v1/kpi/user/capacity") +@Tags("kpiUserCapacity") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class KpiUserCapacityController extends Controller { + private kpiUserEvalutionRepository = AppDataSource.getRepository(KpiUserEvaluation); + private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity); + private kpiUserCapacityRepository = AppDataSource.getRepository(KpiUserCapacity); + + /** + * API สร้างองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + * @summary สร้างองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + * + */ + @Post() + async CreateKpiUserCapacity( + @Body() requestBody: { + kpiUserEvaluationId: string; + kpiCapacityId: string; + level: string; + point: number; + weight: number; + }, + @Request() request: { user: Record }, + ){ + const kpiUserEvalution = await this.kpiUserEvalutionRepository.findOne({ + where: { id: requestBody.kpiUserEvaluationId }, + }); + if (!kpiUserEvalution) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ)นี้", + ); + } + const kpiCapacity = await this.kpiCapacityRepository.findOne({ + where: { id: requestBody.kpiCapacityId }, + }); + if (!kpiCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการสมรรถนะนี้", + ); + } + const kpiUserCapacity = Object.assign(new KpiUserCapacity(), requestBody); + kpiUserCapacity.createdUserId = request.user.sub; + kpiUserCapacity.createdFullName = request.user.name; + kpiUserCapacity.lastUpdateUserId = request.user.sub; + 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: { + kpiUserEvaluationId: string; + kpiCapacityId: string; + level: string; + point: number; + weight: number; + }, + @Request() request: { user: Record }, + ) { + + const kpiUserEvalution = await this.kpiUserEvalutionRepository.findOne({ + where: { id: requestBody.kpiUserEvaluationId }, + }); + if (!kpiUserEvalution) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", + ); + } + const kpiCapacity = await this.kpiCapacityRepository.findOne({ + where: { id: requestBody.kpiCapacityId }, + }); + if (!kpiCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการสมรรถนะนี้", + ); + } + const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({ + where: { id: id }, + }); + if (!kpiUserCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) นี้", + ); + } + const _kpiUserCapacity = Object.assign(new KpiUserCapacity(), requestBody); + kpiUserCapacity.lastUpdateUserId = request.user.sub; + kpiUserCapacity.lastUpdateFullName = request.user.name; + this.kpiUserCapacityRepository.merge(kpiUserCapacity, _kpiUserCapacity); + await this.kpiUserCapacityRepository.save(kpiUserCapacity); + return new HttpSuccess(kpiUserCapacity.id); + + } + + /** + * API รายละเอียดองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + * @summary รายละเอียดองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + * @param {string} id Guid, *Id องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + */ + @Get("{id}") + async GetKpiUserCapacityById(@Path() id: string) { + const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({ + where: { id: id }, + relations: ["kpiCapacity"] + }) + if (!kpiUserCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ)นี้", + ); + } + const mapData = { + id: kpiUserCapacity.id, + // capacityId: kpiUserCapacity.kpiCapacity.id, + name: kpiUserCapacity.kpiCapacity.name, + level: kpiUserCapacity.level, + point: kpiUserCapacity.point, + weight: kpiUserCapacity.weight, + summary: kpiUserCapacity.summary + } + return new HttpSuccess(mapData); + } + + /** + * API องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + * @summary องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + */ + @Get() + async listKpiUserCapacity( + @Query("id") id: string, //kpiUserEvaluationId + @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(); + + const mapData = kpiUserCapacity.map((item) => ({ + id: item.id, + name: item.kpiCapacity.name, + level: item.level, + point: item.point, + weight: item.weight, + summary: item.summary, + })); + + return new HttpSuccess({ data: mapData, total }); + } + + /** + * API ลบองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + * @summary ลบองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + * + * @param {string} id Guid, *Id องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER) + */ + @Delete("{id}") + async deleteKpiUserCapacity(@Path() id: string) { + const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({ + where: { id: id }, + }); + if (!kpiUserCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", + ); + } + await this.kpiUserCapacityRepository.remove(kpiUserCapacity); + return new HttpSuccess(); + } +}