diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index 8269e9b..da78bb9 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -22,7 +22,8 @@ import HttpStatusCode from "../interfaces/http-status"; import { KpiRole, createKpiRole, updateKpiRole } from "../entities/kpiRole"; import CallAPI from "../interfaces/call-api"; import { KpiPeriod } from "../entities/kpiPeriod"; -import { Brackets } from "typeorm"; +import { Brackets, IsNull } from "typeorm"; +import { KpiRoleHistory } from "../entities/kpiRoleHistory"; @Route("api/v1/kpi/role") @Tags("kpiRole") @@ -34,6 +35,7 @@ import { Brackets } from "typeorm"; @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class kpiRoleController extends Controller { private kpiRoleRepository = AppDataSource.getRepository(KpiRole); + private kpiRoleHistoryRepository = AppDataSource.getRepository(KpiRoleHistory); private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); /** * สร้างตัวชี้วัดตามตำแหน่ง @@ -77,11 +79,79 @@ export class kpiRoleController extends Controller { kpiRole.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName; }) .catch((x) => {}); + + let maxIncludingRole: any; + if (requestBody.node == 0) { + maxIncludingRole = await this.kpiRoleRepository + .createQueryBuilder("kpiRole") + .select("MAX(kpiRole.including)", "maxIncluding") + .where("kpiRole.rootId = :rootId AND kpiRole.child1Id IS NULL", { + rootId: requestBody.nodeId, + }) + .andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 1) { + maxIncludingRole = await this.kpiRoleRepository + .createQueryBuilder("kpiRole") + .select("MAX(kpiRole.including)", "maxIncluding") + .where("kpiRole.child1Id = :child1Id AND kpiRole.child2Id IS NULL", { + child1Id: requestBody.nodeId, + }) + .andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 2) { + maxIncludingRole = await this.kpiRoleRepository + .createQueryBuilder("kpiRole") + .select("MAX(kpiRole.including)", "maxIncluding") + .where("kpiRole.child2Id = :child2Id AND kpiRole.child3Id IS NULL", { + child2Id: requestBody.nodeId, + }) + .andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 3) { + maxIncludingRole = await this.kpiRoleRepository + .createQueryBuilder("kpiRole") + .select("MAX(kpiRole.including)", "maxIncluding") + .where("kpiRole.child3Id = :child3Id AND kpiRole.child4Id IS NULL", { + child3Id: requestBody.nodeId, + }) + .andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } else if (requestBody.node == 4) { + maxIncludingRole = await this.kpiRoleRepository + .createQueryBuilder("kpiRole") + .select("MAX(kpiRole.including)", "maxIncluding") + .where("kpiRole.child4Id = :child4Id", { + child4Id: requestBody.nodeId, + }) + .andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .getRawOne(); + } + kpiRole.including = maxIncludingRole.maxIncluding + 1; kpiRole.createdUserId = request.user.sub; kpiRole.createdFullName = request.user.name; kpiRole.lastUpdateUserId = request.user.sub; kpiRole.lastUpdateFullName = request.user.name; await this.kpiRoleRepository.save(kpiRole); + + const history = new KpiRoleHistory(); + history.kpiRoleId = kpiRole.id; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + await this.kpiRoleHistoryRepository.save(history); + return new HttpSuccess(kpiRole.id); } @@ -141,6 +211,15 @@ export class kpiRoleController extends Controller { kpiRole.lastUpdateUserId = request.user.sub; kpiRole.lastUpdateFullName = request.user.name; await this.kpiRoleRepository.save(kpiRole); + + const history = new KpiRoleHistory(); + history.kpiRoleId = kpiRole.id; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + await this.kpiRoleHistoryRepository.save(history); + return new HttpSuccess(id); } @@ -227,6 +306,7 @@ export class kpiRoleController extends Controller { @Query("node") node?: number | null, @Query("keyword") keyword?: string, @Query("position") position?: string, + @Query("isAll") isAll: boolean = false, ) { const [kpiRole, total] = await AppDataSource.getRepository(KpiRole) .createQueryBuilder("kpiRole") @@ -294,7 +374,77 @@ export class kpiRoleController extends Controller { if (!kpiRole) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } + let type = 0; + if (kpiRole.child1Id != null) { + type = 1; + } else if (kpiRole.child2Id != null) { + type = 2; + } else if (kpiRole.child3Id != null) { + type = 3; + } else if (kpiRole.child4Id != null) { + type = 4; + } + await this.kpiRoleHistoryRepository.delete({ kpiRoleId: id }); await this.kpiRoleRepository.remove(kpiRole); + + if (kpiRole) { + let remainingKpiRoles: any; + if (type == 0) { + remainingKpiRoles = await this.kpiRoleRepository.find({ + where: { + rootId: kpiRole.rootId, + child1Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 1) { + remainingKpiRoles = await this.kpiRoleRepository.find({ + where: { + child1Id: kpiRole.child1Id, + child2Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 2) { + remainingKpiRoles = await this.kpiRoleRepository.find({ + where: { + child2Id: kpiRole.child2Id, + child3Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 3) { + remainingKpiRoles = await this.kpiRoleRepository.find({ + where: { + child3Id: kpiRole.child3Id, + child4Id: IsNull(), + }, + order: { + including: "ASC", + }, + }); + } else if (type == 4) { + remainingKpiRoles = await this.kpiRoleRepository.find({ + where: { + child4Id: kpiRole.child4Id, + }, + order: { + including: "ASC", + }, + }); + } + remainingKpiRoles.forEach((kpiRole: any, index: any) => { + kpiRole.including = index + 1; + }); + await this.kpiRoleRepository.save(remainingKpiRoles); + } + return new HttpSuccess(); } } diff --git a/src/entities/kpiPlan.ts b/src/entities/kpiPlan.ts index 3542b24..858b94e 100644 --- a/src/entities/kpiPlan.ts +++ b/src/entities/kpiPlan.ts @@ -321,6 +321,10 @@ export class createKpiPlan { // @Column() // including: number | null; @Column() + period: string | null; + @Column() + year: string | null; + @Column() includingName: string | null; @Column() target: string | null; @@ -362,6 +366,10 @@ export class updateKpiPlan { // @Column() // including: number | null; @Column() + period: string | null; + @Column() + year: string | null; + @Column() includingName: string | null; @Column() target: string | null; diff --git a/src/entities/kpiRole.ts b/src/entities/kpiRole.ts index 06597bb..91d0240 100644 --- a/src/entities/kpiRole.ts +++ b/src/entities/kpiRole.ts @@ -32,7 +32,7 @@ export class KpiRole extends EntityBase { comment: "ลำดับ/รหัสตัวชี้วัด", default: null, }) - including: string; + including: number; @Column({ nullable: true, @@ -252,7 +252,11 @@ export class createKpiRole { @Column() position: string | null; // @Column() - // including: string | null; + // including: number | null; + @Column() + period: string | null; + @Column() + year: string | null; @Column() includingName: string | null; @Column() @@ -291,7 +295,11 @@ export class updateKpiRole { @Column() position: string | null; // @Column() - // including: string | null; + // including: number | null; + @Column() + period: string | null; + @Column() + year: string | null; @Column() includingName: string | null; @Column()