hrms-api-kpi/src/controllers/KpiUserCapacityController.ts

294 lines
12 KiB
TypeScript
Raw Normal View History

2024-04-23 12:35:03 +07:00
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";
2024-04-26 12:40:43 +07:00
import { KpiUserCapacity, KpiUserCapacityDataPoint } from "../entities/kpiUserCapacity";
import { Not } from "typeorm";
2024-08-09 16:28:52 +07:00
import { RequestWithUser } from "../middlewares/user";
import permission from "../interfaces/permission";
import { request } from "axios";
2024-08-22 14:23:50 +07:00
import { addLogSequence, setLogDataDiff } from "../interfaces/utils";
2024-04-23 12:35:03 +07:00
@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 2 () (USER)
2024-04-26 12:40:43 +07:00
*
2024-04-23 12:35:03 +07:00
* @summary 2 () (USER)
2024-04-26 12:40:43 +07:00
*
*
2024-04-23 12:35:03 +07:00
*/
@Post()
async CreateKpiUserCapacity(
2024-04-26 12:40:43 +07:00
@Body()
requestBody: {
2024-04-23 12:35:03 +07:00
kpiUserEvaluationId: string;
kpiCapacityId: string;
2024-06-19 17:34:31 +07:00
level: string | null;
2024-04-23 12:35:03 +07:00
weight: number;
},
2024-08-09 16:28:52 +07:00
@Request() request: RequestWithUser,
2024-04-26 12:40:43 +07:00
) {
2024-08-29 16:13:28 +07:00
// await new permission().PermissionCreate(request, "SYS_KPI_LIST");
2024-04-23 12:35:03 +07:00
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) {
2024-04-26 12:40:43 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
2024-04-23 12:35:03 +07:00
}
const chkRepleat = await this.kpiUserCapacityRepository.find({
where: {
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
2024-04-26 12:40:43 +07:00
kpiCapacityId: requestBody.kpiCapacityId,
},
});
if (chkRepleat.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากรายการสมรรถนะซ้ำ",
);
}
2024-08-22 14:23:50 +07:00
const before = null;
2024-04-23 12:35:03 +07:00
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;
2024-08-22 14:23:50 +07:00
await this.kpiUserCapacityRepository.save(kpiUserCapacity, { data: request });
setLogDataDiff(request, { before, after: kpiUserCapacity });
2024-04-23 12:35:03 +07:00
return new HttpSuccess(kpiUserCapacity.id);
}
/**
* API 2 () (USER)
2024-04-26 12:40:43 +07:00
*
2024-04-23 12:35:03 +07:00
* @summary 2 () (USER)
2024-04-26 12:40:43 +07:00
*
2024-04-23 12:35:03 +07:00
* @param {string} id Guid, *Id 2 () (USER)
*/
@Put("{id}")
async updateKpiUserCapacity(
@Path() id: string,
2024-04-26 12:40:43 +07:00
@Body()
requestBody: {
2024-04-23 12:35:03 +07:00
kpiUserEvaluationId: string;
kpiCapacityId: string;
level: string;
weight: number;
},
2024-08-09 16:28:52 +07:00
@Request() request: RequestWithUser,
2024-04-23 12:35:03 +07:00
) {
2024-08-29 16:13:28 +07:00
// await new permission().PermissionUpdate(request, "SYS_KPI_LIST");
2024-04-23 12:35:03 +07:00
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) {
2024-04-26 12:40:43 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายการสมรรถนะนี้");
2024-04-23 12:35:03 +07:00
}
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,
2024-04-26 12:40:43 +07:00
id: Not(id),
},
});
if (chkRepleat.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถแก้ไขข้อมูลได้เนื่องจากรายการสมรรถนะซ้ำ",
);
}
2024-08-22 14:23:50 +07:00
const before = structuredClone(kpiUserCapacity);
2024-04-23 12:35:03 +07:00
const _kpiUserCapacity = Object.assign(new KpiUserCapacity(), requestBody);
kpiUserCapacity.lastUpdateUserId = request.user.sub;
kpiUserCapacity.lastUpdateFullName = request.user.name;
this.kpiUserCapacityRepository.merge(kpiUserCapacity, _kpiUserCapacity);
2024-08-22 14:23:50 +07:00
await this.kpiUserCapacityRepository.save(kpiUserCapacity, { data: request });
setLogDataDiff(request, { before, after: kpiUserCapacity });
2024-04-23 12:35:03 +07:00
return new HttpSuccess(kpiUserCapacity.id);
}
/**
* API 2 () (USER)
*
* @summary 2 () (USER)
*
* @param {string} id Guid, *Id 2 () (USER)
*/
@Get("{id}")
2024-08-22 14:14:24 +07:00
async GetKpiUserCapacityById(@Request() request: RequestWithUser, @Path() id: string) {
2024-08-29 16:13:28 +07:00
// await new permission().PermissionGet(request, "SYS_KPI_LIST");
2024-04-23 12:35:03 +07:00
const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({
where: { id: id },
2024-04-26 12:40:43 +07:00
relations: ["kpiCapacity"],
});
2024-04-23 12:35:03 +07:00
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,
2024-04-26 12:40:43 +07:00
summary: kpiUserCapacity.summary,
};
2024-04-23 12:35:03 +07:00
return new HttpSuccess(mapData);
}
/**
* API 2 () (USER)
*
* @summary 2 () (USER)
*
*/
@Get()
async listKpiUserCapacity(
2024-08-22 14:14:24 +07:00
@Request() request: RequestWithUser,
2024-04-23 12:35:03 +07:00
@Query("id") id: string, //kpiUserEvaluationId
2024-04-26 12:40:43 +07:00
@Query("type") type: string,
2024-04-23 12:35:03 +07:00
) {
2024-08-29 16:13:28 +07:00
// await new permission().PermissionGet(request, "SYS_KPI_LIST");
2024-04-23 12:35:03 +07:00
const [kpiUserCapacity, total] = await AppDataSource.getRepository(KpiUserCapacity)
2024-04-26 12:40:43 +07:00
.createQueryBuilder("kpiUserCapacity")
.leftJoinAndSelect("kpiUserCapacity.kpiCapacity", "kpiCapacity")
.leftJoinAndSelect("kpiCapacity.kpiCapacityDetails", "kpiCapacityDetails")
2024-04-26 12:40:43 +07:00
.andWhere("kpiUserCapacity.kpiUserEvaluationId = :id", { id: id })
.andWhere(type ? "kpiCapacity.type LIKE :type" : "1=1", { type: type.toLocaleUpperCase() })
.orderBy("kpiUserCapacity.createdAt", "ASC")
.getManyAndCount();
2024-04-23 12:35:03 +07:00
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,
2024-04-23 12:35:03 +07:00
}));
return new HttpSuccess({ data: mapData, total });
}
/**
* API 2 () (USER)
2024-04-26 12:40:43 +07:00
*
2024-04-23 12:35:03 +07:00
* @summary 2 () (USER)
2024-04-26 12:40:43 +07:00
*
2024-04-23 12:35:03 +07:00
* @param {string} id Guid, *Id 2 () (USER)
*/
@Delete("{id}")
2024-08-09 16:28:52 +07:00
async deleteKpiUserCapacity(@Path() id: string, @Request() request: RequestWithUser) {
2024-08-29 16:13:28 +07:00
// await new permission().PermissionDelete(request, "SYS_KPI_LIST");
2024-04-23 12:35:03 +07:00
const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({
where: { id: id },
});
if (!kpiUserCapacity) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
2024-04-23 16:34:13 +07:00
"ไม่พบข้อมูลองค์ประกอบที่ 2 พฤติกรรมการปฎิบัติราชการ (สมรรถนะ)นี้",
2024-04-23 12:35:03 +07:00
);
}
await this.kpiUserCapacityRepository.remove(kpiUserCapacity);
return new HttpSuccess();
}
2024-04-23 16:34:13 +07:00
/**
* API () (USER)
2024-04-26 12:40:43 +07:00
*
2024-04-23 16:34:13 +07:00
* @summary () (USER)
2024-04-26 12:40:43 +07:00
*
*
2024-04-23 16:34:13 +07:00
*/
@Post("point")
async CreateKpiUserCapacityPoint(
@Body() requestBody: KpiUserCapacityDataPoint[],
2024-08-09 16:28:52 +07:00
@Request() request: RequestWithUser,
2024-04-26 12:40:43 +07:00
) {
2024-08-29 16:13:28 +07:00
// await new permission().PermissionCreate(request, "SYS_KPI_LIST");
2024-04-23 16:34:13 +07:00
for (const item of requestBody) {
const kpiUserCapacity = await this.kpiUserCapacityRepository.findOne({
where: { id: item.id },
});
if (!kpiUserCapacity) {
2024-04-26 12:40:43 +07:00
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลพฤติกรรมการปฎิบัติราชการ (สมรรถนะ): ${item.id}`,
);
2024-04-23 16:34:13 +07:00
}
2024-08-22 14:23:50 +07:00
const before = structuredClone(kpiUserCapacity);
2024-04-23 16:34:13 +07:00
this.kpiUserCapacityRepository.merge(kpiUserCapacity, item);
kpiUserCapacity.lastUpdateUserId = request.user.sub;
kpiUserCapacity.lastUpdateFullName = request.user.name;
2024-08-22 14:23:50 +07:00
await this.kpiUserCapacityRepository.save(kpiUserCapacity, { data: request });
setLogDataDiff(request, { before, after: kpiUserCapacity });
2024-04-23 16:34:13 +07:00
}
return new HttpSuccess();
}
2024-04-23 12:35:03 +07:00
}