diff --git a/src/controllers/KpiSpecialController.ts b/src/controllers/KpiSpecialController.ts new file mode 100644 index 0000000..fffd3b6 --- /dev/null +++ b/src/controllers/KpiSpecialController.ts @@ -0,0 +1,215 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + Example, + SuccessResponse, + Response, + Query, +} 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 { KpiSpecial, CreateKpiSpecial, UpdateKpiSpecial } from "../entities/kpiSpecial"; +import CallAPI from "../interfaces/call-api"; +import { Brackets, IsNull, Like, Not } from "typeorm"; + +@Route("api/v1/kpi/special") +@Tags("kpiSpecial") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class kpiSpecialController extends Controller { + private kpiSpecialRepository = AppDataSource.getRepository(KpiSpecial); + + /** + * สร้างตัวชี้วัด Special + * @param requestBody + * @param request + */ + @Post() + async createKpiSpecial( + @Body() requestBody: CreateKpiSpecial, + @Request() request: { user: Record }, + ) { + const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ + where: { + including: String(requestBody.including), + includingName: String(requestBody.includingName), + }, + }); + if(chk_kpiSpecial){ + throw new HttpError( + HttpStatusCode.CONFLICT, + "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", + ); + } + const kpiSpecial = Object.assign(new KpiSpecial(), requestBody); + if (!kpiSpecial) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + kpiSpecial.createdUserId = request.user.sub; + kpiSpecial.createdFullName = request.user.name; + kpiSpecial.lastUpdateUserId = request.user.sub; + kpiSpecial.lastUpdateFullName = request.user.name; + await this.kpiSpecialRepository.save(kpiSpecial); + return new HttpSuccess(kpiSpecial.id); + } + + /** + * API แก้ไขตัวชี้วัด Special + * @param id + * @param requestBody + * @param request + */ + @Put("{id}") + async updateKpiSpecial( + @Path() id: string, + @Body() requestBody: UpdateKpiSpecial, + @Request() request: { user: Record }, + ) { + const kpiSpecial = await this.kpiSpecialRepository.findOne({ + where: { id: id }, + }); + if (!kpiSpecial) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); + } + + const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ + where: { + id: Not(id), + including: String(requestBody.including), + includingName: String(requestBody.includingName), + }, + }); + if(chk_kpiSpecial){ + throw new HttpError( + HttpStatusCode.CONFLICT, + "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", + ); + } + kpiSpecial.lastUpdateUserId = request.user.sub; + kpiSpecial.lastUpdateFullName = request.user.name; + this.kpiSpecialRepository.merge(kpiSpecial, requestBody); + await this.kpiSpecialRepository.save(kpiSpecial); + return new HttpSuccess(id); + } + + /** + * API ตัวชี้วัด Special + * @param id Guid, *Id ตัวชี้วัด Special + */ + @Get("{id}") + async GetKpiSpecialById(@Path() id: string) { + const KpiSpecial = await this.kpiSpecialRepository.findOne({ + where: { id: id } + }); + if (!KpiSpecial) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); + } + + const mapData = { + id: KpiSpecial.id, + period: KpiSpecial.period, + year: KpiSpecial.year, + including: KpiSpecial.including, + includingName: KpiSpecial.includingName, + target: KpiSpecial.target, + unit: KpiSpecial.unit, + weight: KpiSpecial.weight, + point: KpiSpecial.point, + summary: KpiSpecial.summary, + documentInfoEvidence: KpiSpecial.documentInfoEvidence, + startDate: KpiSpecial.startDate, + endDate: KpiSpecial.endDate, + achievement1: KpiSpecial.achievement1, + achievement2: KpiSpecial.achievement2, + achievement3: KpiSpecial.achievement3, + achievement4: KpiSpecial.achievement4, + achievement5: KpiSpecial.achievement5, + meaning: KpiSpecial.meaning, + formula: KpiSpecial.formula, + }; + return new HttpSuccess(mapData); + } + + /** + * API list ตัวชี้วัด Special + * @param page + * @param pageSize + * @param keyword + */ + @Get() + async listKpiSpecial( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("keyword") keyword?: string, + ) { + let whereClause: any = {}; + if (keyword !== undefined && keyword !== "") { + whereClause = { + where: [{ + including: Like(`%${keyword}%`), + includingName: Like(`%${keyword}%`), + }], + }; + } + + const [kpiSpecial, total] = await this.kpiSpecialRepository.findAndCount({ + ...whereClause, + ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), + order: { createdAt: "ASC" }, + }); + const mapData = kpiSpecial.map((KpiSpecial) => ({ + id: KpiSpecial.id, + period: KpiSpecial.period, + year: KpiSpecial.year, + including: KpiSpecial.including, + includingName: KpiSpecial.includingName, + target: KpiSpecial.target, + unit: KpiSpecial.unit, + weight: KpiSpecial.weight, + point: KpiSpecial.point, + summary: KpiSpecial.summary, + documentInfoEvidence: KpiSpecial.documentInfoEvidence, + startDate: KpiSpecial.startDate, + endDate: KpiSpecial.endDate, + achievement1: KpiSpecial.achievement1, + achievement2: KpiSpecial.achievement2, + achievement3: KpiSpecial.achievement3, + achievement4: KpiSpecial.achievement4, + achievement5: KpiSpecial.achievement5, + meaning: KpiSpecial.meaning, + formula: KpiSpecial.formula, + })); + return new HttpSuccess({ data: mapData, total }); + } + + /** + * API ลบตัวชี้วัด Special + * @param id + */ + @Delete("{id}") + async deleteKpiSpecial(@Path() id: string) { + const kpiSpecial = await this.kpiSpecialRepository.findOne({ + where: { id: id }, + }); + if (!kpiSpecial) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); + } + await this.kpiSpecialRepository.remove(kpiSpecial); + return new HttpSuccess(); + } +} diff --git a/src/entities/kpiSpecial.ts b/src/entities/kpiSpecial.ts index c15a63e..cf73317 100644 --- a/src/entities/kpiSpecial.ts +++ b/src/entities/kpiSpecial.ts @@ -148,21 +148,21 @@ export class KpiSpecial extends EntityBase { formula: string; } -export class CreateKpiUserSpecial { +export class CreateKpiSpecial { @Column() - including: string | null; + including: string; @Column() - includingName: string | null; + includingName: string; @Column() - achievement1: string | null; + achievement1: string; @Column() - achievement2: string | null; + achievement2: string; @Column() - achievement3: string | null; + achievement3: string; @Column() - achievement4: string | null; + achievement4: string; @Column() - achievement5: string | null; + achievement5: string; @Column() target: string; @Column() @@ -175,23 +175,27 @@ export class CreateKpiUserSpecial { formula: string; @Column("uuid") kpiUserEvaluationId: string; + @Column() + period: string; + @Column() + year: string; } -export class UpdateKpiUserSpecial { +export class UpdateKpiSpecial { @Column() - including: string | null; + including: string; @Column() - includingName: string | null; + includingName: string; @Column() - achievement1: string | null; + achievement1: string; @Column() - achievement2: string | null; + achievement2: string; @Column() - achievement3: string | null; + achievement3: string ; @Column() - achievement4: string | null; + achievement4: string; @Column() - achievement5: string | null; + achievement5: string; @Column() target: string; @Column() @@ -204,6 +208,10 @@ export class UpdateKpiUserSpecial { formula: string; @Column("uuid") kpiUserEvaluationId: string; + @Column() + period: string; + @Column() + year: string; } export class KpiUserSpecialDataPoint {