From cda71e10995c2f1770218c385b5ab83cf84c1ec6 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Mon, 22 Apr 2024 10:22:14 +0700 Subject: [PATCH] entity kpiUserEvaluation --- src/controllers/KpiLinkController.ts | 148 ++++++++++++--------------- src/entities/kpiPeriod.ts | 4 + src/entities/kpiUserEvaluation.ts | 75 ++++++++++++++ 3 files changed, 145 insertions(+), 82 deletions(-) create mode 100644 src/entities/kpiUserEvaluation.ts diff --git a/src/controllers/KpiLinkController.ts b/src/controllers/KpiLinkController.ts index 64a2918..f943b1e 100644 --- a/src/controllers/KpiLinkController.ts +++ b/src/controllers/KpiLinkController.ts @@ -53,11 +53,11 @@ export class kpiLinkController extends Controller { id: requestBody.kpiGroupId, }, }); - + if (!chkkpiGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); } - + const kpiLink = Object.assign(new KpiLink(), requestBody, { createdUserId: request.user.sub, createdFullName: request.user.name, @@ -66,9 +66,9 @@ export class kpiLinkController extends Controller { kpiGroup: chkkpiGroup, }); await this.kpiLinkRepository.save(kpiLink); - + if (requestBody.positions != null) { - Promise.all( + Promise.all( requestBody.positions.map(async (positionName) => { let position = new Position(); position.name = positionName; @@ -91,12 +91,11 @@ export class kpiLinkController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); } kpiLink.kpiCapacitys = chkCapacity; - + await this.kpiLinkRepository.save(kpiLink); return new HttpSuccess(kpiLink.id); } - /** * API แก้ไขเชื่อมโยง @@ -116,7 +115,7 @@ export class kpiLinkController extends Controller { positions: true, kpiCapacitys: true, }, - }) + }); if (!chkKpiLink) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); } @@ -126,24 +125,24 @@ export class kpiLinkController extends Controller { ...requestBody, kpiCapacitys: [], }); - chkKpiLink.kpiGroupId = requestBody.kpiGroupId, - chkKpiLink.lastUpdateUserId = request.user.sub; + (chkKpiLink.kpiGroupId = requestBody.kpiGroupId), + (chkKpiLink.lastUpdateUserId = request.user.sub); chkKpiLink.lastUpdateFullName = request.user.name; - + if (requestBody.positions != null) { Promise.all( - requestBody.positions.map(async (positionName) => { - let position = new Position(); - position.name = positionName; - position.kpiLinkId = chkKpiLink.id; - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - await this.positionRepository.save(position); - }), - ); - } + requestBody.positions.map(async (positionName) => { + let position = new Position(); + position.name = positionName; + position.kpiLinkId = chkKpiLink.id; + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + await this.positionRepository.save(position); + }), + ); + } const chkCapacity = await this.kpiCapacityRepository.find({ where: { @@ -154,7 +153,7 @@ export class kpiLinkController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); } chkKpiLink.kpiCapacitys = chkCapacity; - + await this.kpiLinkRepository.save(chkKpiLink); return new HttpSuccess(chkKpiLink.id); @@ -168,28 +167,28 @@ export class kpiLinkController extends Controller { async KpiLinkById(@Path() id: string) { const kpiLink = await this.kpiLinkRepository.findOne({ where: { id: id }, - relations:["positions","kpiCapacitys","kpiGroup"], - order:{ - createdAt: "ASC" - } + relations: ["positions", "kpiCapacitys", "kpiGroup"], + order: { + createdAt: "ASC", + }, }); if (!kpiLink) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); } const formattedResponse = { - id: kpiLink.id, + id: kpiLink.id, groupName: kpiLink.kpiGroup.nameGroupKPI, groupId: kpiLink.kpiGroup.id, - positions: kpiLink.positions.map(position => ({ - id: position.id, - name: position.name - })), - capacitys: kpiLink.kpiCapacitys.map(capacity => ({ - id: capacity.id , + positions: kpiLink.positions.map((position) => ({ + id: position.id, + name: position.name, + })), + capacitys: kpiLink.kpiCapacitys.map((capacity) => ({ + id: capacity.id, name: capacity.name, type: capacity.type, - description: capacity.description - })) + description: capacity.description, + })), }; return new HttpSuccess(formattedResponse); } @@ -227,60 +226,45 @@ export class kpiLinkController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { - const [kpiLink , total] = await AppDataSource.getRepository(KpiLink) - .createQueryBuilder("kpiLink") - .leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup") - .leftJoinAndSelect("kpiLink.positions", "positions") - .leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys") - .andWhere( - new Brackets((qb) => { - qb.where( - keyword != null && keyword != "" - ? "kpiGroup.nameGroupKPI LIKE :keyword" - : "1=1", - { - keyword: `%${keyword}%`, - }, - ) - .orWhere( - keyword != null && keyword != "" - ? "positions.name LIKE :keyword" - : "1=1", - { - keyword: `%${keyword}%`, - }, - ) - .orWhere( - keyword != null && keyword != "" - ? "kpiCapacitys.name LIKE :keyword" - : "1=1", - { - keyword: `%${keyword}%`, - }, - ) - }), - ) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); - + const [kpiLink, total] = await AppDataSource.getRepository(KpiLink) + .createQueryBuilder("kpiLink") + .leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup") + .leftJoinAndSelect("kpiLink.positions", "positions") + .leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys") + .where( + new Brackets((qb) => { + let conditions = []; + if (keyword) { + conditions.push("kpiGroup.nameGroupKPI LIKE :keyword"); + conditions.push("positions.name LIKE :keyword"); + conditions.push("kpiCapacitys.name LIKE :keyword"); + } + if (conditions.length) { + qb.where(conditions.join(" OR "), { keyword: `%${keyword}%` }); + } + }), + ) + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + if (!kpiLink) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); } const formattedResponse = kpiLink.map((item) => ({ - id: item.id, + id: item.id, groupName: item.kpiGroup.nameGroupKPI, groupId: item.kpiGroup.id, - positions: item.positions.map(position => ({ - id: position.id, - name: position.name - })), - capacitys: item.kpiCapacitys.map(capacity => ({ - id: capacity.id , + positions: item.positions.map((position) => ({ + id: position.id, + name: position.name, + })), + capacitys: item.kpiCapacitys.map((capacity) => ({ + id: capacity.id, name: capacity.name, type: capacity.type, - description: capacity.description - })) + description: capacity.description, + })), })); return new HttpSuccess({ data: formattedResponse, total }); } diff --git a/src/entities/kpiPeriod.ts b/src/entities/kpiPeriod.ts index 2e02ada..24e1b8b 100644 --- a/src/entities/kpiPeriod.ts +++ b/src/entities/kpiPeriod.ts @@ -2,6 +2,7 @@ import { Entity, Column, OneToMany } from "typeorm"; import { EntityBase } from "./base/Base"; import { KpiPlan } from "./kpiPlan"; import { KpiRole } from "./kpiRole"; +import { KpiUserEvaluation } from "./kpiUserEvaluation"; @Entity("kpiPeriod") export class KpiPeriod extends EntityBase { @@ -45,6 +46,9 @@ export class KpiPeriod extends EntityBase { @OneToMany(() => KpiPlan, (kpiPlan) => kpiPlan.kpiPeriod) kpiPlans: KpiPlan[]; + + @OneToMany(() => KpiUserEvaluation, (kpiPlan) => kpiPlan.kpiPeriod) + kpiUserEvaluation: KpiUserEvaluation[]; } export class createKpiPeriod { @Column() diff --git a/src/entities/kpiUserEvaluation.ts b/src/entities/kpiUserEvaluation.ts new file mode 100644 index 0000000..0ddfe0f --- /dev/null +++ b/src/entities/kpiUserEvaluation.ts @@ -0,0 +1,75 @@ +import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { KpiPeriod } from "./kpiPeriod"; +@Entity("kpiUserEvaluation") +export class KpiUserEvaluation extends EntityBase { + @Column({ + nullable: true, + comment: "คำนำหน้า", + length: 255, + default: null, + }) + prefix: string; + + @Column({ + nullable: true, + comment: "ชื่อ", + length: 255, + default: null, + }) + firstName: string; + + @Column({ + nullable: true, + comment: "สกุล", + length: 255, + default: null, + }) + lastName: string; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง kpiPeriodId", + default: null, + }) + kpiPeriodId: string; + + @Column({ + nullable: true, + length: 40, + comment: "ไอดีโปรไฟล์", + default: null, + }) + profileId: string; + + @ManyToOne(() => KpiPeriod, (kpiPeriod) => kpiPeriod.kpiUserEvaluation) + @JoinColumn({ name: "kpiPeriodId" }) + kpiPeriod: KpiPeriod; +} + +export class createKpiUserEvaluation { + @Column() + prefix: string; + @Column() + firstName: string; + @Column() + lastName: string; + @Column() + kpiPeriodId: string; + @Column() + profileId: string; +} + +export class updateKpiUserEvaluation { + @Column() + prefix: string; + @Column() + firstName: string; + @Column() + lastName: string; + @Column() + kpiPeriodId: string; + @Column() + profileId: string; +}