From 19ea1c54681f670678ba5463756a1645d12957cf Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 19 Apr 2024 16:11:46 +0700 Subject: [PATCH 1/5] add position --- src/controllers/KpiRoleController.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index d0390a5..84bfbaf 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -194,6 +194,7 @@ export class kpiRoleController extends Controller { @Query("nodeId") nodeId?: string | null, @Query("node") node?: number | null, @Query("keyword") keyword?: string, + @Query("position") position?: string, ) { const [kpiRole, total] = await AppDataSource.getRepository(KpiRole) .createQueryBuilder("kpiRole") @@ -222,6 +223,9 @@ export class kpiRoleController extends Controller { .andWhere(keyword != undefined ? "kpiRole.projectName LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }) + .andWhere(position != undefined ? "kpiRole.projectName LIKE :position" : "1=1", { + position: `%${position}%`, + }) .select([ "kpiRole.id", "kpiRole.year", From b70f447174c1e05f5eb78ebb019340da958339e2 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 19 Apr 2024 16:18:24 +0700 Subject: [PATCH 2/5] fix unkwons field Projectname --- src/controllers/KpiPlanController.ts | 3 --- src/controllers/KpiRoleController.ts | 5 +---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/controllers/KpiPlanController.ts b/src/controllers/KpiPlanController.ts index d7ea759..d863b2b 100644 --- a/src/controllers/KpiPlanController.ts +++ b/src/controllers/KpiPlanController.ts @@ -275,9 +275,6 @@ export class kpiPlanController extends Controller { round: `${round?.trim().toUpperCase()}`, }, ) - .andWhere(keyword != undefined ? "kpiPlan.projectName LIKE :keyword" : "1=1", { - keyword: `%${keyword}%`, - }) .select([ "kpiPlan.id", "kpiPlan.year", diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index 84bfbaf..25703ff 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -220,10 +220,7 @@ export class kpiRoleController extends Controller { round: `${round?.trim().toUpperCase()}`, }, ) - .andWhere(keyword != undefined ? "kpiRole.projectName LIKE :keyword" : "1=1", { - keyword: `%${keyword}%`, - }) - .andWhere(position != undefined ? "kpiRole.projectName LIKE :position" : "1=1", { + .andWhere(position != undefined ? "kpiRole.position LIKE :position" : "1=1", { position: `%${position}%`, }) .select([ From 85f39a55a649d9979386e99666ab8de282740f24 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 19 Apr 2024 16:19:09 +0700 Subject: [PATCH 3/5] checkpoint --- src/controllers/KpiEvaluationController.ts | 30 +- src/controllers/KpiLinkController.ts | 375 +++++++++++++-------- src/entities/kpiLink.ts | 21 ++ 3 files changed, 260 insertions(+), 166 deletions(-) diff --git a/src/controllers/KpiEvaluationController.ts b/src/controllers/KpiEvaluationController.ts index bb263a0..057b6f7 100644 --- a/src/controllers/KpiEvaluationController.ts +++ b/src/controllers/KpiEvaluationController.ts @@ -70,27 +70,23 @@ async updateKpiEvaluations( */ @Get() async listKpiEvaluation( - // @Query("page") page: number = 1, - // @Query("pageSize") pageSize: number = 10, - // @Query("keyword") keyword?: string, + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("keyword") keyword?: string, ) { let whereClause: any = {}; - // if (keyword !== undefined && keyword !== "") { - // whereClause = { - // where: [{ description: Like(`%${keyword}%`) }], - // }; - // whereClause.where.push({ level: Like(`%${keyword}%`) }); + if (keyword !== undefined && keyword !== "") { + whereClause = { + where: [{ description: Like(`%${keyword}%`) }], + }; + whereClause.where.push({ level: Like(`%${keyword}%`) }); - // } + } - // const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({ - // ...whereClause, - // ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - // order:{ - // level: "DESC"} - // }); - const kpiEvaluation = await this.kpiEvaluationRepository.find({ + const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({ + ...whereClause, + ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), order:{ level: "DESC"} }); @@ -100,6 +96,6 @@ async updateKpiEvaluations( level: item.level, description: item.description })); - return new HttpSuccess(formatted); + return new HttpSuccess({ data: formatted, total }); } } diff --git a/src/controllers/KpiLinkController.ts b/src/controllers/KpiLinkController.ts index 39c0d57..02b72c1 100644 --- a/src/controllers/KpiLinkController.ts +++ b/src/controllers/KpiLinkController.ts @@ -1,159 +1,236 @@ -// 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 { Like, Not } from "typeorm"; -// import HttpStatusCode from "../interfaces/http-status"; -// import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink"; -// @Route("api/v1/kpi/link") -// @Tags("kpiLink") -// @Security("bearerAuth") -// @Response( -// HttpStatusCode.INTERNAL_SERVER_ERROR, -// "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", -// ) -// @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") -// export class kpiLinkController extends Controller { -// private kpiLinkRepository = AppDataSource.getRepository(KpiLink); +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 { Like, Not, In } from "typeorm"; +import HttpStatusCode from "../interfaces/http-status"; +import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink"; +import { KpiGroup } from "../entities/kpiGroup"; +import { KpiCapacity } from "../entities/kpiCapacity"; +import { Position } from "../entities/position"; +@Route("api/v1/kpi/link") +@Tags("kpiLink") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class kpiLinkController extends Controller { + private kpiGroupRepository = AppDataSource.getRepository(KpiGroup); + private kpiLinkRepository = AppDataSource.getRepository(KpiLink); + private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity); + private positionRepository = AppDataSource.getRepository(Position); -// /** -// * API สร้างเชื่อมโยง -// * @param requestBody -// * @returns -// */ -// @Post() -// @Example({ -// nameLinkKPI: "string", //ชื่อเชื่อมโยง -// }) -// async createKpiLink( -// @Body() requestBody: createKpiLink, -// @Request() request: { user: Record }, -// ) { -// const kpiLink = Object.assign(new KpiLink(), requestBody); -// const chkkpinameLink = await this.kpiLinkRepository.findOne({ -// where: { -// nameLinkKPI: requestBody.nameLinkKPI, -// }, -// }); -// if (chkkpinameLink) { -// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว"); -// } -// kpiLink.createdUserId = request.user.sub; -// kpiLink.createdFullName = request.user.name; -// kpiLink.lastUpdateUserId = request.user.sub; -// kpiLink.lastUpdateFullName = request.user.name; -// await this.kpiLinkRepository.save(kpiLink); -// return new HttpSuccess(kpiLink.id); -// } + /** + * API สร้างเชื่อมโยง + * @param requestBody + * @returns + */ + @Post() + async createKpiLink( + @Body() requestBody: createKpiLink, + @Request() request: { user: Record }, + ) { + const chkkpiGroup = await this.kpiGroupRepository.findOne({ + where: { + id: requestBody.kpiGroupId, + }, + }); + + if (!chkkpiGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + } + + const kpiLink = Object.assign(new KpiLink(), requestBody, { + createdUserId: request.user.sub, + createdFullName: request.user.name, + lastUpdateUserId: request.user.sub, + lastUpdateFullName: request.user.name, + kpiGroup: chkkpiGroup, + }); + await this.kpiLinkRepository.save(kpiLink); + + if (requestBody.positions != null) { + Promise.all( + requestBody.positions.map(async (positionName) => { + let position = new Position(); + position.name = positionName; + position.kpiLinkId = kpiLink.id; + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + await this.positionRepository.save(position); + }), + ); + } -// /** -// * API แก้ไขชื่อเชื่อมโยง -// * @param id ไอดีของเชื่อมโยง -// */ -// @Put("{id}") -// async updateKpiLink( -// @Path() id: string, -// @Body() requestBody: updateKpiLink, -// @Request() request: { user: Record }, -// ) { -// const kpiLink = await this.kpiLinkRepository.findOne({ -// where: { id: id }, -// }); -// if (!kpiLink) { -// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้"); -// } + const chkCapacity = await this.kpiCapacityRepository.find({ + where: { + id: In(requestBody.kpiCapacityIds), + }, + }); + if (!chkCapacity) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + } + kpiLink.kpiCapacitys = chkCapacity; + + await this.kpiLinkRepository.save(kpiLink); -// const chkkpinameLink = await this.kpiLinkRepository.findOne({ -// where: { -// nameLinkKPI: requestBody.nameLinkKPI, -// }, -// }); -// if (chkkpinameLink) { -// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อเชื่อมโยงนี้มีอยู่ในระบบแล้ว"); -// } -// this.kpiLinkRepository.merge(kpiLink, requestBody); -// kpiLink.lastUpdateUserId = request.user.sub; -// kpiLink.lastUpdateFullName = request.user.name; -// await this.kpiLinkRepository.save(kpiLink); -// return new HttpSuccess(id); -// } + return new HttpSuccess(kpiLink.id); + } + -// /** -// * API ชื่อเชื่อมโยง -// * @param id -// */ -// @Get("{id}") -// @Example({ -// nameLinkKPI: "string", //ชื่อเชื่อมโยง -// }) -// async KpiLinkById(@Path() id: string) { -// const kpiLink = await this.kpiLinkRepository.findOne({ -// where: { id: id }, -// select: ["nameLinkKPI"], -// }); -// if (!kpiLink) { -// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้"); -// } -// return new HttpSuccess(kpiLink); -// } + /** + * API แก้ไขเชื่อมโยง + * @param id ไอดีของเชื่อมโยง + */ + @Put("{id}") + async updateKpiLink( + @Path() id: string, + @Body() requestBody: createKpiLink, + @Request() request: { user: Record }, + ) { + const chkKpiLink = await this.kpiLinkRepository.findOne({ + where: { + id: id, + }, + relations: { + positions: true, + kpiCapacitys: true, + }, + }) + if (!chkKpiLink) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + } -// /** -// * API ลบเชื่อมโยง -// * @param id -// */ -// @Delete("{id}") -// async deleteKpiLink(@Path() id: string) { -// const kpiLink = await this.kpiLinkRepository.findOne({ -// where: { id: id }, -// }); -// if (!kpiLink) { -// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงนี้"); -// } + await this.positionRepository.remove(chkKpiLink.positions); + Object.assign(chkKpiLink, { + ...requestBody, + positions: [], + kpiCapacitys: [], + }); + chkKpiLink.kpiGroupId = requestBody.kpiGroupId, + chkKpiLink.lastUpdateUserId = request.user.sub; + chkKpiLink.lastUpdateFullName = request.user.name; + + if (requestBody.positions != null) { + Promise.all( + requestBody.positions.map(async (positionName) => { + let position = new Position(); + position.name = positionName; + position.kpiLinkId = chkKpiLink.id; + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + await this.positionRepository.save(position); + }), + ); + } -// await this.kpiLinkRepository.remove(kpiLink); -// return new HttpSuccess(); -// } + const chkCapacity = await this.kpiCapacityRepository.find({ + where: { + id: In(requestBody.kpiCapacityIds), + }, + }); + if (!chkCapacity) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + } + chkKpiLink.kpiCapacitys = chkCapacity; + + await this.kpiLinkRepository.save(chkKpiLink); -// /** -// * 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 = {}; + return new HttpSuccess(chkKpiLink.id); + } -// if (keyword !== undefined && keyword !== "") { -// whereClause = { -// where: [{ nameLinkKPI: Like(`%${keyword}%`) }], -// }; -// } + /** + * API เชื่อมโยง + * @param id + */ + @Get("{id}") + async KpiLinkById(@Path() id: string) { + const kpiLink = await this.kpiLinkRepository.findOne({ + where: { id: id }, + relations:["positions","kpiCapacitys"] + }); + if (!kpiLink) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + } + const formattedResponse = { + id: kpiLink.id, + positions: kpiLink.positions.map(position => ({ + id: position.id, + name: position.name + })), + kpiCapacitys: kpiLink.kpiCapacitys.map(capacity => ({ + id: capacity.id , + name: capacity.name, + type: capacity.type, + description: capacity.description + })) + }; + return new HttpSuccess(kpiLink); + } -// const [kpiLink, total] = await this.kpiLinkRepository.findAndCount({ -// ...whereClause, -// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), -// }); + // /** + // * API ลบเชื่อมโยง + // * @param id + // */ + // @Delete("{id}") + // async deleteKpiLink(@Path() id: string) { + // const kpiLink = await this.kpiLinkRepository.findOne({ + // where: { id: id }, + // }); + // if (!kpiLink) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + // } -// return new HttpSuccess({ data: kpiLink, total }); -// } -// } + // await this.kpiLinkRepository.remove(kpiLink); + // 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 }); + // } +} diff --git a/src/entities/kpiLink.ts b/src/entities/kpiLink.ts index 4741c86..d680b3c 100644 --- a/src/entities/kpiLink.ts +++ b/src/entities/kpiLink.ts @@ -23,3 +23,24 @@ export class KpiLink extends EntityBase { @OneToMany(() => Position, (position) => position.kpiLink) positions: Position[]; } +export class createKpiLink { + @Column() + kpiGroupId: string; + + @Column() + positions: string[]; + + @Column() + kpiCapacityIds: string[]; +} + +export class updateKpiLink { + @Column() + kpiGroupId: string; + + @Column() + positions: string[]; + + @Column() + kpiCapacityIds: string[]; +} From 6499cb3da09e114ff1d70d29f4ab6e650f445685 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 19 Apr 2024 16:41:54 +0700 Subject: [PATCH 4/5] =?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 }); + } } From 17f110d3459b44445a5fbd1f66657f7735b06ae5 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 19 Apr 2024 17:13:45 +0700 Subject: [PATCH 5/5] delete --- src/controllers/KpiLinkController.ts | 36 +++++++++++++++------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/controllers/KpiLinkController.ts b/src/controllers/KpiLinkController.ts index ca0bdaf..c2ceb7c 100644 --- a/src/controllers/KpiLinkController.ts +++ b/src/controllers/KpiLinkController.ts @@ -124,7 +124,6 @@ export class kpiLinkController extends Controller { await this.positionRepository.remove(chkKpiLink.positions); Object.assign(chkKpiLink, { ...requestBody, - positions: [], kpiCapacitys: [], }); chkKpiLink.kpiGroupId = requestBody.kpiGroupId, @@ -194,22 +193,27 @@ export class kpiLinkController extends Controller { return new HttpSuccess(formattedResponse); } - // /** - // * API ลบเชื่อมโยง - // * @param id - // */ - // @Delete("{id}") - // async deleteKpiLink(@Path() id: string) { - // const kpiLink = await this.kpiLinkRepository.findOne({ - // where: { id: id }, - // }); - // if (!kpiLink) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); - // } + /** + * API ลบเชื่อมโยง + * @param id + */ + @Delete("{id}") + async deleteKpiLink(@Path() id: string) { + const kpiLink = await this.kpiLinkRepository.findOne({ + where: { id: id }, + relations: ["kpiCapacitys"], + }); + if (!kpiLink) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); + } + kpiLink.kpiCapacitys = []; + await this.kpiLinkRepository.save(kpiLink); - // await this.kpiLinkRepository.remove(kpiLink); - // return new HttpSuccess(); - // } + await this.positionRepository.delete({ kpiLinkId: id }); + await this.kpiLinkRepository.delete({ id: id }); + + return new HttpSuccess(); + } /** * API list เชื่อมโยง