entity kpiUserEvaluation

This commit is contained in:
AdisakKanthawilang 2024-04-22 10:22:14 +07:00
parent 0d8d5c28aa
commit cda71e1099
3 changed files with 145 additions and 82 deletions

View file

@ -53,11 +53,11 @@ export class kpiLinkController extends Controller {
id: requestBody.kpiGroupId, id: requestBody.kpiGroupId,
}, },
}); });
if (!chkkpiGroup) { if (!chkkpiGroup) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
} }
const kpiLink = Object.assign(new KpiLink(), requestBody, { const kpiLink = Object.assign(new KpiLink(), requestBody, {
createdUserId: request.user.sub, createdUserId: request.user.sub,
createdFullName: request.user.name, createdFullName: request.user.name,
@ -66,9 +66,9 @@ export class kpiLinkController extends Controller {
kpiGroup: chkkpiGroup, kpiGroup: chkkpiGroup,
}); });
await this.kpiLinkRepository.save(kpiLink); await this.kpiLinkRepository.save(kpiLink);
if (requestBody.positions != null) { if (requestBody.positions != null) {
Promise.all( Promise.all(
requestBody.positions.map(async (positionName) => { requestBody.positions.map(async (positionName) => {
let position = new Position(); let position = new Position();
position.name = positionName; position.name = positionName;
@ -91,12 +91,11 @@ export class kpiLinkController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
} }
kpiLink.kpiCapacitys = chkCapacity; kpiLink.kpiCapacitys = chkCapacity;
await this.kpiLinkRepository.save(kpiLink); await this.kpiLinkRepository.save(kpiLink);
return new HttpSuccess(kpiLink.id); return new HttpSuccess(kpiLink.id);
} }
/** /**
* API * API
@ -116,7 +115,7 @@ export class kpiLinkController extends Controller {
positions: true, positions: true,
kpiCapacitys: true, kpiCapacitys: true,
}, },
}) });
if (!chkKpiLink) { if (!chkKpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
} }
@ -126,24 +125,24 @@ export class kpiLinkController extends Controller {
...requestBody, ...requestBody,
kpiCapacitys: [], kpiCapacitys: [],
}); });
chkKpiLink.kpiGroupId = requestBody.kpiGroupId, (chkKpiLink.kpiGroupId = requestBody.kpiGroupId),
chkKpiLink.lastUpdateUserId = request.user.sub; (chkKpiLink.lastUpdateUserId = request.user.sub);
chkKpiLink.lastUpdateFullName = request.user.name; chkKpiLink.lastUpdateFullName = request.user.name;
if (requestBody.positions != null) { if (requestBody.positions != null) {
Promise.all( Promise.all(
requestBody.positions.map(async (positionName) => { requestBody.positions.map(async (positionName) => {
let position = new Position(); let position = new Position();
position.name = positionName; position.name = positionName;
position.kpiLinkId = chkKpiLink.id; position.kpiLinkId = chkKpiLink.id;
position.createdUserId = request.user.sub; position.createdUserId = request.user.sub;
position.createdFullName = request.user.name; position.createdFullName = request.user.name;
position.lastUpdateUserId = request.user.sub; position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name; position.lastUpdateFullName = request.user.name;
await this.positionRepository.save(position); await this.positionRepository.save(position);
}), }),
); );
} }
const chkCapacity = await this.kpiCapacityRepository.find({ const chkCapacity = await this.kpiCapacityRepository.find({
where: { where: {
@ -154,7 +153,7 @@ export class kpiLinkController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
} }
chkKpiLink.kpiCapacitys = chkCapacity; chkKpiLink.kpiCapacitys = chkCapacity;
await this.kpiLinkRepository.save(chkKpiLink); await this.kpiLinkRepository.save(chkKpiLink);
return new HttpSuccess(chkKpiLink.id); return new HttpSuccess(chkKpiLink.id);
@ -168,28 +167,28 @@ export class kpiLinkController extends Controller {
async KpiLinkById(@Path() id: string) { async KpiLinkById(@Path() id: string) {
const kpiLink = await this.kpiLinkRepository.findOne({ const kpiLink = await this.kpiLinkRepository.findOne({
where: { id: id }, where: { id: id },
relations:["positions","kpiCapacitys","kpiGroup"], relations: ["positions", "kpiCapacitys", "kpiGroup"],
order:{ order: {
createdAt: "ASC" createdAt: "ASC",
} },
}); });
if (!kpiLink) { if (!kpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
} }
const formattedResponse = { const formattedResponse = {
id: kpiLink.id, id: kpiLink.id,
groupName: kpiLink.kpiGroup.nameGroupKPI, groupName: kpiLink.kpiGroup.nameGroupKPI,
groupId: kpiLink.kpiGroup.id, groupId: kpiLink.kpiGroup.id,
positions: kpiLink.positions.map(position => ({ positions: kpiLink.positions.map((position) => ({
id: position.id, id: position.id,
name: position.name name: position.name,
})), })),
capacitys: kpiLink.kpiCapacitys.map(capacity => ({ capacitys: kpiLink.kpiCapacitys.map((capacity) => ({
id: capacity.id , id: capacity.id,
name: capacity.name, name: capacity.name,
type: capacity.type, type: capacity.type,
description: capacity.description description: capacity.description,
})) })),
}; };
return new HttpSuccess(formattedResponse); return new HttpSuccess(formattedResponse);
} }
@ -227,60 +226,45 @@ export class kpiLinkController extends Controller {
@Query("pageSize") pageSize: number = 10, @Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string, @Query("keyword") keyword?: string,
) { ) {
const [kpiLink , total] = await AppDataSource.getRepository(KpiLink) const [kpiLink, total] = await AppDataSource.getRepository(KpiLink)
.createQueryBuilder("kpiLink") .createQueryBuilder("kpiLink")
.leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup") .leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup")
.leftJoinAndSelect("kpiLink.positions", "positions") .leftJoinAndSelect("kpiLink.positions", "positions")
.leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys") .leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys")
.andWhere( .where(
new Brackets((qb) => { new Brackets((qb) => {
qb.where( let conditions = [];
keyword != null && keyword != "" if (keyword) {
? "kpiGroup.nameGroupKPI LIKE :keyword" conditions.push("kpiGroup.nameGroupKPI LIKE :keyword");
: "1=1", conditions.push("positions.name LIKE :keyword");
{ conditions.push("kpiCapacitys.name LIKE :keyword");
keyword: `%${keyword}%`, }
}, if (conditions.length) {
) qb.where(conditions.join(" OR "), { keyword: `%${keyword}%` });
.orWhere( }
keyword != null && keyword != "" }),
? "positions.name LIKE :keyword" )
: "1=1", .skip((page - 1) * pageSize)
{ .take(pageSize)
keyword: `%${keyword}%`, .getManyAndCount();
},
)
.orWhere(
keyword != null && keyword != ""
? "kpiCapacitys.name LIKE :keyword"
: "1=1",
{
keyword: `%${keyword}%`,
},
)
}),
)
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
if (!kpiLink) { if (!kpiLink) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
} }
const formattedResponse = kpiLink.map((item) => ({ const formattedResponse = kpiLink.map((item) => ({
id: item.id, id: item.id,
groupName: item.kpiGroup.nameGroupKPI, groupName: item.kpiGroup.nameGroupKPI,
groupId: item.kpiGroup.id, groupId: item.kpiGroup.id,
positions: item.positions.map(position => ({ positions: item.positions.map((position) => ({
id: position.id, id: position.id,
name: position.name name: position.name,
})), })),
capacitys: item.kpiCapacitys.map(capacity => ({ capacitys: item.kpiCapacitys.map((capacity) => ({
id: capacity.id , id: capacity.id,
name: capacity.name, name: capacity.name,
type: capacity.type, type: capacity.type,
description: capacity.description description: capacity.description,
})) })),
})); }));
return new HttpSuccess({ data: formattedResponse, total }); return new HttpSuccess({ data: formattedResponse, total });
} }

View file

@ -2,6 +2,7 @@ import { Entity, Column, OneToMany } from "typeorm";
import { EntityBase } from "./base/Base"; import { EntityBase } from "./base/Base";
import { KpiPlan } from "./kpiPlan"; import { KpiPlan } from "./kpiPlan";
import { KpiRole } from "./kpiRole"; import { KpiRole } from "./kpiRole";
import { KpiUserEvaluation } from "./kpiUserEvaluation";
@Entity("kpiPeriod") @Entity("kpiPeriod")
export class KpiPeriod extends EntityBase { export class KpiPeriod extends EntityBase {
@ -45,6 +46,9 @@ export class KpiPeriod extends EntityBase {
@OneToMany(() => KpiPlan, (kpiPlan) => kpiPlan.kpiPeriod) @OneToMany(() => KpiPlan, (kpiPlan) => kpiPlan.kpiPeriod)
kpiPlans: KpiPlan[]; kpiPlans: KpiPlan[];
@OneToMany(() => KpiUserEvaluation, (kpiPlan) => kpiPlan.kpiPeriod)
kpiUserEvaluation: KpiUserEvaluation[];
} }
export class createKpiPeriod { export class createKpiPeriod {
@Column() @Column()

View file

@ -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;
}