2024-04-19 16:19:09 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
Example,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Response,
|
|
|
|
|
Query,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
2024-04-19 16:41:54 +07:00
|
|
|
import { Like, Not, In, Brackets } from "typeorm";
|
2024-04-19 16:19:09 +07:00
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import { KpiLink, createKpiLink, updateKpiLink } from "../entities/kpiLink";
|
|
|
|
|
import { KpiGroup } from "../entities/kpiGroup";
|
|
|
|
|
import { KpiCapacity } from "../entities/kpiCapacity";
|
|
|
|
|
import { Position } from "../entities/position";
|
|
|
|
|
@Route("api/v1/kpi/link")
|
|
|
|
|
@Tags("kpiLink")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
|
|
|
export class kpiLinkController extends Controller {
|
|
|
|
|
private kpiGroupRepository = AppDataSource.getRepository(KpiGroup);
|
|
|
|
|
private kpiLinkRepository = AppDataSource.getRepository(KpiLink);
|
|
|
|
|
private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity);
|
|
|
|
|
private positionRepository = AppDataSource.getRepository(Position);
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
/**
|
|
|
|
|
* API สร้างเชื่อมโยง
|
|
|
|
|
* @param requestBody
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
@Post()
|
|
|
|
|
async createKpiLink(
|
|
|
|
|
@Body() requestBody: createKpiLink,
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const chkkpiGroup = await this.kpiGroupRepository.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
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,
|
|
|
|
|
lastUpdateUserId: request.user.sub,
|
|
|
|
|
lastUpdateFullName: request.user.name,
|
|
|
|
|
kpiGroup: chkkpiGroup,
|
|
|
|
|
});
|
|
|
|
|
await this.kpiLinkRepository.save(kpiLink);
|
|
|
|
|
|
|
|
|
|
if (requestBody.positions != null) {
|
|
|
|
|
Promise.all(
|
|
|
|
|
requestBody.positions.map(async (positionName) => {
|
|
|
|
|
let position = new Position();
|
|
|
|
|
position.name = positionName;
|
|
|
|
|
position.kpiLinkId = kpiLink.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);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
const chkCapacity = await this.kpiCapacityRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
id: In(requestBody.kpiCapacityIds),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!chkCapacity) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
|
|
|
}
|
|
|
|
|
kpiLink.kpiCapacitys = chkCapacity;
|
|
|
|
|
|
|
|
|
|
await this.kpiLinkRepository.save(kpiLink);
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
return new HttpSuccess(kpiLink.id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
/**
|
|
|
|
|
* API แก้ไขเชื่อมโยง
|
|
|
|
|
* @param id ไอดีของเชื่อมโยง
|
|
|
|
|
*/
|
|
|
|
|
@Put("{id}")
|
|
|
|
|
async updateKpiLink(
|
|
|
|
|
@Path() id: string,
|
|
|
|
|
@Body() requestBody: createKpiLink,
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const chkKpiLink = await this.kpiLinkRepository.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
id: id,
|
|
|
|
|
},
|
|
|
|
|
relations: {
|
|
|
|
|
positions: true,
|
|
|
|
|
kpiCapacitys: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if (!chkKpiLink) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
|
|
|
}
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
await this.positionRepository.remove(chkKpiLink.positions);
|
|
|
|
|
Object.assign(chkKpiLink, {
|
|
|
|
|
...requestBody,
|
|
|
|
|
positions: [],
|
|
|
|
|
kpiCapacitys: [],
|
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
const chkCapacity = await this.kpiCapacityRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
id: In(requestBody.kpiCapacityIds),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!chkCapacity) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
|
|
|
}
|
|
|
|
|
chkKpiLink.kpiCapacitys = chkCapacity;
|
|
|
|
|
|
|
|
|
|
await this.kpiLinkRepository.save(chkKpiLink);
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
return new HttpSuccess(chkKpiLink.id);
|
|
|
|
|
}
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
/**
|
|
|
|
|
* API เชื่อมโยง
|
|
|
|
|
* @param id
|
|
|
|
|
*/
|
|
|
|
|
@Get("{id}")
|
|
|
|
|
async KpiLinkById(@Path() id: string) {
|
|
|
|
|
const kpiLink = await this.kpiLinkRepository.findOne({
|
|
|
|
|
where: { id: id },
|
2024-04-19 16:41:54 +07:00
|
|
|
relations:["positions","kpiCapacitys","kpiGroup"],
|
|
|
|
|
order:{
|
|
|
|
|
createdAt: "ASC"
|
|
|
|
|
}
|
2024-04-19 16:19:09 +07:00
|
|
|
});
|
|
|
|
|
if (!kpiLink) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
|
|
|
}
|
|
|
|
|
const formattedResponse = {
|
|
|
|
|
id: kpiLink.id,
|
2024-04-19 16:41:54 +07:00
|
|
|
groupName: kpiLink.kpiGroup.nameGroupKPI,
|
2024-04-19 16:19:09 +07:00
|
|
|
positions: kpiLink.positions.map(position => ({
|
|
|
|
|
id: position.id,
|
|
|
|
|
name: position.name
|
|
|
|
|
})),
|
2024-04-19 16:41:54 +07:00
|
|
|
capacitys: kpiLink.kpiCapacitys.map(capacity => ({
|
2024-04-19 16:19:09 +07:00
|
|
|
id: capacity.id ,
|
|
|
|
|
name: capacity.name,
|
|
|
|
|
type: capacity.type,
|
|
|
|
|
description: capacity.description
|
|
|
|
|
}))
|
|
|
|
|
};
|
2024-04-19 16:41:54 +07:00
|
|
|
return new HttpSuccess(formattedResponse);
|
2024-04-19 16:19:09 +07:00
|
|
|
}
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
// /**
|
|
|
|
|
// * API ลบเชื่อมโยง
|
|
|
|
|
// * @param id
|
|
|
|
|
// */
|
|
|
|
|
// @Delete("{id}")
|
|
|
|
|
// async deleteKpiLink(@Path() id: string) {
|
|
|
|
|
// const kpiLink = await this.kpiLinkRepository.findOne({
|
|
|
|
|
// where: { id: id },
|
|
|
|
|
// });
|
|
|
|
|
// if (!kpiLink) {
|
|
|
|
|
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
|
|
|
// }
|
2024-04-19 10:31:18 +07:00
|
|
|
|
2024-04-19 16:19:09 +07:00
|
|
|
// await this.kpiLinkRepository.remove(kpiLink);
|
|
|
|
|
// return new HttpSuccess();
|
|
|
|
|
// }
|
|
|
|
|
|
2024-04-19 16:41:54 +07:00
|
|
|
/**
|
|
|
|
|
* 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 });
|
|
|
|
|
}
|
2024-04-19 16:19:09 +07:00
|
|
|
}
|