304 lines
12 KiB
TypeScript
304 lines
12 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 HttpStatusCode from "../interfaces/http-status";
|
|
import { KpiCapacity } from "../entities/kpiCapacity";
|
|
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
|
|
import { KpiUserCapacity, KpiUserCapacityDataPoint } from "../entities/kpiUserCapacity";
|
|
import { Not } from "typeorm";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import { setLogDataDiff } from "../interfaces/utils";
|
|
|
|
@Route("api/v1/kpi/user/capacity")
|
|
@Tags("kpiUserCapacity")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class KpiUserCapacityController extends Controller {
|
|
private kpiUserEvalutionRepository = AppDataSource.getRepository(KpiUserEvaluation);
|
|
private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity);
|
|
private kpiUserCapacityRepository = AppDataSource.getRepository(KpiUserCapacity);
|
|
|
|
/**
|
|
* API ล้างข้อมูล
|
|
*
|
|
* @summary ล้างข้อมูล
|
|
*
|
|
*/
|
|
@Get("clear-db")
|
|
async ClearDb() {
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API สร้างองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @summary สร้างองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
*
|
|
*/
|
|
@Post()
|
|
async CreateKpiUserCapacity(
|
|
@Body()
|
|
requestBody: {
|
|
kpiUserEvaluationId: string;
|
|
kpiCapacityId: string;
|
|
level: string | null;
|
|
weight: number;
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
// await new permission().PermissionCreate(request, "SYS_KPI_LIST");
|
|
const kpiUserEvalution = await this.kpiUserEvalutionRepository.findOne({
|
|
where: { id: requestBody.kpiUserEvaluationId },
|
|
});
|
|
if (!kpiUserEvalution) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ)นี้",
|
|
);
|
|
}
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
where: { id: requestBody.kpiCapacityId },
|
|
});
|
|
if (!kpiCapacity) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
|
}
|
|
const chkRepleat = await this.kpiUserCapacityRepository.find({
|
|
where: {
|
|
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
|
|
kpiCapacityId: requestBody.kpiCapacityId,
|
|
},
|
|
});
|
|
if (chkRepleat.length > 0) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากรายการสมรรถนะซ้ำ",
|
|
);
|
|
}
|
|
const before = null;
|
|
|
|
const kpiUserCapacity = Object.assign(new KpiUserCapacity(), requestBody);
|
|
kpiUserCapacity.createdUserId = request.user.sub;
|
|
kpiUserCapacity.createdFullName = request.user.name;
|
|
kpiUserCapacity.lastUpdateUserId = request.user.sub;
|
|
kpiUserCapacity.lastUpdateFullName = request.user.name;
|
|
kpiUserCapacity.createdAt = new Date();
|
|
kpiUserCapacity.lastUpdatedAt = new Date();
|
|
await this.kpiUserCapacityRepository.save(kpiUserCapacity, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiUserCapacity });
|
|
|
|
return new HttpSuccess(kpiUserCapacity.id);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไของค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @summary แก้ไของค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @param {string} id Guid, *Id องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*/
|
|
@Put("{id}")
|
|
async updateKpiUserCapacity(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: {
|
|
kpiUserEvaluationId: string;
|
|
kpiCapacityId: string;
|
|
level: string;
|
|
weight: number;
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
// await new permission().PermissionUpdate(request, "SYS_KPI_LIST");
|
|
const kpiUserEvalution = await this.kpiUserEvalutionRepository.findOne({
|
|
where: { id: requestBody.kpiUserEvaluationId },
|
|
});
|
|
if (!kpiUserEvalution) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
|
|
);
|
|
}
|
|
const kpiCapacity = await this.kpiCapacityRepository.findOne({
|
|
where: { id: requestBody.kpiCapacityId },
|
|
});
|
|
if (!kpiCapacity) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
|
|
}
|
|
const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiUserCapacity) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) นี้",
|
|
);
|
|
}
|
|
const chkRepleat = await this.kpiUserCapacityRepository.find({
|
|
where: {
|
|
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
|
|
kpiCapacityId: requestBody.kpiCapacityId,
|
|
id: Not(id),
|
|
},
|
|
});
|
|
if (chkRepleat.length > 0) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่สามารถแก้ไขข้อมูลได้เนื่องจากรายการสมรรถนะซ้ำ",
|
|
);
|
|
}
|
|
const before = structuredClone(kpiUserCapacity);
|
|
const _kpiUserCapacity = Object.assign(new KpiUserCapacity(), requestBody);
|
|
kpiUserCapacity.lastUpdateUserId = request.user.sub;
|
|
kpiUserCapacity.lastUpdateFullName = request.user.name;
|
|
kpiUserCapacity.lastUpdatedAt = new Date();
|
|
this.kpiUserCapacityRepository.merge(kpiUserCapacity, _kpiUserCapacity);
|
|
await this.kpiUserCapacityRepository.save(kpiUserCapacity, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiUserCapacity });
|
|
|
|
return new HttpSuccess(kpiUserCapacity.id);
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @summary รายละเอียดองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @param {string} id Guid, *Id องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*/
|
|
@Get("{id}")
|
|
async GetKpiUserCapacityById(@Request() request: RequestWithUser, @Path() id: string) {
|
|
const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({
|
|
where: { id: id },
|
|
relations: ["kpiCapacity"],
|
|
});
|
|
if (!kpiUserCapacity) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ)นี้",
|
|
);
|
|
}
|
|
const mapData = {
|
|
id: kpiUserCapacity.id,
|
|
// capacityId: kpiUserCapacity.kpiCapacity.id,
|
|
name: kpiUserCapacity.kpiCapacity.name,
|
|
level: kpiUserCapacity.level,
|
|
point: kpiUserCapacity.point,
|
|
weight: kpiUserCapacity.weight,
|
|
summary: kpiUserCapacity.summary,
|
|
};
|
|
return new HttpSuccess(mapData);
|
|
}
|
|
|
|
/**
|
|
* API องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @summary องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
*/
|
|
@Get()
|
|
async listKpiUserCapacity(
|
|
@Request() request: RequestWithUser,
|
|
@Query("id") id: string, //kpiUserEvaluationId
|
|
@Query("type") type: string,
|
|
) {
|
|
const [kpiUserCapacity, total] = await AppDataSource.getRepository(KpiUserCapacity)
|
|
.createQueryBuilder("kpiUserCapacity")
|
|
.leftJoinAndSelect("kpiUserCapacity.kpiCapacity", "kpiCapacity")
|
|
.leftJoinAndSelect("kpiCapacity.kpiCapacityDetails", "kpiCapacityDetails")
|
|
.andWhere("kpiUserCapacity.kpiUserEvaluationId = :id", { id: id })
|
|
.andWhere(type ? "kpiCapacity.type LIKE :type" : "1=1", { type: type.toLocaleUpperCase() })
|
|
.orderBy("kpiUserCapacity.createdAt", "ASC")
|
|
.getManyAndCount();
|
|
|
|
const mapData = kpiUserCapacity.map((item) => ({
|
|
id: item.id,
|
|
name: item.kpiCapacity.name,
|
|
level: item.level,
|
|
point: item.point,
|
|
weight: item.weight,
|
|
summary: item.summary,
|
|
achievement: item.kpiCapacity.kpiCapacityDetails,
|
|
}));
|
|
|
|
return new HttpSuccess({ data: mapData, total });
|
|
}
|
|
|
|
/**
|
|
* API ลบองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @summary ลบองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*
|
|
* @param {string} id Guid, *Id องค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ) (USER)
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiUserCapacity(@Path() id: string, @Request() request: RequestWithUser) {
|
|
// await new permission().PermissionDelete(request, "SYS_KPI_LIST");
|
|
const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiUserCapacity) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ)นี้",
|
|
);
|
|
}
|
|
await this.kpiUserCapacityRepository.remove(kpiUserCapacity);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API กรอกระดับคะแนนตามเกณฑ์การประเมิน (สมรรถนะ) (USER)
|
|
*
|
|
* @summary กรอกระดับคะแนนตามเกณฑ์การประเมิน (สมรรถนะ) (USER)
|
|
*
|
|
*
|
|
*/
|
|
@Post("point")
|
|
async CreateKpiUserCapacityPoint(
|
|
@Body() requestBody: KpiUserCapacityDataPoint[],
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
// await new permission().PermissionCreate(request, "SYS_KPI_LIST");
|
|
for (const item of requestBody) {
|
|
const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({
|
|
where: { id: item.id },
|
|
});
|
|
if (!kpiUserCapacity) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
`ไม่พบข้อมูลพฤติกรรมการปฎิบัติราชการ (สมรรถนะ): ${item.id}`,
|
|
);
|
|
}
|
|
const before = structuredClone(kpiUserCapacity);
|
|
|
|
this.kpiUserCapacityRepository.merge(kpiUserCapacity, item);
|
|
kpiUserCapacity.lastUpdateUserId = request.user.sub;
|
|
kpiUserCapacity.lastUpdateFullName = request.user.name;
|
|
kpiUserCapacity.lastUpdatedAt = new Date();
|
|
await this.kpiUserCapacityRepository.save(kpiUserCapacity, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiUserCapacity });
|
|
}
|
|
return new HttpSuccess();
|
|
}
|
|
}
|