diff --git a/src/controllers/KpiPlanController.ts b/src/controllers/KpiPlanController.ts index a71a9e1..fb5bfa2 100644 --- a/src/controllers/KpiPlanController.ts +++ b/src/controllers/KpiPlanController.ts @@ -485,6 +485,10 @@ export class kpiPlanController extends Controller { keyword: `%${requestBody.keyword}%`, }).orWhere("kpiPlan.includingName LIKE :keyword", { keyword: `%${requestBody.keyword}%`, + }).orWhere("kpiPlan.year LIKE :keyword",{ + keyword: `%${requestBody.keyword}%` + }).orWhere("kpiPlan.period LIKE :keyword",{ + keyword: `%${requestBody.keyword}%` }); }), ) diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index fe98487..987a653 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -450,10 +450,15 @@ export class kpiRoleController extends Controller { }) .andWhere( new Brackets((qb) => { - qb.orWhere("kpiRole.including LIKE :keyword", { keyword: `%${requestBody.keyword}%` }).orWhere( - "kpiRole.includingName LIKE :keyword", - { keyword: `%${requestBody.keyword}%` }, - ); + qb.orWhere("kpiRole.including LIKE :keyword", { + keyword: `%${requestBody.keyword}%` + }).orWhere("kpiRole.includingName LIKE :keyword",{ + keyword: `%${requestBody.keyword}%` + }).orWhere("kpiRole.year LIKE :keyword",{ + keyword: `%${requestBody.keyword}%` + }).orWhere("kpiRole.period LIKE :keyword",{ + keyword: `%${requestBody.keyword}%` + }); }), ) .select([ diff --git a/src/controllers/KpiUserDevelopmentController.ts b/src/controllers/KpiUserDevelopmentController.ts new file mode 100644 index 0000000..5bc4ea3 --- /dev/null +++ b/src/controllers/KpiUserDevelopmentController.ts @@ -0,0 +1,280 @@ +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/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 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, + // }, + // }); + // if ( + // (chk_indicator && chk_indicator.including == requestBody.including) || + // (chk_indicator && chk_indicator.includingName == requestBody.includingName) + // ) { + // throw new HttpError( + // HttpStatusCode.CONFLICT, + // "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", + // ); + // } + + 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 chk_indicator = await this.kpiUserDevelopmentRepository.findOne({ + // where: { + // id: Not(id), + // kpiUserEvaluationId: requestBody.kpiUserEvaluationId, + // }, + // }); + // if ( + // (chk_indicator && chk_indicator.including == requestBody.including) || + // (chk_indicator && chk_indicator.includingName == requestBody.includingName) + // ) { + // throw new HttpError( + // HttpStatusCode.CONFLICT, + // "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", + // ); + // } + + 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, + // including: getKpiUserDevelopment.including, + // includingName: getKpiUserDevelopment.includingName, + // target: getKpiUserDevelopment.target, + // weight: getKpiUserDevelopment.weight, + // unit: getKpiUserDevelopment.unit, + // meaning: getKpiUserDevelopment.meaning, + // formula: getKpiUserDevelopment.formula, + // point: getKpiUserDevelopment.point, + // achievement: + // getKpiUserDevelopment.point === 1 + // ? getKpiUserDevelopment.achievement1 + // : getKpiUserDevelopment.point === 2 + // ? getKpiUserDevelopment.achievement2 + // : getKpiUserDevelopment.point === 3 + // ? getKpiUserDevelopment.achievement3 + // : getKpiUserDevelopment.point === 4 + // ? getKpiUserDevelopment.achievement4 + // : getKpiUserDevelopment.point === 5 + // ? getKpiUserDevelopment.achievement5 + // : null, + // achievement1: getKpiUserDevelopment.achievement1, + // achievement2: getKpiUserDevelopment.achievement2, + // achievement3: getKpiUserDevelopment.achievement3, + // achievement4: getKpiUserDevelopment.achievement4, + // achievement5: getKpiUserDevelopment.achievement5, + // }; + + // 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, + // including: item.including, + // includingName: item.includingName, + // target: item.target, + // weight: item.weight, + // unit: item.unit, + // meaning: item.meaning, + // formula: item.formula, + // point: item.point, + // achievement: + // item.point === 1 + // ? item.achievement1 + // : item.point === 2 + // ? item.achievement2 + // : item.point === 3 + // ? item.achievement3 + // : item.point === 4 + // ? item.achievement4 + // : item.point === 5 + // ? item.achievement5 + // : null, + // achievement1: item.achievement1, + // achievement2: item.achievement2, + // achievement3: item.achievement3, + // achievement4: item.achievement4, + // achievement5: item.achievement5, + // })); + // 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(); + } + } + \ No newline at end of file diff --git a/src/entities/kpiUserDevelopment.ts b/src/entities/kpiUserDevelopment.ts index 3ee07e1..0843439 100644 --- a/src/entities/kpiUserDevelopment.ts +++ b/src/entities/kpiUserDevelopment.ts @@ -60,7 +60,7 @@ export class KpiUserDevelopment extends EntityBase { kpiUserEvaluation: KpiUserEvaluation; } -export class CreateKpiUserSpecial { +export class CreateKpiUserDevelopment { @Column() including: string | null; @Column() @@ -89,7 +89,7 @@ export class CreateKpiUserSpecial { kpiUserEvaluationId: string; } -export class UpdateKpiUserSpecial { +export class UpdateKpiUserDevelopment { @Column() including: string | null; @Column() @@ -118,7 +118,7 @@ export class UpdateKpiUserSpecial { kpiUserEvaluationId: string; } -export class KpiUserSpecialDataPoint { +export class KpiUserDevelopmentDataPoint { id: string; point: number; }