diff --git a/src/controllers/KpiUserDevelopmentController.ts b/src/controllers/KpiUserDevelopmentController.ts index b5b0cbf..269fb3b 100644 --- a/src/controllers/KpiUserDevelopmentController.ts +++ b/src/controllers/KpiUserDevelopmentController.ts @@ -1,68 +1,75 @@ import { - Controller, - Get, - Post, - Put, - Delete, - Route, - Security, - Tags, - Body, - Path, - Request, - SuccessResponse, - Response, - Query, - } from "tsoa"; - import { AppDataSource } from "../database/data-source"; - import HttpSuccess from "../interfaces/http-success"; - import HttpStatusCode from "../interfaces/http-status"; - import { - KpiUserDevelopment, - CreateKpiUserDevelopment, - UpdateKpiUserDevelopment, - KpiUserDevelopmentDataPoint, - } from "../entities/kpiUserDevelopment"; - import HttpError from "../interfaces/http-error"; - import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; - import { Not } from "typeorm"; - - @Route("api/v1/kpi/user/achievement/evelopment") - @Tags("KpiUserDevelopment") - @Security("bearerAuth") - @Response( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", - ) - @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") - export class KpiUserDevelopmentController extends Controller { - private kpiUserDevelopmentRepository = AppDataSource.getRepository(KpiUserDevelopment); - private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); - - /** - * API เพิ่มพัฒนาตนเอง - * - * @summary - เพิ่มพัฒนาตนเอง # - * - */ - @Post() - async createKpiUserDevelopment( - @Body() - requestBody: CreateKpiUserDevelopment, - @Request() request: { user: Record }, - ) { - const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ - where: { id: requestBody.kpiUserEvaluationId }, - }); - if (!chkUserEvaluation) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); - } - - const kpiUserDevelopment = Object.assign(new KpiUserDevelopment(), requestBody); - if (!kpiUserDevelopment) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } - + Controller, + Get, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + SuccessResponse, + Response, + Query, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatusCode from "../interfaces/http-status"; +import { + KpiUserDevelopment, + CreateKpiUserDevelopment, + UpdateKpiUserDevelopment, + KpiUserDevelopmentDataPoint, +} from "../entities/kpiUserDevelopment"; +import HttpError from "../interfaces/http-error"; +import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; +import { Not, Like } from "typeorm"; + +@Route("api/v1/kpi/user/achievement/development") +@Tags("KpiUserDevelopment") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class KpiUserDevelopmentController extends Controller { + private kpiUserDevelopmentRepository = AppDataSource.getRepository(KpiUserDevelopment); + private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); + + /** + * API เพิ่มพัฒนาตนเอง + * + * @summary - เพิ่มพัฒนาตนเอง # + * + */ + @Post() + async createKpiUserDevelopment( + @Body() + requestBody: CreateKpiUserDevelopment, + @Request() request: { user: Record }, + ) { + const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ + where: { id: requestBody.kpiUserEvaluationId }, + }); + if (!chkUserEvaluation) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); + } + + const chkName = await this.kpiUserDevelopmentRepository.findOne({ + where: { name: requestBody.name}, + }); + if (chkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "มีชื่อนี้ในระบบแล้ว"); + } + + const kpiUserDevelopment = Object.assign(new KpiUserDevelopment(), requestBody); + if (!kpiUserDevelopment) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + // const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({ // where: { // kpiUserEvaluationId: requestBody.kpiUserEvaluationId, @@ -77,39 +84,49 @@ import { // "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", // ); // } - - kpiUserDevelopment.createdUserId = request.user.sub; - kpiUserDevelopment.createdFullName = request.user.name; - kpiUserDevelopment.lastUpdateUserId = request.user.sub; - kpiUserDevelopment.lastUpdateFullName = request.user.name; - await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); - return new HttpSuccess(kpiUserDevelopment.id); + + kpiUserDevelopment.createdUserId = request.user.sub; + kpiUserDevelopment.createdFullName = request.user.name; + kpiUserDevelopment.lastUpdateUserId = request.user.sub; + kpiUserDevelopment.lastUpdateFullName = request.user.name; + await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); + return new HttpSuccess(kpiUserDevelopment.id); + } + + /** + * API แก้ไขพัฒนาตนเอง + * + * @summary - แก้ไขพัฒนาตนเอง # + * + * @param {string} id Id พัฒนาตนเอง + */ + @Put("{id}") + async editKpiUserDevelopment( + @Path() id: string, + @Body() requestBody: UpdateKpiUserDevelopment, + @Request() request: { user: Record }, + ) { + const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ where: { id } }); + if (!kpiUserDevelopment) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้"); + } + const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ + where: { id: requestBody.kpiUserEvaluationId }, + }); + if (!chkUserEvaluation) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); + } + + const chkName = await this.kpiUserDevelopmentRepository.find({ + where: { + id: Not(id), + name: requestBody.name + }, + }); + + if (chkName && chkName.length > 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "มีชื่อนี้ในระบบแล้ว"); } - - /** - * API แก้ไขพัฒนาตนเอง - * - * @summary - แก้ไขพัฒนาตนเอง # - * - * @param {string} id Id พัฒนาตนเอง - */ - @Put("{id}") - async editKpiUserDevelopment( - @Path() id: string, - @Body() requestBody: UpdateKpiUserDevelopment, - @Request() request: { user: Record }, - ) { - const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ where: { id } }); - if (!kpiUserDevelopment) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้"); - } - const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({ - where: { id: requestBody.kpiUserEvaluationId }, - }); - if (!chkUserEvaluation) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน"); - } - // const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({ // where: { // id: Not(id), @@ -125,118 +142,122 @@ import { // "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", // ); // } - - kpiUserDevelopment.lastUpdateUserId = request.user.sub; - kpiUserDevelopment.lastUpdateFullName = request.user.name; - Object.assign(kpiUserDevelopment, requestBody); - await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); - return new HttpSuccess(kpiUserDevelopment.id); - } - - /** - * API ลบพัฒนาตนเอง - * - * @summary - ลบพัฒนาตนเอง # - * - */ - @Delete("{id}") - async deleteKpiUserDevelopment(@Path() id: string) { - const delKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ where: { id } }); - if (!delKpiUserDevelopment) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้"); - } - await this.kpiUserDevelopmentRepository.remove(delKpiUserDevelopment); - return new HttpSuccess(); - } - - /** - * API รายละเอียดพัฒนาตนเอง - * - * @summary - รายละเอียดพัฒนาตนเอง # - * - * @param {string} id Id พัฒนาตนเอง - */ - @Get("{id}") - async GetKpiUserDevelopmentDetail(@Path() id: string) { - const getKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ - relations: ["kpiUserEvaluation"], - where: { id: id }, - }); - if (!getKpiUserDevelopment) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้"); - } - - const mapKpiUserDevelopment = { - id: getKpiUserDevelopment.id, - evaluationId: getKpiUserDevelopment.kpiUserEvaluation.id, - target: getKpiUserDevelopment.target, - summary: getKpiUserDevelopment.summary, - name: getKpiUserDevelopment.name, - achievement10: getKpiUserDevelopment.achievement10, - achievement5: getKpiUserDevelopment.achievement5, - achievement0: getKpiUserDevelopment.achievement0, - }; - - return new HttpSuccess(mapKpiUserDevelopment); - } - - /** - * API รายการพัฒนาตนเอง - * - * @summary - รายการพัฒนาตนเอง # - * - */ - @Get() - async GetKpiUserDevelopment(@Query("id") id: string) { - const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.find({ - where: { - kpiUserEvaluationId: id, - }, - relations: ["kpiUserEvaluation"], - order: { createdAt: "ASC" }, - }); - - const mapKpiUserDevelopment = kpiUserDevelopment.map((item) => ({ - id: item.id, - evaluationId: item.kpiUserEvaluation.id, - target: item.target, - summary: item.summary, - name: item.name, - achievement10: item.achievement10, - achievement5: item.achievement5, - achievement0: item.achievement0, - })); - return new HttpSuccess(mapKpiUserDevelopment); - } - - /** - * API กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี - * - * @summary กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี - * - * - */ - @Post("point") - async CreateKpiUserDevelopmentPoint( - @Body() requestBody: KpiUserDevelopmentDataPoint[], - @Request() request: { user: Record }, - ) { - for (const item of requestBody) { - const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ - where: { id: item.id }, - }); - if (!kpiUserDevelopment) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `ไม่พบข้อมูลพัฒนาตนเองนี้: ${item.id}`, - ); - } - this.kpiUserDevelopmentRepository.merge(kpiUserDevelopment, item); - kpiUserDevelopment.lastUpdateUserId = request.user.sub; - kpiUserDevelopment.lastUpdateFullName = request.user.name; - await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); - } - return new HttpSuccess(); - } + + kpiUserDevelopment.lastUpdateUserId = request.user.sub; + kpiUserDevelopment.lastUpdateFullName = request.user.name; + Object.assign(kpiUserDevelopment, requestBody); + await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); + return new HttpSuccess(kpiUserDevelopment.id); } - \ No newline at end of file + + /** + * API ลบพัฒนาตนเอง + * + * @summary - ลบพัฒนาตนเอง # + * + */ + @Delete("{id}") + async deleteKpiUserDevelopment(@Path() id: string) { + const delKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ + where: { id }, + }); + if (!delKpiUserDevelopment) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้"); + } + await this.kpiUserDevelopmentRepository.remove(delKpiUserDevelopment); + return new HttpSuccess(); + } + + /** + * API รายละเอียดพัฒนาตนเอง + * + * @summary - รายละเอียดพัฒนาตนเอง # + * + * @param {string} id Id พัฒนาตนเอง + */ + @Get("{id}") + async GetKpiUserDevelopmentDetail(@Path() id: string) { + const getKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ + relations: ["kpiUserEvaluation"], + where: { id: id }, + }); + if (!getKpiUserDevelopment) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้"); + } + + const mapKpiUserDevelopment = { + id: getKpiUserDevelopment.id, + evaluationId: getKpiUserDevelopment.kpiUserEvaluation.id, + target: getKpiUserDevelopment.target, + summary: getKpiUserDevelopment.summary, + name: getKpiUserDevelopment.name, + achievement10: getKpiUserDevelopment.achievement10, + achievement5: getKpiUserDevelopment.achievement5, + achievement0: getKpiUserDevelopment.achievement0, + isDevelopment70: getKpiUserDevelopment.isDevelopment70, + isDevelopment20: getKpiUserDevelopment.isDevelopment20, + isDevelopment10: getKpiUserDevelopment.isDevelopment10, + }; + + return new HttpSuccess(mapKpiUserDevelopment); + } + + /** + * API รายการพัฒนาตนเอง + * + * @summary - รายการพัฒนาตนเอง # + * + */ + @Get() + async GetKpiUserDevelopment(@Query("id") id: string) { + const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.find({ + where: { + kpiUserEvaluationId: id, + }, + relations: ["kpiUserEvaluation"], + order: { createdAt: "ASC" }, + }); + + const mapKpiUserDevelopment = kpiUserDevelopment.map((item) => ({ + id: item.id, + evaluationId: item.kpiUserEvaluation.id, + target: item.target, + summary: item.summary, + name: item.name, + achievement10: item.achievement10, + achievement5: item.achievement5, + achievement0: item.achievement0, + isDevelopment70: item.isDevelopment70, + isDevelopment20: item.isDevelopment20, + isDevelopment10: item.isDevelopment10, + })); + return new HttpSuccess(mapKpiUserDevelopment); + } + +// /** +// * API กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี +// * +// * @summary กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี +// * +// * +// */ +// @Post("point") +// async CreateKpiUserDevelopmentPoint( +// @Body() requestBody: KpiUserDevelopmentDataPoint[], +// @Request() request: { user: Record }, +// ) { +// for (const item of requestBody) { +// const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ +// where: { id: item.id }, +// }); +// if (!kpiUserDevelopment) { +// throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลพัฒนาตนเองนี้: ${item.id}`); +// } +// this.kpiUserDevelopmentRepository.merge(kpiUserDevelopment, item); +// kpiUserDevelopment.lastUpdateUserId = request.user.sub; +// kpiUserDevelopment.lastUpdateFullName = request.user.name; +// await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment); +// } +// return new HttpSuccess(); +// } +} diff --git a/src/entities/kpiUserDevelopment.ts b/src/entities/kpiUserDevelopment.ts index 6fdce78..2e24fa0 100644 --- a/src/entities/kpiUserDevelopment.ts +++ b/src/entities/kpiUserDevelopment.ts @@ -87,7 +87,7 @@ export class KpiUserDevelopment extends EntityBase { export class CreateKpiUserDevelopment { @Column() - name: string | null; + name: string; @Column() target: string | null; @Column() @@ -110,7 +110,7 @@ export class CreateKpiUserDevelopment { export class UpdateKpiUserDevelopment { @Column() - name: string | null; + name: string; @Column() target: string | null; @Column()