diff --git a/src/controllers/KpiCapacityController.ts b/src/controllers/KpiCapacityController.ts index da75c5f..e97db55 100644 --- a/src/controllers/KpiCapacityController.ts +++ b/src/controllers/KpiCapacityController.ts @@ -22,7 +22,7 @@ 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"; +import { Like, In } from "typeorm"; @Route("api/v1/kpi/capacity") @Tags("kpiCapacity") @@ -37,18 +37,18 @@ export class kpiCapacityController extends Controller { private kpiCapacityDetailRepository = AppDataSource.getRepository(KpiCapacityDetail); /** - * สร้างรายการสมรรถนะ - * @param requestBody - * @param request + * API สร้างรายการสมรรถนะ + * + * @summary สร้างรายการสมรรถนะ + * */ @Post() @Example({ - type: "HEAD", //ประเภทสมรรถนะ - name: "รายการสมรรถะ 1", //ชื่อสมรรถนะ - description: "รายละเอียดรายการสมรรถะ 1", //คำจำกัดความ + type: "HEAD", + name: "ชื่อสมรรถนะ", + description: "คำจำกัดความ", kpiCapacityDetails: [{ - level: 1, //ระดับ - description: "คำอธิบายระดับ" //คำอธิบายระดับ + description: "คำอธิบายระดับ" }] }) async createKpiCapacity( @@ -57,118 +57,227 @@ export class kpiCapacityController extends Controller { type: string name: string description: string - kpiCapacityDetails: { - level: number; + capacityDetails: { + // level: number; description: string; }[]; }, @Request() request: { user: Record }, - ) { - const kpiCapacity = Object.assign(new KpiCapacity(), requestBody); + ){ + const kpiCapacity = Object.assign(new KpiCapacity(), { + type: requestBody.type, + name: requestBody.name, + description: requestBody.description + }); 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); + + let idx: number = 0; + for (const data of requestBody.capacityDetails) { + idx += 1 + const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), { + level: idx, + description: data.description, + kpiCapacityId: kpiCapacity.id + }); + kpiCapacityDetail.createdUserId = request.user.sub; + kpiCapacityDetail.createdFullName = request.user.name; + kpiCapacityDetail.lastUpdateUserId = request.user.sub; + kpiCapacityDetail.lastUpdateFullName = request.user.name; + await this.kpiCapacityDetailRepository.save(kpiCapacityDetail); + } + + 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, - // "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", - // ); - // } + /** + * API แก้ไขรายการสมรรถนะ + * + * @summary แก้ไขรายการสมรรถนะ + * + * @param {string} id Guid, *Id รายการสมรรถนะ + */ + @Put("{id}") + @Example({ + type: "HEAD", + name: "ชื่อสมรรถนะ", + description: "คำจำกัดความ", + kpiCapacityDetails: [{ + description: "คำอธิบายระดับ" + }] + }) + async updateKpiCapacity( + @Path() id: string, + @Body() requestBody: { + type: string + name: string + description: string + capacityDetails: { + description: string; + }[]; + }, + @Request() request: { user: Record }, + ) { + const kpiCapacity = await this.kpiCapacityRepository.findOne({ + where: { id: id }, + }); + if (!kpiCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการสมรรถนะนี้", + ); + } + const _kpiCapacity = Object.assign(new KpiCapacity(), { + type: requestBody.type, + name: requestBody.name, + description: requestBody.description + }); + kpiCapacity.lastUpdateUserId = request.user.sub; + kpiCapacity.lastUpdateFullName = request.user.name; + this.kpiCapacityRepository.merge(kpiCapacity, _kpiCapacity); + await this.kpiCapacityRepository.save(kpiCapacity); - // 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); - // } + const _kpiCapacityDetailsOld = await this.kpiCapacityDetailRepository.find({ + where: { kpiCapacityId: kpiCapacity.id }, + }); + await this.kpiCapacityDetailRepository.remove(_kpiCapacityDetailsOld); - // /** - // * 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); - // } + let idx: number = 0; + for (const data of requestBody.capacityDetails) { + idx += 1 + const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), { + level: idx, + description: data.description, + kpiCapacityId: kpiCapacity.id + }); + kpiCapacityDetail.createdUserId = request.user.sub; + kpiCapacityDetail.createdFullName = request.user.name; + kpiCapacityDetail.lastUpdateUserId = request.user.sub; + kpiCapacityDetail.lastUpdateFullName = request.user.name; + await this.kpiCapacityDetailRepository.save(kpiCapacityDetail); + } + + return new HttpSuccess(kpiCapacity.id) + } - // /** - // * 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 }), - // }); + /** + * API รายละเอียดรายการสมรรถนะ + * + * @summary รายละเอียดดรายการสมรรถนะ + * + * @param {string} id Guid, *Id รายการสมรรถนะ + */ + @Get("{id}") + @Example({ + type: "HEAD", + name: "ชื่อสมรรถนะ", + description: "คำจำกัดความ", + kpiCapacityDetails: [{ + level: 1, + description: "คำอธิบายระดับ" + }] + }) + async GetKpiCapacityById(@Path() id: string) { + const kpiCapacity = await this.kpiCapacityRepository.findOne({ + where: { id: id }, + select: ["type", "name", "description"], + }) + if (!kpiCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการสมรรถนะนี้", + ); + } + const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({ + where: { kpiCapacityId: id }, + select: ["level", "description"], + order: { "level": "ASC" } + }) + const mapData = { + type: kpiCapacity.type, + name: kpiCapacity.name, + description: kpiCapacity.description, + capacityDetails: kpiCapacityDetails + } + return new HttpSuccess(mapData); + } - // return new HttpSuccess({ data: kpiPeriod, total }); - // } + /** + * API รายการสมรรถนะ + * + * @summary รายการสมรรถนะ + * + */ + @Get() + async listKpiCapacity( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("type") type?: string, + @Query("keyword") keyword?: string, + ) { - // /** - // * 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, - // "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", - // ); - // } + const [kpiCapacity, total] = await AppDataSource.getRepository(KpiCapacity) + .createQueryBuilder("kpiCapacity") + .leftJoinAndSelect("kpiCapacity.KpiCapacityDetails", "kpiCapacityDetail") + .andWhere( + keyword == undefined + ? "1=1" + : [ + { "kpiCapacity.name": Like(`%${keyword}%`) }, + { "kpiCapacity.description": Like(`%${keyword}%`) }, + ], + ) + .andWhere(type == undefined ? "1=1" : { "kpiCapacity.type": type }) + .orderBy("kpiCapacityDetail.level", "ASC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); - // await this.kpiPeriodRepository.remove(kpiPeriod); - // return new HttpSuccess(); - // } + const mapFormula = kpiCapacity.map((item) => ({ + id: item.id, + type: item.type, + name: item.name, + description: item.description, + capacityDetails: item.KpiCapacityDetails.map(detail => { + return { + id: detail.id, + description: detail.description, + level: detail.level, + capacityId: detail.kpiCapacityId + }; + }) + })); + return new HttpSuccess({ data: mapFormula, total }); + } + + /** + * API ลบรายการสมรรถนะ + * + * @summary ลบรายการสมรรถนะ + * + * @param {string} id Guid, *Id รายการสมรรถนะ + */ + @Delete("{id}") + async deleteKpiCapacity(@Path() id: string) { + const kpiCapacity = await this.kpiCapacityRepository.findOne({ + where: { id: id }, + }); + if (!kpiCapacity) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลรายการสมรรถนะนี้", + ); + } + const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({ + where: { kpiCapacityId: id }, + }); + if (kpiCapacityDetails.length > 0) { + await this.kpiCapacityDetailRepository.remove(kpiCapacityDetails); + } + await this.kpiCapacityRepository.remove(kpiCapacity); + return new HttpSuccess(); + } }