404 lines
13 KiB
TypeScript
404 lines
13 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
SuccessResponse,
|
|
Response,
|
|
Query,
|
|
} from "tsoa";
|
|
import { AppDataSource } from "../database/data-source";
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
import HttpError from "../interfaces/http-error";
|
|
import { In, Brackets } from "typeorm";
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
import { KpiLink, createKpiLink } from "../entities/kpiLink";
|
|
import { KpiGroup } from "../entities/kpiGroup";
|
|
import { KpiCapacity } from "../entities/kpiCapacity";
|
|
import { Position } from "../entities/position";
|
|
import permission from "../interfaces/permission";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import { addLogSequence, setLogDataDiff } from "../interfaces/utils";
|
|
|
|
@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);
|
|
|
|
/**
|
|
* API สร้างเชื่อมโยง
|
|
* @param requestBody
|
|
* @returns
|
|
*/
|
|
@Post()
|
|
async createKpiLink(@Body() requestBody: createKpiLink, @Request() request: RequestWithUser) {
|
|
await new permission().PermissionCreate(request, "SYS_EVA_COMPETENCY");
|
|
const chkkpiGroup = await this.kpiGroupRepository.findOne({
|
|
where: {
|
|
id: requestBody.kpiGroupId,
|
|
},
|
|
});
|
|
|
|
if (!chkkpiGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
}
|
|
const before = null;
|
|
|
|
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, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiLink });
|
|
|
|
if (requestBody.positions != null) {
|
|
await 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, { data: request });
|
|
setLogDataDiff(request, { before, after: position });
|
|
}),
|
|
);
|
|
}
|
|
|
|
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, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiLink });
|
|
|
|
return new HttpSuccess(kpiLink.id);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขเชื่อมโยง
|
|
* @param id ไอดีของเชื่อมโยง
|
|
*/
|
|
@Put("{id}")
|
|
async updateKpiLink(
|
|
@Path() id: string,
|
|
@Body() requestBody: createKpiLink,
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
await new permission().PermissionUpdate(request, "SYS_EVA_COMPETENCY");
|
|
const chkKpiLink = await this.kpiLinkRepository.findOne({
|
|
where: {
|
|
id: id,
|
|
},
|
|
relations: {
|
|
positions: true,
|
|
kpiCapacitys: true,
|
|
},
|
|
});
|
|
if (!chkKpiLink) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
}
|
|
const before = structuredClone(chkKpiLink);
|
|
|
|
await this.positionRepository.remove(chkKpiLink.positions, { data: request });
|
|
Object.assign(chkKpiLink, {
|
|
...requestBody,
|
|
kpiCapacitys: [],
|
|
});
|
|
(chkKpiLink.kpiGroupId = requestBody.kpiGroupId),
|
|
(chkKpiLink.lastUpdateUserId = request.user.sub);
|
|
chkKpiLink.lastUpdateFullName = request.user.name;
|
|
|
|
if (requestBody.positions != null) {
|
|
await 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, { data: request });
|
|
setLogDataDiff(request, { before: null, after: position });
|
|
}),
|
|
);
|
|
}
|
|
|
|
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, { data: request });
|
|
setLogDataDiff(request, { before, after: chkKpiLink });
|
|
|
|
return new HttpSuccess(chkKpiLink.id);
|
|
}
|
|
|
|
/**
|
|
* API เชื่อมโยง
|
|
* @param id
|
|
*/
|
|
@Get("edit/{id}")
|
|
async KpiLinkByIdEdit(@Request() request: RequestWithUser, @Path() id: string) {
|
|
let _data = await new permission().PermissionGet(request, "SYS_EVA_COMPETENCY");
|
|
const kpiLink = await this.kpiLinkRepository.findOne({
|
|
where: { id: id },
|
|
relations: ["positions", "kpiCapacitys", "kpiGroup"],
|
|
order: {
|
|
createdAt: "ASC",
|
|
},
|
|
});
|
|
if (!kpiLink) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
}
|
|
const formattedResponse = {
|
|
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,
|
|
name: capacity.name,
|
|
type: capacity.type,
|
|
description: capacity.description,
|
|
})),
|
|
};
|
|
return new HttpSuccess(formattedResponse);
|
|
}
|
|
/**
|
|
* API list เชื่อมโยง
|
|
* @param page
|
|
* @param pageSize
|
|
*/
|
|
@Get("edit")
|
|
async listKpiLinkEdit(
|
|
@Request() request: RequestWithUser,
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("keyword") keyword?: string,
|
|
) {
|
|
let _data = await new permission().PermissionList(request, "SYS_EVA_COMPETENCY");
|
|
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.orWhere("kpiGroup.nameGroupKPI LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("positions.name LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("kpiCapacitys.name LIKE :keyword", { keyword: `%${keyword}%` });
|
|
}),
|
|
)
|
|
.getManyAndCount();
|
|
|
|
if (_total === 0) {
|
|
return new HttpSuccess({
|
|
data: [],
|
|
total: 0,
|
|
});
|
|
}
|
|
|
|
const [kpiLink, total] = await AppDataSource.getRepository(KpiLink)
|
|
.createQueryBuilder("kpiLink")
|
|
.leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup")
|
|
.leftJoinAndSelect("kpiLink.positions", "positions")
|
|
.leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys")
|
|
.andWhere("kpiLink.id In (:id)", {
|
|
id: _kpiLink.map((x) => x.id),
|
|
})
|
|
.skip((page - 1) * pageSize)
|
|
.take(pageSize)
|
|
.orderBy("kpiLink.createdAt", "ASC")
|
|
.getManyAndCount();
|
|
|
|
if (_total === 0) {
|
|
return new HttpSuccess({
|
|
data: [],
|
|
total: 0,
|
|
});
|
|
}
|
|
|
|
const formattedResponse = kpiLink.map((item) => ({
|
|
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,
|
|
name: capacity.name,
|
|
type: capacity.type,
|
|
description: capacity.description,
|
|
})),
|
|
}));
|
|
return new HttpSuccess({ data: formattedResponse, total });
|
|
}
|
|
/**
|
|
* API เชื่อมโยง
|
|
* @param id
|
|
*/
|
|
@Get("{id}")
|
|
async KpiLinkById(@Request() request: RequestWithUser, @Path() id: string) {
|
|
const kpiLink = await this.kpiLinkRepository.findOne({
|
|
where: { id: id },
|
|
relations: ["positions", "kpiCapacitys", "kpiGroup"],
|
|
order: {
|
|
createdAt: "ASC",
|
|
},
|
|
});
|
|
if (!kpiLink) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
}
|
|
const formattedResponse = {
|
|
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,
|
|
name: capacity.name,
|
|
type: capacity.type,
|
|
description: capacity.description,
|
|
})),
|
|
};
|
|
return new HttpSuccess(formattedResponse);
|
|
}
|
|
|
|
/**
|
|
* API ลบเชื่อมโยง
|
|
* @param id
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiLink(@Path() id: string, @Request() request: RequestWithUser) {
|
|
await new permission().PermissionDelete(request, "SYS_EVA_COMPETENCY");
|
|
const kpiLink = await this.kpiLinkRepository.findOne({
|
|
where: { id: id },
|
|
relations: ["kpiCapacitys"],
|
|
});
|
|
if (!kpiLink) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการเชื่อมโยง");
|
|
}
|
|
|
|
kpiLink.kpiCapacitys = [];
|
|
await this.kpiLinkRepository.save(kpiLink, { data: request });
|
|
|
|
await this.positionRepository.delete({ kpiLinkId: id });
|
|
await this.kpiLinkRepository.delete({ id: id });
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API list เชื่อมโยง
|
|
* @param page
|
|
* @param pageSize
|
|
*/
|
|
@Get()
|
|
async listKpiLink(
|
|
@Request() request: RequestWithUser,
|
|
@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.orWhere("kpiGroup.nameGroupKPI LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("positions.name LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("kpiCapacitys.name LIKE :keyword", { keyword: `%${keyword}%` });
|
|
}),
|
|
)
|
|
.getManyAndCount();
|
|
|
|
if (_total === 0) {
|
|
return new HttpSuccess({
|
|
data: [],
|
|
total: 0,
|
|
});
|
|
}
|
|
|
|
const [kpiLink, total] = await AppDataSource.getRepository(KpiLink)
|
|
.createQueryBuilder("kpiLink")
|
|
.leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup")
|
|
.leftJoinAndSelect("kpiLink.positions", "positions")
|
|
.leftJoinAndSelect("kpiLink.kpiCapacitys", "kpiCapacitys")
|
|
.andWhere("kpiLink.id In (:id)", {
|
|
id: _kpiLink.map((x) => x.id),
|
|
})
|
|
.skip((page - 1) * pageSize)
|
|
.take(pageSize)
|
|
.orderBy("kpiLink.createdAt", "ASC")
|
|
.getManyAndCount();
|
|
|
|
if (_total === 0) {
|
|
return new HttpSuccess({
|
|
data: [],
|
|
total: 0,
|
|
});
|
|
}
|
|
|
|
const formattedResponse = kpiLink.map((item) => ({
|
|
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,
|
|
name: capacity.name,
|
|
type: capacity.type,
|
|
description: capacity.description,
|
|
})),
|
|
}));
|
|
return new HttpSuccess({ data: formattedResponse, total });
|
|
}
|
|
}
|