2024-04-18 17:40:49 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
Example,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Response,
|
|
|
|
|
Query,
|
|
|
|
|
ArrayValidator
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import { KpiCapacity, createKpiCapacity, updateKpiCapacity } from "../entities/kpiCapacity";
|
|
|
|
|
import { KpiCapacityDetail, createKpiCapacityDetail, updateKpiCapacityDetail } from "../entities/kpiCapacityDetail";
|
2024-04-19 12:59:57 +07:00
|
|
|
import { Like, In } from "typeorm";
|
2024-04-18 17:40:49 +07:00
|
|
|
|
|
|
|
|
@Route("api/v1/kpi/capacity")
|
|
|
|
|
@Tags("kpiCapacity")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
|
|
|
export class kpiCapacityController extends Controller {
|
|
|
|
|
private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity);
|
|
|
|
|
private kpiCapacityDetailRepository = AppDataSource.getRepository(KpiCapacityDetail);
|
|
|
|
|
|
|
|
|
|
/**
|
2024-04-19 12:59:57 +07:00
|
|
|
* API สร้างรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @summary สร้างรายการสมรรถนะ
|
|
|
|
|
*
|
2024-04-18 17:40:49 +07:00
|
|
|
*/
|
|
|
|
|
@Post()
|
|
|
|
|
@Example({
|
2024-04-19 12:59:57 +07:00
|
|
|
type: "HEAD",
|
|
|
|
|
name: "ชื่อสมรรถนะ",
|
|
|
|
|
description: "คำจำกัดความ",
|
2024-04-18 17:40:49 +07:00
|
|
|
kpiCapacityDetails: [{
|
2024-04-19 12:59:57 +07:00
|
|
|
description: "คำอธิบายระดับ"
|
2024-04-18 17:40:49 +07:00
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
async createKpiCapacity(
|
|
|
|
|
// @Body() requestBody: createKpiCapacity,
|
|
|
|
|
@Body() requestBody: {
|
|
|
|
|
type: string
|
|
|
|
|
name: string
|
|
|
|
|
description: string
|
2024-04-19 12:59:57 +07:00
|
|
|
capacityDetails: {
|
|
|
|
|
// level: number;
|
2024-04-18 17:40:49 +07:00
|
|
|
description: string;
|
|
|
|
|
}[];
|
|
|
|
|
},
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
2024-04-19 12:59:57 +07:00
|
|
|
){
|
|
|
|
|
const kpiCapacity = Object.assign(new KpiCapacity(), {
|
|
|
|
|
type: requestBody.type,
|
|
|
|
|
name: requestBody.name,
|
|
|
|
|
description: requestBody.description
|
|
|
|
|
});
|
2024-04-18 17:40:49 +07:00
|
|
|
kpiCapacity.createdUserId = request.user.sub;
|
|
|
|
|
kpiCapacity.createdFullName = request.user.name;
|
|
|
|
|
kpiCapacity.lastUpdateUserId = request.user.sub;
|
|
|
|
|
kpiCapacity.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.kpiCapacityRepository.save(kpiCapacity);
|
2024-04-19 12:59:57 +07:00
|
|
|
|
|
|
|
|
let idx: number = 0;
|
|
|
|
|
for (const data of requestBody.capacityDetails) {
|
|
|
|
|
idx += 1
|
|
|
|
|
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
|
|
|
|
level: idx,
|
|
|
|
|
description: data.description,
|
|
|
|
|
kpiCapacityId: kpiCapacity.id
|
|
|
|
|
});
|
|
|
|
|
kpiCapacityDetail.createdUserId = request.user.sub;
|
|
|
|
|
kpiCapacityDetail.createdFullName = request.user.name;
|
|
|
|
|
kpiCapacityDetail.lastUpdateUserId = request.user.sub;
|
|
|
|
|
kpiCapacityDetail.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.kpiCapacityDetailRepository.save(kpiCapacityDetail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess(kpiCapacity.id)
|
2024-04-18 17:40:49 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
/**
|
|
|
|
|
* API แก้ไขรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @summary แก้ไขรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
|
|
|
|
*/
|
|
|
|
|
@Put("{id}")
|
|
|
|
|
@Example({
|
|
|
|
|
type: "HEAD",
|
|
|
|
|
name: "ชื่อสมรรถนะ",
|
|
|
|
|
description: "คำจำกัดความ",
|
|
|
|
|
kpiCapacityDetails: [{
|
|
|
|
|
description: "คำอธิบายระดับ"
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
async updateKpiCapacity(
|
|
|
|
|
@Path() id: string,
|
|
|
|
|
@Body() requestBody: {
|
|
|
|
|
type: string
|
|
|
|
|
name: string
|
|
|
|
|
description: string
|
|
|
|
|
capacityDetails: {
|
|
|
|
|
description: string;
|
|
|
|
|
}[];
|
|
|
|
|
},
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
|
|
|
where: { id: id },
|
|
|
|
|
});
|
|
|
|
|
if (!kpiCapacity) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatusCode.NOT_FOUND,
|
|
|
|
|
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const _kpiCapacity = Object.assign(new KpiCapacity(), {
|
|
|
|
|
type: requestBody.type,
|
|
|
|
|
name: requestBody.name,
|
|
|
|
|
description: requestBody.description
|
|
|
|
|
});
|
|
|
|
|
kpiCapacity.lastUpdateUserId = request.user.sub;
|
|
|
|
|
kpiCapacity.lastUpdateFullName = request.user.name;
|
|
|
|
|
this.kpiCapacityRepository.merge(kpiCapacity, _kpiCapacity);
|
|
|
|
|
await this.kpiCapacityRepository.save(kpiCapacity);
|
2024-04-18 17:40:49 +07:00
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
const _kpiCapacityDetailsOld = await this.kpiCapacityDetailRepository.find({
|
|
|
|
|
where: { kpiCapacityId: kpiCapacity.id },
|
|
|
|
|
});
|
|
|
|
|
await this.kpiCapacityDetailRepository.remove(_kpiCapacityDetailsOld);
|
2024-04-18 17:40:49 +07:00
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
let idx: number = 0;
|
|
|
|
|
for (const data of requestBody.capacityDetails) {
|
|
|
|
|
idx += 1
|
|
|
|
|
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
|
|
|
|
level: idx,
|
|
|
|
|
description: data.description,
|
|
|
|
|
kpiCapacityId: kpiCapacity.id
|
|
|
|
|
});
|
|
|
|
|
kpiCapacityDetail.createdUserId = request.user.sub;
|
|
|
|
|
kpiCapacityDetail.createdFullName = request.user.name;
|
|
|
|
|
kpiCapacityDetail.lastUpdateUserId = request.user.sub;
|
|
|
|
|
kpiCapacityDetail.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.kpiCapacityDetailRepository.save(kpiCapacityDetail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess(kpiCapacity.id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API รายละเอียดรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @summary รายละเอียดดรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
|
|
|
|
*/
|
|
|
|
|
@Get("{id}")
|
|
|
|
|
@Example({
|
|
|
|
|
type: "HEAD",
|
|
|
|
|
name: "ชื่อสมรรถนะ",
|
|
|
|
|
description: "คำจำกัดความ",
|
|
|
|
|
kpiCapacityDetails: [{
|
|
|
|
|
level: 1,
|
|
|
|
|
description: "คำอธิบายระดับ"
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
async GetKpiCapacityById(@Path() id: string) {
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
|
|
|
where: { id: id },
|
|
|
|
|
select: ["type", "name", "description"],
|
|
|
|
|
})
|
|
|
|
|
if (!kpiCapacity) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatusCode.NOT_FOUND,
|
|
|
|
|
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({
|
|
|
|
|
where: { kpiCapacityId: id },
|
|
|
|
|
select: ["level", "description"],
|
|
|
|
|
order: { "level": "ASC" }
|
|
|
|
|
})
|
|
|
|
|
const mapData = {
|
|
|
|
|
type: kpiCapacity.type,
|
|
|
|
|
name: kpiCapacity.name,
|
|
|
|
|
description: kpiCapacity.description,
|
|
|
|
|
capacityDetails: kpiCapacityDetails
|
|
|
|
|
}
|
|
|
|
|
return new HttpSuccess(mapData);
|
|
|
|
|
}
|
2024-04-18 17:40:49 +07:00
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
/**
|
|
|
|
|
* API รายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @summary รายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get()
|
|
|
|
|
async listKpiCapacity(
|
|
|
|
|
@Query("page") page: number = 1,
|
|
|
|
|
@Query("pageSize") pageSize: number = 10,
|
|
|
|
|
@Query("type") type?: string,
|
|
|
|
|
@Query("keyword") keyword?: string,
|
|
|
|
|
) {
|
2024-04-18 17:40:49 +07:00
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
const [kpiCapacity, total] = await AppDataSource.getRepository(KpiCapacity)
|
|
|
|
|
.createQueryBuilder("kpiCapacity")
|
|
|
|
|
.leftJoinAndSelect("kpiCapacity.KpiCapacityDetails", "kpiCapacityDetail")
|
|
|
|
|
.andWhere(
|
|
|
|
|
keyword == undefined
|
|
|
|
|
? "1=1"
|
|
|
|
|
: [
|
2024-04-19 13:39:25 +07:00
|
|
|
{ name: Like(`%${keyword}%`) },
|
|
|
|
|
{ description: Like(`%${keyword}%`) },
|
2024-04-19 12:59:57 +07:00
|
|
|
],
|
|
|
|
|
)
|
2024-04-19 13:39:25 +07:00
|
|
|
.andWhere(type == undefined ? "1=1" : { type: type })
|
2024-04-19 12:59:57 +07:00
|
|
|
.orderBy("kpiCapacityDetail.level", "ASC")
|
|
|
|
|
.skip((page - 1) * pageSize)
|
|
|
|
|
.take(pageSize)
|
|
|
|
|
.getManyAndCount();
|
2024-04-18 17:40:49 +07:00
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
const mapFormula = kpiCapacity.map((item) => ({
|
|
|
|
|
id: item.id,
|
|
|
|
|
type: item.type,
|
|
|
|
|
name: item.name,
|
|
|
|
|
description: item.description,
|
|
|
|
|
capacityDetails: item.KpiCapacityDetails.map(detail => {
|
|
|
|
|
return {
|
|
|
|
|
id: detail.id,
|
|
|
|
|
description: detail.description,
|
|
|
|
|
level: detail.level,
|
|
|
|
|
capacityId: detail.kpiCapacityId
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
}));
|
|
|
|
|
return new HttpSuccess({ data: mapFormula, total });
|
|
|
|
|
}
|
2024-04-18 17:40:49 +07:00
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
/**
|
|
|
|
|
* API ลบรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @summary ลบรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
|
|
|
|
*/
|
|
|
|
|
@Delete("{id}")
|
|
|
|
|
async deleteKpiCapacity(@Path() id: string) {
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
|
|
|
where: { id: id },
|
|
|
|
|
});
|
|
|
|
|
if (!kpiCapacity) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatusCode.NOT_FOUND,
|
|
|
|
|
"ไม่พบข้อมูลรายการสมรรถนะนี้",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({
|
|
|
|
|
where: { kpiCapacityId: id },
|
|
|
|
|
});
|
|
|
|
|
if (kpiCapacityDetails.length > 0) {
|
|
|
|
|
await this.kpiCapacityDetailRepository.remove(kpiCapacityDetails);
|
|
|
|
|
}
|
|
|
|
|
await this.kpiCapacityRepository.remove(kpiCapacity);
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
2024-04-18 17:40:49 +07:00
|
|
|
}
|