2024-04-18 17:40:49 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
Example,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Response,
|
|
|
|
|
Query,
|
2024-04-22 15:50:04 +07:00
|
|
|
ArrayValidator,
|
2024-04-18 17:40:49 +07:00
|
|
|
} 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";
|
2024-05-01 13:35:12 +07:00
|
|
|
import { Position } from "../entities/position";
|
2024-04-22 15:50:04 +07:00
|
|
|
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-05-01 13:35:12 +07:00
|
|
|
private positionRepository = AppDataSource.getRepository(Position);
|
2024-04-18 17:40:49 +07:00
|
|
|
|
|
|
|
|
/**
|
2024-04-19 12:59:57 +07:00
|
|
|
* API สร้างรายการสมรรถนะ
|
2024-04-22 15:50:04 +07:00
|
|
|
*
|
2024-04-19 12:59:57 +07:00
|
|
|
* @summary สร้างรายการสมรรถนะ
|
2024-04-22 15:50:04 +07:00
|
|
|
*
|
2024-04-18 17:40:49 +07:00
|
|
|
*/
|
|
|
|
|
@Post()
|
|
|
|
|
@Example({
|
2024-04-19 12:59:57 +07:00
|
|
|
type: "HEAD",
|
|
|
|
|
name: "ชื่อสมรรถนะ",
|
|
|
|
|
description: "คำจำกัดความ",
|
2024-04-22 15:50:04 +07:00
|
|
|
kpiCapacityDetails: [
|
|
|
|
|
{
|
|
|
|
|
level: "ระดับ",
|
|
|
|
|
description: "คำอธิบายระดับ",
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-04-18 17:40:49 +07:00
|
|
|
})
|
|
|
|
|
async createKpiCapacity(
|
2024-04-22 15:50:04 +07:00
|
|
|
@Body()
|
|
|
|
|
requestBody: {
|
|
|
|
|
type: string;
|
|
|
|
|
name: string;
|
|
|
|
|
description: string;
|
2024-04-19 12:59:57 +07:00
|
|
|
capacityDetails: {
|
2024-04-19 14:09:49 +07:00
|
|
|
level: string;
|
2024-04-18 17:40:49 +07:00
|
|
|
description: string;
|
|
|
|
|
}[];
|
|
|
|
|
},
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
2024-04-22 15:50:04 +07:00
|
|
|
) {
|
2024-04-19 12:59:57 +07:00
|
|
|
const kpiCapacity = Object.assign(new KpiCapacity(), {
|
|
|
|
|
type: requestBody.type,
|
|
|
|
|
name: requestBody.name,
|
2024-04-22 15:50:04 +07:00
|
|
|
description: requestBody.description,
|
2024-04-19 12:59:57 +07:00
|
|
|
});
|
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) {
|
2024-04-22 15:50:04 +07:00
|
|
|
idx += 1;
|
|
|
|
|
let _level =
|
|
|
|
|
kpiCapacity.type === "HEAD" || kpiCapacity.type === "GROUP" ? idx.toString() : data.level;
|
2024-04-19 12:59:57 +07:00
|
|
|
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
2024-04-19 14:09:49 +07:00
|
|
|
level: _level,
|
2024-04-19 12:59:57 +07:00
|
|
|
description: data.description,
|
2024-04-22 15:50:04 +07:00
|
|
|
kpiCapacityId: kpiCapacity.id,
|
2024-04-19 12:59:57 +07:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 15:50:04 +07:00
|
|
|
return new HttpSuccess(kpiCapacity.id);
|
2024-04-18 17:40:49 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
/**
|
|
|
|
|
* API แก้ไขรายการสมรรถนะ
|
2024-04-22 15:50:04 +07:00
|
|
|
*
|
2024-04-19 12:59:57 +07:00
|
|
|
* @summary แก้ไขรายการสมรรถนะ
|
2024-04-22 15:50:04 +07:00
|
|
|
*
|
2024-04-19 12:59:57 +07:00
|
|
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
|
|
|
|
*/
|
|
|
|
|
@Put("{id}")
|
|
|
|
|
@Example({
|
|
|
|
|
type: "HEAD",
|
|
|
|
|
name: "ชื่อสมรรถนะ",
|
|
|
|
|
description: "คำจำกัดความ",
|
2024-04-22 15:50:04 +07:00
|
|
|
kpiCapacityDetails: [
|
|
|
|
|
{
|
|
|
|
|
level: "ระดับ",
|
|
|
|
|
description: "คำอธิบายระดับ",
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-04-19 12:59:57 +07:00
|
|
|
})
|
|
|
|
|
async updateKpiCapacity(
|
|
|
|
|
@Path() id: string,
|
2024-04-22 15:50:04 +07:00
|
|
|
@Body()
|
|
|
|
|
requestBody: {
|
|
|
|
|
type: string;
|
|
|
|
|
name: string;
|
|
|
|
|
description: string;
|
2024-04-19 12:59:57 +07:00
|
|
|
capacityDetails: {
|
2024-04-22 15:50:04 +07:00
|
|
|
level: string;
|
2024-04-19 12:59:57 +07:00
|
|
|
description: string;
|
|
|
|
|
}[];
|
|
|
|
|
},
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
|
|
|
where: { id: id },
|
|
|
|
|
});
|
|
|
|
|
if (!kpiCapacity) {
|
2024-04-22 15:50:04 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
2024-04-19 12:59:57 +07:00
|
|
|
}
|
|
|
|
|
const _kpiCapacity = Object.assign(new KpiCapacity(), {
|
|
|
|
|
type: requestBody.type,
|
|
|
|
|
name: requestBody.name,
|
2024-04-22 15:50:04 +07:00
|
|
|
description: requestBody.description,
|
2024-04-19 12:59:57 +07:00
|
|
|
});
|
|
|
|
|
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) {
|
2024-04-22 15:50:04 +07:00
|
|
|
idx += 1;
|
|
|
|
|
let _level =
|
|
|
|
|
kpiCapacity.type === "HEAD" || kpiCapacity.type === "GROUP" ? idx.toString() : data.level;
|
2024-04-19 12:59:57 +07:00
|
|
|
const kpiCapacityDetail = Object.assign(new KpiCapacityDetail(), {
|
2024-04-19 14:09:49 +07:00
|
|
|
level: _level,
|
2024-04-19 12:59:57 +07:00
|
|
|
description: data.description,
|
2024-04-22 15:50:04 +07:00
|
|
|
kpiCapacityId: kpiCapacity.id,
|
2024-04-19 12:59:57 +07:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-04-22 15:50:04 +07:00
|
|
|
|
|
|
|
|
return new HttpSuccess(kpiCapacity.id);
|
2024-04-19 12:59:57 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-30 17:57:09 +07:00
|
|
|
/**
|
|
|
|
|
* API รายละเอียดรายการสมรรถนะ "สมรรถนะหลัก"
|
|
|
|
|
*
|
|
|
|
|
* @summary รายละเอียดดรายการสมรรถนะ "สมรรถนะหลัก"
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("head")
|
|
|
|
|
async GetKpiCapacityTypeHEAD() {
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
type: In(["HEAD"]),
|
|
|
|
|
kpiCapacityDetails: { level: "1" }
|
|
|
|
|
},
|
|
|
|
|
select: ["id", "name"],
|
|
|
|
|
relations: ["kpiCapacityDetails"],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mapData = kpiCapacity.map((x) => ({
|
|
|
|
|
id: x.id,
|
|
|
|
|
name: x.name,
|
|
|
|
|
level: x.kpiCapacityDetails.length > 0 ? x.kpiCapacityDetails[0].level : null,
|
|
|
|
|
description: x.kpiCapacityDetails.length > 0 ? x.kpiCapacityDetails[0].description : null,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess(mapData);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 13:35:12 +07:00
|
|
|
/**
|
|
|
|
|
* API รายละเอียดรายการสมรรถนะ "สมรรถนะประจำกลุ่มงาน"
|
|
|
|
|
*
|
|
|
|
|
* @summary รายละเอียดดรายการสมรรถนะ "สมรรถนะประจำกลุ่มงาน"
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("group")
|
|
|
|
|
async GetKpiCapacityTypeGROUP(
|
|
|
|
|
@Query("positionName") positionName: string
|
|
|
|
|
) {
|
|
|
|
|
|
2024-05-29 17:28:39 +07:00
|
|
|
let position = await this.positionRepository.findOne({
|
2024-05-01 13:35:12 +07:00
|
|
|
where: { name: Like(`%${positionName}%`) },
|
|
|
|
|
relations: ["kpiLink.kpiCapacitys"]
|
|
|
|
|
})
|
2024-05-29 17:28:39 +07:00
|
|
|
|
|
|
|
|
if (position == null) {
|
|
|
|
|
position = await this.positionRepository.findOne({
|
|
|
|
|
where: { name: "นักจัดการงานทั่วไป" },
|
|
|
|
|
relations: ["kpiLink.kpiCapacitys"]
|
|
|
|
|
})
|
2024-05-01 13:35:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let positionLinkId: any
|
|
|
|
|
positionLinkId = position != null ? position : null
|
|
|
|
|
const kpiCapacityIds = positionLinkId.kpiLink.kpiCapacitys.map((kpiCapacity:any) => kpiCapacity.id)
|
|
|
|
|
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
id: In(kpiCapacityIds),
|
|
|
|
|
type: In(["GROUP"]),
|
|
|
|
|
kpiCapacityDetails: { level: "2" }
|
|
|
|
|
},
|
|
|
|
|
select: ["id", "name"],
|
|
|
|
|
relations: ["kpiCapacityDetails",],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mapData = kpiCapacity.map((x) => ({
|
|
|
|
|
id: x.id,
|
|
|
|
|
name: x.name,
|
|
|
|
|
level: x.kpiCapacityDetails.length > 0 ? x.kpiCapacityDetails[0].level : null,
|
|
|
|
|
description: x.kpiCapacityDetails.length > 0 ? x.kpiCapacityDetails[0].description : null,
|
|
|
|
|
}));
|
|
|
|
|
return new HttpSuccess(mapData);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
/**
|
|
|
|
|
* API รายละเอียดรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @summary รายละเอียดดรายการสมรรถนะ
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
|
|
|
|
*/
|
|
|
|
|
@Get("{id}")
|
|
|
|
|
@Example({
|
|
|
|
|
type: "HEAD",
|
|
|
|
|
name: "ชื่อสมรรถนะ",
|
|
|
|
|
description: "คำจำกัดความ",
|
2024-04-22 15:50:04 +07:00
|
|
|
kpiCapacityDetails: [
|
|
|
|
|
{
|
|
|
|
|
level: 1,
|
|
|
|
|
description: "คำอธิบายระดับ",
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-04-19 12:59:57 +07:00
|
|
|
})
|
|
|
|
|
async GetKpiCapacityById(@Path() id: string) {
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
|
|
|
where: { id: id },
|
|
|
|
|
select: ["type", "name", "description"],
|
2024-04-22 15:50:04 +07:00
|
|
|
});
|
2024-04-19 12:59:57 +07:00
|
|
|
if (!kpiCapacity) {
|
2024-04-22 15:50:04 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
2024-04-19 12:59:57 +07:00
|
|
|
}
|
|
|
|
|
const kpiCapacityDetails = await this.kpiCapacityDetailRepository.find({
|
|
|
|
|
where: { kpiCapacityId: id },
|
|
|
|
|
select: ["level", "description"],
|
2024-04-22 15:50:04 +07:00
|
|
|
order: { level: "ASC" },
|
|
|
|
|
});
|
2024-04-19 12:59:57 +07:00
|
|
|
const mapData = {
|
|
|
|
|
type: kpiCapacity.type,
|
|
|
|
|
name: kpiCapacity.name,
|
|
|
|
|
description: kpiCapacity.description,
|
2024-04-22 15:50:04 +07:00
|
|
|
capacityDetails: kpiCapacityDetails,
|
|
|
|
|
};
|
2024-04-19 12:59:57 +07:00
|
|
|
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,
|
|
|
|
|
) {
|
|
|
|
|
const [kpiCapacity, total] = await AppDataSource.getRepository(KpiCapacity)
|
|
|
|
|
.createQueryBuilder("kpiCapacity")
|
2024-04-22 15:50:04 +07:00
|
|
|
.leftJoinAndSelect("kpiCapacity.kpiCapacityDetails", "kpiCapacityDetail")
|
2024-04-19 12:59:57 +07:00
|
|
|
.andWhere(
|
|
|
|
|
keyword == undefined
|
|
|
|
|
? "1=1"
|
2024-04-22 15:50:04 +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-23 18:15:27 +07:00
|
|
|
.orderBy("kpiCapacity.createdAt", "ASC")
|
2024-04-19 12:59:57 +07:00
|
|
|
.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,
|
2024-04-22 15:50:04 +07:00
|
|
|
capacityDetails: item.kpiCapacityDetails.map((detail) => {
|
2024-04-19 12:59:57 +07:00
|
|
|
return {
|
|
|
|
|
id: detail.id,
|
|
|
|
|
description: detail.description,
|
|
|
|
|
level: detail.level,
|
2024-04-22 15:50:04 +07:00
|
|
|
capacityId: detail.kpiCapacityId,
|
2024-04-19 12:59:57 +07:00
|
|
|
};
|
2024-04-22 15:50:04 +07:00
|
|
|
}),
|
2024-04-19 12:59:57 +07:00
|
|
|
}));
|
|
|
|
|
return new HttpSuccess({ data: mapFormula, total });
|
|
|
|
|
}
|
2024-04-18 17:40:49 +07:00
|
|
|
|
2024-04-19 12:59:57 +07:00
|
|
|
/**
|
|
|
|
|
* API ลบรายการสมรรถนะ
|
2024-04-22 15:50:04 +07:00
|
|
|
*
|
2024-04-19 12:59:57 +07:00
|
|
|
* @summary ลบรายการสมรรถนะ
|
2024-04-22 15:50:04 +07:00
|
|
|
*
|
2024-04-19 12:59:57 +07:00
|
|
|
* @param {string} id Guid, *Id รายการสมรรถนะ
|
|
|
|
|
*/
|
|
|
|
|
@Delete("{id}")
|
|
|
|
|
async deleteKpiCapacity(@Path() id: string) {
|
|
|
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
|
|
|
where: { id: id },
|
|
|
|
|
});
|
|
|
|
|
if (!kpiCapacity) {
|
2024-04-22 15:50:04 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
2024-04-19 12:59:57 +07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|