From 3105b5f6568b27e40a1f0e6f617aefa3e7de1ee6 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Wed, 25 Feb 2026 21:30:55 +0700 Subject: [PATCH] fix: delete kpi --- .../KpiUserEvaluationController.ts | 81 ++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index d0a2ebb..7d3118a 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -46,6 +46,10 @@ import { KpiUserRole } from "../entities/kpiUserRole"; import { KpiUserSpecial } from "../entities/kpiUserSpecial"; import { KpiUserDevelopment } from "../entities/kpiUserDevelopment"; import { KpiUserEvaluationReasonPlan } from "../entities/kpiUserEvaluationReasonPlan"; +import { KpiUserEvaluationReasonCapacity } from "../entities/kpiUserEvaluationReasonCapacity"; +import { KpiUserEvaluationReasonRole } from "../entities/kpiUserEvaluationReasonRole"; +import { KpiUserEvaluationReasonSpecial } from "../entities/kpiUserEvaluationReasonSpecial"; +import { KpiUserEvaluationReasonDevelopment } from "../entities/kpiUserEvaluationReasonDevelopment"; @Route("api/v1/kpi/user/evaluation") @Tags("kpiUserEvaluation") @@ -1903,15 +1907,88 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } + + // Phase 1: Delete grandchild entities (reason entities) + // These must be deleted before their parent records + + // First, get all child entity IDs for this evaluation + const [kpiUserCapacities, kpiUserRoles, kpiUserSpecials, kpiUserDevelopments, kpiUserPlanneds] = + await Promise.all([ + queryRunner.manager.find(KpiUserCapacity, { + where: { kpiUserEvaluationId: id }, + select: ["id"], + }), + queryRunner.manager.find(KpiUserRole, { + where: { kpiUserEvaluationId: id }, + select: ["id"], + }), + queryRunner.manager.find(KpiUserSpecial, { + where: { kpiUserEvaluationId: id }, + select: ["id"], + }), + queryRunner.manager.find(KpiUserDevelopment, { + where: { kpiUserEvaluationId: id }, + select: ["id"], + }), + queryRunner.manager.find(KpiUserPlanned, { + where: { kpiUserEvaluationId: id }, + select: ["id"], + }), + ]); + + const capacityIds = kpiUserCapacities.map((c) => c.id); + const roleIds = kpiUserRoles.map((r) => r.id); + const specialIds = kpiUserSpecials.map((s) => s.id); + const developmentIds = kpiUserDevelopments.map((d) => d.id); + const plannedIds = kpiUserPlanneds.map((p) => p.id); + + // 1. KpiUserEvaluationReasonCapacity (via kpiUserCapacityId) + if (capacityIds.length > 0) { + await queryRunner.manager.delete(KpiUserEvaluationReasonCapacity, { + kpiUserCapacityId: In(capacityIds), + }); + } + + // 2. KpiUserEvaluationReasonRole (via kpiUserRoleId) + if (roleIds.length > 0) { + await queryRunner.manager.delete(KpiUserEvaluationReasonRole, { + kpiUserRoleId: In(roleIds), + }); + } + + // 3. KpiUserEvaluationReasonSpecial (via kpiUserSpecialId) + if (specialIds.length > 0) { + await queryRunner.manager.delete(KpiUserEvaluationReasonSpecial, { + kpiUserSpecialId: In(specialIds), + }); + } + + // 4. KpiUserEvaluationReasonDevelopment (via kpiUserDevelopmentId) + if (developmentIds.length > 0) { + await queryRunner.manager.delete(KpiUserEvaluationReasonDevelopment, { + kpiUserDevelopmentId: In(developmentIds), + }); + } + + // 5. KpiUserEvaluationReasonPlan (via kpiUserPlannedId) + if (plannedIds.length > 0) { + await queryRunner.manager.delete(KpiUserEvaluationReasonPlan, { + kpiUserPlannedId: In(plannedIds), + }); + } + + // Phase 2: Delete child entities await queryRunner.manager.delete(KpiUserCapacity, { kpiUserEvaluationId: id }); - await queryRunner.manager.delete(KpiUserEvaluationReasonPlan, { 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(KpiUserPlanned, { kpiUserEvaluationId: id }); await queryRunner.manager.delete(KpiUserRejectAgreement, { kpiUserEvaluationId: id }); await queryRunner.manager.delete(KpiUserRejectResult, { kpiUserEvaluationId: id }); + + // Phase 3: Delete parent entity await queryRunner.manager.delete(KpiUserEvaluation, { id }); + await queryRunner.commitTransaction(); return new HttpSuccess(); } catch (error) {