diff --git a/src/controllers/KpiPlanController.ts b/src/controllers/KpiPlanController.ts index adb7ad3..2402e26 100644 --- a/src/controllers/KpiPlanController.ts +++ b/src/controllers/KpiPlanController.ts @@ -22,7 +22,8 @@ import HttpStatusCode from "../interfaces/http-status"; import { KpiPlan, createKpiPlan, updateKpiPlan } from "../entities/kpiPlan"; import CallAPI from "../interfaces/call-api"; import { KpiPeriod } from "../entities/kpiPeriod"; -import { Brackets } from "typeorm"; +import { Brackets, IsNull, Not } from "typeorm"; +import { KpiPlanHistory } from "../entities/kpiPlanHistory"; @Route("api/v1/kpi/plan") @Tags("kpiPlan") @@ -34,6 +35,7 @@ import { Brackets } from "typeorm"; @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiPlanController extends Controller { private kpiPlanRepository = AppDataSource.getRepository(KpiPlan); + private kpiPlanHistoryRepository = AppDataSource.getRepository(KpiPlanHistory); private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); /** * สร้างตัวชี้วัดตามแผนฯ @@ -96,11 +98,78 @@ export class kpiPlanController extends Controller { }) .catch((x) => {}); + let maxIncludingPlan: any; + if (requestBody.node == 0) { + maxIncludingPlan = await this.kpiPlanRepository + .createQueryBuilder("kpiPlan") + .select("MAX(kpiPlan.including)", "maxIncluding") + .where("kpiPlan.rootId = :rootId AND kpiPlan.child1Id IS NULL", { + rootId: requestBody.nodeId, + }) + .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 1) { + maxIncludingPlan = await this.kpiPlanRepository + .createQueryBuilder("kpiPlan") + .select("MAX(kpiPlan.including)", "maxIncluding") + .where("kpiPlan.child1Id = :child1Id AND kpiPlan.child2Id IS NULL", { + child1Id: requestBody.nodeId, + }) + .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 2) { + maxIncludingPlan = await this.kpiPlanRepository + .createQueryBuilder("kpiPlan") + .select("MAX(kpiPlan.including)", "maxIncluding") + .where("kpiPlan.child2Id = :child2Id AND kpiPlan.child3Id IS NULL", { + child2Id: requestBody.nodeId, + }) + .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 3) { + maxIncludingPlan = await this.kpiPlanRepository + .createQueryBuilder("kpiPlan") + .select("MAX(kpiPlan.including)", "maxIncluding") + .where("kpiPlan.child3Id = :child3Id AND kpiPlan.child4Id IS NULL", { + child3Id: requestBody.nodeId, + }) + .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 4) { + maxIncludingPlan = await this.kpiPlanRepository + .createQueryBuilder("kpiPlan") + .select("MAX(kpiPlan.including)", "maxIncluding") + .where("kpiPlan.child4Id = :child4Id", { + child4Id: requestBody.nodeId, + }) + .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } + kpiPlan.including = maxIncludingPlan.maxIncluding + 1; kpiPlan.createdUserId = request.user.sub; kpiPlan.createdFullName = request.user.name; kpiPlan.lastUpdateUserId = request.user.sub; kpiPlan.lastUpdateFullName = request.user.name; await this.kpiPlanRepository.save(kpiPlan); + + const history = new KpiPlanHistory(); + history.kpiPlanId = kpiPlan.id; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + await this.kpiPlanHistoryRepository.save(history); + return new HttpSuccess(kpiPlan.id); } @@ -178,6 +247,15 @@ export class kpiPlanController extends Controller { kpiPlan.lastUpdateUserId = request.user.sub; kpiPlan.lastUpdateFullName = request.user.name; await this.kpiPlanRepository.save(kpiPlan); + + const history = new KpiPlanHistory(); + history.kpiPlanId = kpiPlan.id; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + await this.kpiPlanHistoryRepository.save(history); + return new HttpSuccess(id); } @@ -357,7 +435,77 @@ export class kpiPlanController extends Controller { if (!kpiPlan) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } + let type = 0; + if (kpiPlan.child1Id != null) { + type = 1; + } else if (kpiPlan.child2Id != null) { + type = 2; + } else if (kpiPlan.child3Id != null) { + type = 3; + } else if (kpiPlan.child4Id != null) { + type = 4; + } + await this.kpiPlanHistoryRepository.delete({ kpiPlanId: id }); await this.kpiPlanRepository.remove(kpiPlan); + + if (kpiPlan) { + let remainingKpiPlans: any; + if (type == 0) { + remainingKpiPlans = await this.kpiPlanRepository.find({ + where: { + rootId: kpiPlan.rootId, + child1Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 1) { + remainingKpiPlans = await this.kpiPlanRepository.find({ + where: { + child1Id: kpiPlan.child1Id, + child2Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 2) { + remainingKpiPlans = await this.kpiPlanRepository.find({ + where: { + child2Id: kpiPlan.child2Id, + child3Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 3) { + remainingKpiPlans = await this.kpiPlanRepository.find({ + where: { + child3Id: kpiPlan.child3Id, + child4Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 4) { + remainingKpiPlans = await this.kpiPlanRepository.find({ + where: { + child4Id: kpiPlan.child4Id, + }, + order: { + including: "ASC", + }, + }); + } + remainingKpiPlans.forEach((kpiPlan: any, index: any) => { + kpiPlan.including = index + 1; + }); + await this.kpiPlanRepository.save(remainingKpiPlans); + } + return new HttpSuccess(); } } diff --git a/src/entities/kpiPlan.ts b/src/entities/kpiPlan.ts index 962c3cc..ea5917c 100644 --- a/src/entities/kpiPlan.ts +++ b/src/entities/kpiPlan.ts @@ -340,8 +340,8 @@ export class createKpiPlan { strategyId: string | null; @Column() kpiPeriodId: string | null; - @Column() - documentInfoEvidence: string | null; + // @Column() + // documentInfoEvidence: string | null; } export class updateKpiPlan { @@ -381,6 +381,6 @@ export class updateKpiPlan { strategyId: string | null; @Column() kpiPeriodId: string | null; - @Column() - documentInfoEvidence: string | null; + // @Column() + // documentInfoEvidence: string | null; } diff --git a/src/entities/kpiRole.ts b/src/entities/kpiRole.ts index 86fa42e..0fde4c2 100644 --- a/src/entities/kpiRole.ts +++ b/src/entities/kpiRole.ts @@ -270,8 +270,8 @@ export class createKpiRole { formula: string | null; @Column() kpiPeriodId: string | null; - @Column() - documentInfoEvidence: string | null; + // @Column() + // documentInfoEvidence: string | null; } export class updateKpiRole { @@ -309,6 +309,6 @@ export class updateKpiRole { formula: string | null; @Column() kpiPeriodId: string | null; - @Column() - documentInfoEvidence: string | null; + // @Column() + // documentInfoEvidence: string | null; }