From 1ccde3f38d01269b43808f80870bfb129fc64f09 Mon Sep 17 00:00:00 2001 From: harid Date: Wed, 25 Feb 2026 16:42:09 +0700 Subject: [PATCH] Fix Delete Error FK #2332 --- .../KpiUserEvaluationController.ts | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index 3364f23..a177e6f 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -40,6 +40,11 @@ import permission from "../interfaces/permission"; import { resolveNodeId, resolveNodeLevel, setLogDataDiff } from "../interfaces/utils"; import { KpiUserRejectAgreement } from "../entities/kpiUserRejectAgreement"; import { KpiUserRejectResult } from "../entities/kpiUserRejectResult"; +import { KpiUserCapacity } from "../entities/kpiUserCapacity"; +import { KpiUserPlanned } from "../entities/kpiUserPlanned" +import { KpiUserRole } from "../entities/kpiUserRole"; +import { KpiUserSpecial } from "../entities/kpiUserSpecial"; +import { KpiUserDevelopment } from "../entities/kpiUserDevelopment"; @Route("api/v1/kpi/user/evaluation") @Tags("kpiUserEvaluation") @@ -1875,18 +1880,39 @@ export class KpiUserEvaluationController extends Controller { * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) */ @Delete("{id}") - async deleteKpiUserEvaluation(@Path() id: string, @Request() request: RequestWithUser) { - const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ - where: { id: id }, - }); - if (!kpiUserEvaluation) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", - ); + async deleteKpiUserEvaluation(@Path() id: string, @Request() request: RequestWithUser + ) { + const queryRunner = AppDataSource.createQueryRunner(); + await queryRunner.connect(); + await queryRunner.startTransaction(); + + try { + const kpiUserEvaluation = await queryRunner.manager.findOne(KpiUserEvaluation, { + where: { id }, + }); + + if (!kpiUserEvaluation) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้" + ); + } + await queryRunner.manager.delete(KpiUserCapacity, { kpiUserEvaluationId: id }); + await queryRunner.manager.delete(KpiUserPlanned, { kpiUserEvaluationId: id }); + await queryRunner.manager.delete(KpiUserRole, { kpiUserEvaluationId: id }); + await queryRunner.manager.delete(KpiUserSpecial, { kpiUserEvaluationId: id }); + await queryRunner.manager.delete(KpiUserDevelopment, { kpiUserEvaluationId: id }); + await queryRunner.manager.delete(KpiUserRejectAgreement, { kpiUserEvaluationId: id }); + await queryRunner.manager.delete(KpiUserRejectResult, { kpiUserEvaluationId: id }); + await queryRunner.manager.delete(KpiUserEvaluation, { id }); + await queryRunner.commitTransaction(); + return new HttpSuccess(); + } catch (error) { + await queryRunner.rollbackTransaction(); + throw error; + } finally { + await queryRunner.release(); } - await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation, { data: request }); - return new HttpSuccess(); } /**