diff --git a/src/controllers/KpiPlanController.ts b/src/controllers/KpiPlanController.ts index 2402e26..c48c5c7 100644 --- a/src/controllers/KpiPlanController.ts +++ b/src/controllers/KpiPlanController.ts @@ -48,10 +48,12 @@ export class kpiPlanController extends Controller { @Request() request: { user: Record }, ) { const kpiPlan = Object.assign(new KpiPlan(), requestBody); - if (requestBody.kpiPeriodId != null) { - const kpiPeriod = await this.kpiPeriodRepository.findOne({ - where: { id: requestBody.kpiPeriodId }, - }); + if (requestBody.year != null) { + const kpiPeriod = await this.kpiPeriodRepository + .createQueryBuilder("kpiPeriod") + .where("kpiPeriod.year = :year", { year: requestBody.year }) + .andWhere("kpiPeriod.durationKPI = :durationKPI", { durationKPI: requestBody.period }) + .getOne(); if (!kpiPeriod) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } @@ -106,8 +108,11 @@ export class kpiPlanController extends Controller { .where("kpiPlan.rootId = :rootId AND kpiPlan.child1Id IS NULL", { rootId: requestBody.nodeId, }) - .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { - kpiPeriodId: requestBody.kpiPeriodId, + .andWhere("kpiPlan.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiPlan.period = :period", { + period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 1) { @@ -117,8 +122,11 @@ export class kpiPlanController extends Controller { .where("kpiPlan.child1Id = :child1Id AND kpiPlan.child2Id IS NULL", { child1Id: requestBody.nodeId, }) - .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { - kpiPeriodId: requestBody.kpiPeriodId, + .andWhere("kpiPlan.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiPlan.period = :period", { + period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 2) { @@ -128,8 +136,11 @@ export class kpiPlanController extends Controller { .where("kpiPlan.child2Id = :child2Id AND kpiPlan.child3Id IS NULL", { child2Id: requestBody.nodeId, }) - .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { - kpiPeriodId: requestBody.kpiPeriodId, + .andWhere("kpiPlan.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiPlan.period = :period", { + period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 3) { @@ -139,8 +150,11 @@ export class kpiPlanController extends Controller { .where("kpiPlan.child3Id = :child3Id AND kpiPlan.child4Id IS NULL", { child3Id: requestBody.nodeId, }) - .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { - kpiPeriodId: requestBody.kpiPeriodId, + .andWhere("kpiPlan.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiPlan.period = :period", { + period: requestBody.period, }) .getRawOne(); } else if (requestBody.node == 4) { @@ -150,8 +164,11 @@ export class kpiPlanController extends Controller { .where("kpiPlan.child4Id = :child4Id", { child4Id: requestBody.nodeId, }) - .andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", { - kpiPeriodId: requestBody.kpiPeriodId, + .andWhere("kpiPlan.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiPlan.period = :period", { + period: requestBody.period, }) .getRawOne(); } @@ -192,10 +209,12 @@ export class kpiPlanController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้"); } - if (requestBody.kpiPeriodId != null) { - const kpiPeriod = await this.kpiPeriodRepository.findOne({ - where: { id: requestBody.kpiPeriodId }, - }); + if (requestBody.year != null) { + const kpiPeriod = await this.kpiPeriodRepository + .createQueryBuilder("kpiPeriod") + .where("kpiPeriod.year = :year", { year: requestBody.year }) + .andWhere("kpiPeriod.durationKPI = :durationKPI", { durationKPI: requestBody.period }) + .getOne(); if (!kpiPeriod) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } @@ -363,48 +382,91 @@ export class kpiPlanController extends Controller { * @param pageSize * @param keyword */ - @Get() + @Post("search") async listKpiPlan( - @Query("page") page: number = 1, - @Query("pageSize") pageSize: number = 10, - @Query("kpiPeriodId") kpiPeriodId?: string, - @Query("nodeId") nodeId?: string | null, - @Query("node") node?: number | null, - @Query("keyword") keyword?: string, + @Body() + requestBody: { + page: number; + pageSize: number; + year?: string | null; + period?: string | null; + nodeId?: string | null; + node?: number | null; + keyword?: string; + isAll?: boolean; + isNull?: boolean; + }, ) { + let condition = ""; + let parameters: any = {}; + if (requestBody.isAll === false) { + switch (requestBody.node) { + case 0: + condition = "kpiPlan.rootId LIKE :nodeId AND kpiPlan.child1 IS NULL"; + break; + case 1: + condition = "kpiPlan.child1 LIKE :nodeId AND kpiPlan.child2 IS NULL"; + break; + case 2: + condition = "kpiPlan.child2 LIKE :nodeId AND kpiPlan.child3 IS NULL"; + break; + case 3: + condition = "kpiPlan.child3 LIKE :nodeId AND kpiPlan.child4 IS NULL"; + break; + case 4: + condition = "kpiPlan.child4 LIKE :nodeId"; + break; + default: + break; + } + parameters.nodeId = `%${requestBody.nodeId}%`; + } else { + switch (requestBody.node) { + case 0: + condition = "kpiPlan.rootId LIKE :nodeId"; + break; + case 1: + condition = "kpiPlan.child1 LIKE :nodeId"; + break; + case 2: + condition = "kpiPlan.child2 LIKE :nodeId"; + break; + case 3: + condition = "kpiPlan.child3 LIKE :nodeId"; + break; + case 4: + condition = "kpiPlan.child4 LIKE :nodeId"; + break; + default: + break; + } + parameters.nodeId = `%${requestBody.nodeId}%`; + } + if (requestBody.year && requestBody.period && requestBody.isNull === true) { + condition += ` AND (kpiPlan.year LIKE :year OR kpiPlan.year IS NULL) AND (kpiPlan.period LIKE :period OR kpiPlan.period IS NULL)`; + parameters.year = `%${requestBody.year}%`; + parameters.period = `%${requestBody.period}%`; + } const [kpiPlan, total] = await AppDataSource.getRepository(KpiPlan) .createQueryBuilder("kpiPlan") .leftJoinAndSelect("kpiPlan.kpiPeriod", "kpiPeriod") + .andWhere(condition, parameters) .andWhere( - node != undefined && node != null - ? node == 4 - ? "kpiPlan.child4Id LIKE :nodeId" - : node == 3 - ? "kpiPlan.child3Id LIKE :nodeId" - : node == 2 - ? "kpiPlan.child2Id LIKE :nodeId" - : node == 1 - ? "kpiPlan.child1Id LIKE :nodeId" - : "kpiPlan.rootId LIKE :nodeId" + requestBody.isNull === false && requestBody.year && requestBody.period + ? "kpiPlan.year LIKE :year AND kpiPlan.period LIKE :period" : "1=1", { - nodeId: nodeId, - }, - ) - .andWhere( - kpiPeriodId != undefined && kpiPeriodId != null && kpiPeriodId != "" - ? "kpiPlan.kpiPeriodId LIKE :kpiPeriodId" - : "1=1", - { - kpiPeriodId: kpiPeriodId, + year: `%${requestBody.year}%`, + period: `%${requestBody.period}%`, }, ) .andWhere( new Brackets((qb) => { - qb.orWhere("kpiPlan.including LIKE :keyword", { keyword: `%${keyword}%` }).orWhere( - "kpiPlan.includingName LIKE :keyword", - { keyword: `%${keyword}%` }, - ); + qb.orWhere("kpiPlan.including LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }).orWhere("kpiPlan.includingName LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }); }), ) .select([ @@ -416,8 +478,8 @@ export class kpiPlanController extends Controller { "kpiPlan.createdAt", ]) .orderBy("kpiPlan.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) + .skip((requestBody.page - 1) * requestBody.pageSize) + .take(requestBody.pageSize) .getManyAndCount(); return new HttpSuccess({ data: kpiPlan, total }); @@ -447,7 +509,7 @@ export class kpiPlanController extends Controller { } await this.kpiPlanHistoryRepository.delete({ kpiPlanId: id }); await this.kpiPlanRepository.remove(kpiPlan); - + if (kpiPlan) { let remainingKpiPlans: any; if (type == 0) { diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index 5bb89ff..a96b16d 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -48,10 +48,12 @@ export class kpiRoleController extends Controller { @Request() request: { user: Record }, ) { const kpiRole = Object.assign(new KpiRole(), requestBody); - if (requestBody.kpiPeriodId != null) { - const kpiPeriod = await this.kpiPeriodRepository.findOne({ - where: { id: requestBody.kpiPeriodId }, - }); + if (requestBody.year != null) { + const kpiPeriod = await this.kpiPeriodRepository + .createQueryBuilder("kpiPeriod") + .where("kpiPeriod.year = :year", { year: requestBody.year }) + .andWhere("kpiPeriod.durationKPI = :durationKPI", { durationKPI: requestBody.period }) + .getOne(); if (!kpiPeriod) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } @@ -79,79 +81,94 @@ 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.position LIKE :position", { - position: `%${requestBody.position}%`, - }) - .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.position LIKE :position", { - position: `%${requestBody.position}%`, - }) - .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.position LIKE :position", { - position: `%${requestBody.position}%`, - }) - .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.position LIKE :position", { - position: `%${requestBody.position}%`, - }) - .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.position LIKE :position", { - position: `%${requestBody.position}%`, - }) - .andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", { - kpiPeriodId: requestBody.kpiPeriodId, - }) - .getRawOne(); - } + + 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.position LIKE :position", { + position: `%${requestBody.position}%`, + }) + .andWhere("kpiRole.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiRole.period = :period", { + period: requestBody.period, + }) + .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.position LIKE :position", { + position: `%${requestBody.position}%`, + }) + .andWhere("kpiRole.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiRole.period = :period", { + period: requestBody.period, + }) + .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.position LIKE :position", { + position: `%${requestBody.position}%`, + }) + .andWhere("kpiRole.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiRole.period = :period", { + period: requestBody.period, + }) + .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.position LIKE :position", { + position: `%${requestBody.position}%`, + }) + .andWhere("kpiRole.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiRole.period = :period", { + period: requestBody.period, + }) + .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.position LIKE :position", { + position: `%${requestBody.position}%`, + }) + .andWhere("kpiRole.year = :year", { + year: requestBody.year, + }) + .andWhere("kpiRole.period = :period", { + period: requestBody.period, + }) + .getRawOne(); + } kpiRole.including = maxIncludingRole.maxIncluding + 1; kpiRole.createdUserId = request.user.sub; kpiRole.createdFullName = request.user.name; @@ -189,10 +206,12 @@ export class kpiRoleController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } - if (requestBody.kpiPeriodId != null) { - const kpiPeriod = await this.kpiPeriodRepository.findOne({ - where: { id: requestBody.kpiPeriodId }, - }); + if (requestBody.year != null) { + const kpiPeriod = await this.kpiPeriodRepository + .createQueryBuilder("kpiPeriod") + .where("kpiPeriod.year = :year", { year: requestBody.year }) + .andWhere("kpiPeriod.durationKPI = :durationKPI", { durationKPI: requestBody.period }) + .getOne(); if (!kpiPeriod) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้"); } @@ -312,51 +331,108 @@ export class kpiRoleController extends Controller { * @param pageSize * @param keyword */ - @Get() + @Post("search") async listKpiRole( - @Query("page") page: number = 1, - @Query("pageSize") pageSize: number = 10, - @Query("kpiPeriodId") kpiPeriodId?: string, - @Query("nodeId") nodeId?: string | null, - @Query("node") node?: number | null, - @Query("keyword") keyword?: string, - @Query("position") position?: string, + @Body() + requestBody: { + page: number; + pageSize: number; + year?: string | null; + period?: string | null; + nodeId?: string | null; + node?: number | null; + position?: string | null; + keyword?: string; + isAll?: boolean; + isNull?: boolean; + }, ) { + let condition = ""; + let parameters: any = {}; + if (requestBody.isAll === false) { + switch (requestBody.node) { + case 0: + condition = "kpiRole.rootId LIKE :nodeId AND kpiRole.child1 IS NULL"; + break; + case 1: + condition = "kpiRole.child1 LIKE :nodeId AND kpiRole.child2 IS NULL"; + break; + case 2: + condition = "kpiRole.child2 LIKE :nodeId AND kpiRole.child3 IS NULL"; + break; + case 3: + condition = "kpiRole.child3 LIKE :nodeId AND kpiRole.child4 IS NULL"; + break; + case 4: + condition = "kpiRole.child4 LIKE :nodeId"; + break; + default: + break; + } + parameters.nodeId = `%${requestBody.nodeId}%`; + } else { + switch (requestBody.node) { + case 0: + condition = "kpiRole.rootId LIKE :nodeId"; + break; + case 1: + condition = "kpiRole.child1 LIKE :nodeId"; + break; + case 2: + condition = "kpiRole.child2 LIKE :nodeId"; + break; + case 3: + condition = "kpiRole.child3 LIKE :nodeId"; + break; + case 4: + condition = "kpiRole.child4 LIKE :nodeId"; + break; + default: + break; + } + parameters.nodeId = `%${requestBody.nodeId}%`; + } + if (requestBody.year && requestBody.period && requestBody.isNull === true) { + condition += ` AND (kpiRole.year LIKE :year OR kpiRole.year IS NULL) AND (kpiRole.period LIKE :period OR kpiRole.period IS NULL)`; + parameters.year = `%${requestBody.year}%`; + parameters.period = `%${requestBody.period}%`; + } const [kpiRole, total] = await AppDataSource.getRepository(KpiRole) .createQueryBuilder("kpiRole") .leftJoinAndSelect("kpiRole.kpiPeriod", "kpiPeriod") .andWhere( - node != undefined && node != null - ? node == 4 + requestBody.node != undefined && requestBody.node != null + ? requestBody.node == 4 ? "kpiRole.child4Id LIKE :nodeId" - : node == 3 + : requestBody.node == 3 ? "kpiRole.child3Id LIKE :nodeId" - : node == 2 + : requestBody.node == 2 ? "kpiRole.child2Id LIKE :nodeId" - : node == 1 + : requestBody.node == 1 ? "kpiRole.child1Id LIKE :nodeId" : "kpiRole.rootId LIKE :nodeId" : "1=1", { - nodeId: nodeId, + nodeId: requestBody.nodeId, }, ) .andWhere( - kpiPeriodId != undefined && kpiPeriodId != null && kpiPeriodId != "" - ? "kpiRole.kpiPeriod LIKE :kpiPeriodId" + requestBody.isNull === false && requestBody.year && requestBody.period + ? "kpiRole.year LIKE :year AND kpiRole.period LIKE :period" : "1=1", { - kpiPeriodId: kpiPeriodId, + year: `%${requestBody.year}%`, + period: `%${requestBody.period}%`, }, ) - .andWhere(position != undefined ? "kpiRole.position LIKE :position" : "1=1", { - position: `%${position}%`, + .andWhere(requestBody.position != undefined ? "kpiRole.position LIKE :position" : "1=1", { + position: `%${requestBody.position}%`, }) .andWhere( new Brackets((qb) => { - qb.orWhere("kpiRole.including LIKE :keyword", { keyword: `%${keyword}%` }).orWhere( + qb.orWhere("kpiRole.including LIKE :keyword", { keyword: `%${requestBody.keyword}%` }).orWhere( "kpiRole.includingName LIKE :keyword", - { keyword: `%${keyword}%` }, + { keyword: `%${requestBody.keyword}%` }, ); }), ) @@ -369,8 +445,8 @@ export class kpiRoleController extends Controller { "kpiRole.createdAt", ]) .orderBy("kpiRole.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) + .skip((requestBody.page - 1) * requestBody.pageSize) + .take(requestBody.pageSize) .getManyAndCount(); return new HttpSuccess({ data: kpiRole, total }); @@ -463,7 +539,6 @@ export class kpiRoleController extends Controller { }); await this.kpiRoleRepository.save(remainingKpiRoles); } - return new HttpSuccess(); } } diff --git a/src/entities/kpiPlan.ts b/src/entities/kpiPlan.ts index c98e83d..e29ec4c 100644 --- a/src/entities/kpiPlan.ts +++ b/src/entities/kpiPlan.ts @@ -356,8 +356,8 @@ export class createKpiPlan { strategy: number; @Column() strategyId: string | null; - @Column() - kpiPeriodId: string | null; + // @Column() + // kpiPeriodId: string | null; @Column() documentInfoEvidence: string | null; } @@ -401,8 +401,8 @@ export class updateKpiPlan { strategy: number; @Column() strategyId: string | null; - @Column() - kpiPeriodId: string | null; + // @Column() + // kpiPeriodId: string | null; @Column() documentInfoEvidence: string | null; }