api สมรรถนะ ขาดลบ
This commit is contained in:
parent
d53a2cae49
commit
6499cb3da0
1 changed files with 75 additions and 30 deletions
|
|
@ -18,7 +18,7 @@ import {
|
|||
import { AppDataSource } from "../database/data-source";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { Like, Not, In } from "typeorm";
|
||||
import { Like, Not, In, Brackets } from "typeorm";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink";
|
||||
import { KpiGroup } from "../entities/kpiGroup";
|
||||
|
|
@ -169,25 +169,29 @@ export class kpiLinkController extends Controller {
|
|||
async KpiLinkById(@Path() id: string) {
|
||||
const kpiLink = await this.kpiLinkRepository.findOne({
|
||||
where: { id: id },
|
||||
relations:["positions","kpiCapacitys"]
|
||||
relations:["positions","kpiCapacitys","kpiGroup"],
|
||||
order:{
|
||||
createdAt: "ASC"
|
||||
}
|
||||
});
|
||||
if (!kpiLink) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
||||
}
|
||||
const formattedResponse = {
|
||||
id: kpiLink.id,
|
||||
groupName: kpiLink.kpiGroup.nameGroupKPI,
|
||||
positions: kpiLink.positions.map(position => ({
|
||||
id: position.id,
|
||||
name: position.name
|
||||
})),
|
||||
kpiCapacitys: kpiLink.kpiCapacitys.map(capacity => ({
|
||||
capacitys: kpiLink.kpiCapacitys.map(capacity => ({
|
||||
id: capacity.id ,
|
||||
name: capacity.name,
|
||||
type: capacity.type,
|
||||
description: capacity.description
|
||||
}))
|
||||
};
|
||||
return new HttpSuccess(kpiLink);
|
||||
return new HttpSuccess(formattedResponse);
|
||||
}
|
||||
|
||||
// /**
|
||||
|
|
@ -207,30 +211,71 @@ export class kpiLinkController extends Controller {
|
|||
// return new HttpSuccess();
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * API list เชื่อมโยง
|
||||
// * @param page
|
||||
// * @param pageSize
|
||||
// */
|
||||
// @Get()
|
||||
// async listKpiLink(
|
||||
// @Query("page") page: number = 1,
|
||||
// @Query("pageSize") pageSize: number = 10,
|
||||
// @Query("keyword") keyword?: string,
|
||||
// ) {
|
||||
// let whereClause: any = {};
|
||||
|
||||
// if (keyword !== undefined && keyword !== "") {
|
||||
// whereClause = {
|
||||
// where: [{ nameLinkKPI: Like(`%${keyword}%`) }],
|
||||
// };
|
||||
// }
|
||||
|
||||
// const [kpiLink, total] = await this.kpiLinkRepository.findAndCount({
|
||||
// ...whereClause,
|
||||
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
||||
// });
|
||||
|
||||
// return new HttpSuccess({ data: kpiLink, total });
|
||||
// }
|
||||
/**
|
||||
* API list เชื่อมโยง
|
||||
* @param page
|
||||
* @param pageSize
|
||||
*/
|
||||
@Get()
|
||||
async listKpiLink(
|
||||
@Query("page") page: number = 1,
|
||||
@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();
|
||||
|
||||
if (!kpiLink) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
||||
}
|
||||
const formattedResponse = kpiLink.map((item) => ({
|
||||
id: item.id,
|
||||
groupName: item.kpiGroup.nameGroupKPI,
|
||||
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
|
||||
}))
|
||||
}));
|
||||
return new HttpSuccess({ data: formattedResponse, total });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue