From 6499cb3da09e114ff1d70d29f4ab6e650f445685 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 19 Apr 2024 16:41:54 +0700 Subject: [PATCH] =?UTF-8?q?api=20=E0=B8=AA=E0=B8=A1=E0=B8=A3=E0=B8=A3?= =?UTF-8?q?=E0=B8=96=E0=B8=99=E0=B8=B0=20=E0=B8=82=E0=B8=B2=E0=B8=94?= =?UTF-8?q?=E0=B8=A5=E0=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/KpiLinkController.ts | 105 +++++++++++++++++++-------- 1 file changed, 75 insertions(+), 30 deletions(-) diff --git a/src/controllers/KpiLinkController.ts b/src/controllers/KpiLinkController.ts index 02b72c1..ca0bdaf 100644 --- a/src/controllers/KpiLinkController.ts +++ b/src/controllers/KpiLinkController.ts @@ -18,7 +18,7 @@ import { import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; -import { Like, Not, In } from "typeorm"; +import { Like, Not, In, Brackets } from "typeorm"; import HttpStatusCode from "../interfaces/http-status"; import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink"; import { KpiGroup } from "../entities/kpiGroup"; @@ -169,25 +169,29 @@ export class kpiLinkController extends Controller { async KpiLinkById(@Path() id: string) { const kpiLink = await this.kpiLinkRepository.findOne({ where: { id: id }, - relations:["positions","kpiCapacitys"] + relations:["positions","kpiCapacitys","kpiGroup"], + order:{ + createdAt: "ASC" + } }); if (!kpiLink) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); } const formattedResponse = { id: kpiLink.id, + groupName: kpiLink.kpiGroup.nameGroupKPI, positions: kpiLink.positions.map(position => ({ id: position.id, name: position.name })), - kpiCapacitys: kpiLink.kpiCapacitys.map(capacity => ({ + capacitys: kpiLink.kpiCapacitys.map(capacity => ({ id: capacity.id , name: capacity.name, type: capacity.type, description: capacity.description })) }; - return new HttpSuccess(kpiLink); + return new HttpSuccess(formattedResponse); } // /** @@ -207,30 +211,71 @@ export class kpiLinkController extends Controller { // return new HttpSuccess(); // } - // /** - // * API list เชื่อมโยง - // * @param page - // * @param pageSize - // */ - // @Get() - // async listKpiLink( - // @Query("page") page: number = 1, - // @Query("pageSize") pageSize: number = 10, - // @Query("keyword") keyword?: string, - // ) { - // let whereClause: any = {}; - - // if (keyword !== undefined && keyword !== "") { - // whereClause = { - // where: [{ nameLinkKPI: Like(`%${keyword}%`) }], - // }; - // } - - // const [kpiLink, total] = await this.kpiLinkRepository.findAndCount({ - // ...whereClause, - // ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - // }); - - // return new HttpSuccess({ data: kpiLink, total }); - // } + /** + * API list เชื่อมโยง + * @param page + * @param pageSize + */ + @Get() + async listKpiLink( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("keyword") keyword?: string, + ) { + const [kpiLink , total] = await AppDataSource.getRepository(KpiLink) + .createQueryBuilder("kpiLink") + .leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup") + .leftJoinAndSelect("kpiLink.positions", "positions") + .leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys") + .andWhere( + new Brackets((qb) => { + qb.where( + keyword != null && keyword != "" + ? "kpiGroup.nameGroupKPI LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != null && keyword != "" + ? "positions.name LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != null && keyword != "" + ? "kpiCapacitys.name LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + }), + ) + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + if (!kpiLink) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + } + const formattedResponse = kpiLink.map((item) => ({ + id: item.id, + groupName: item.kpiGroup.nameGroupKPI, + positions: item.positions.map(position => ({ + id: position.id, + name: position.name + })), + capacitys: item.kpiCapacitys.map(capacity => ({ + id: capacity.id , + name: capacity.name, + type: capacity.type, + description: capacity.description + })) + })); + return new HttpSuccess({ data: formattedResponse, total }); + } }