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

264 lines
9.5 KiB
TypeScript
Raw Normal View History

2024-05-08 15:18:17 +07:00
import {
2024-05-09 11:06:41 +07:00
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 HttpStatusCode from "../interfaces/http-status";
import {
KpiUserDevelopment,
CreateKpiUserDevelopment,
UpdateKpiUserDevelopment,
KpiUserDevelopmentDataPoint,
} from "../entities/kpiUserDevelopment";
import HttpError from "../interfaces/http-error";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
import { Not, Like } from "typeorm";
@Route("api/v1/kpi/user/achievement/development")
@Tags("KpiUserDevelopment")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserDevelopmentController extends Controller {
private kpiUserDevelopmentRepository = AppDataSource.getRepository(KpiUserDevelopment);
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
/**
* API
*
* @summary - #
*
*/
@Post()
async createKpiUserDevelopment(
@Body()
requestBody: CreateKpiUserDevelopment,
@Request() request: { user: Record<string, any> },
) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkName = await this.kpiUserDevelopmentRepository.findOne({
where: { name: requestBody.name},
});
if (chkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "มีชื่อนี้ในระบบแล้ว");
}
const kpiUserDevelopment = Object.assign(new KpiUserDevelopment(), requestBody);
if (!kpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-05-08 15:18:17 +07:00
// const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({
// where: {
// kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
// },
// });
// if (
// (chk_indicator && chk_indicator.including == requestBody.including) ||
// (chk_indicator && chk_indicator.includingName == requestBody.includingName)
// ) {
// throw new HttpError(
// HttpStatusCode.CONFLICT,
// "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
// );
// }
2024-05-09 11:06:41 +07:00
kpiUserDevelopment.createdUserId = request.user.sub;
kpiUserDevelopment.createdFullName = request.user.name;
kpiUserDevelopment.lastUpdateUserId = request.user.sub;
kpiUserDevelopment.lastUpdateFullName = request.user.name;
await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment);
return new HttpSuccess(kpiUserDevelopment.id);
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Put("{id}")
async editKpiUserDevelopment(
@Path() id: string,
@Body() requestBody: UpdateKpiUserDevelopment,
@Request() request: { user: Record<string, any> },
) {
const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({ where: { id } });
if (!kpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
}
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkName = await this.kpiUserDevelopmentRepository.find({
where: {
id: Not(id),
name: requestBody.name
},
});
if (chkName && chkName.length > 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "มีชื่อนี้ในระบบแล้ว");
2024-05-08 15:18:17 +07:00
}
// const chk_indicator = await this.kpiUserDevelopmentRepository.findOne({
// where: {
// id: Not(id),
// kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
// },
// });
// if (
// (chk_indicator && chk_indicator.including == requestBody.including) ||
// (chk_indicator && chk_indicator.includingName == requestBody.includingName)
// ) {
// throw new HttpError(
// HttpStatusCode.CONFLICT,
// "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
// );
// }
2024-05-09 11:06:41 +07:00
kpiUserDevelopment.lastUpdateUserId = request.user.sub;
kpiUserDevelopment.lastUpdateFullName = request.user.name;
Object.assign(kpiUserDevelopment, requestBody);
await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment);
return new HttpSuccess(kpiUserDevelopment.id);
}
/**
* API
*
* @summary - #
*
*/
@Delete("{id}")
async deleteKpiUserDevelopment(@Path() id: string) {
const delKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
where: { id },
});
if (!delKpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
2024-05-08 17:54:53 +07:00
}
2024-05-09 11:06:41 +07:00
await this.kpiUserDevelopmentRepository.remove(delKpiUserDevelopment);
return new HttpSuccess();
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Get("{id}")
async GetKpiUserDevelopmentDetail(@Path() id: string) {
const getKpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
relations: ["kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลพัฒนาตนเองนี้");
2024-05-08 15:18:17 +07:00
}
2024-05-09 11:06:41 +07:00
const mapKpiUserDevelopment = {
id: getKpiUserDevelopment.id,
evaluationId: getKpiUserDevelopment.kpiUserEvaluation.id,
target: getKpiUserDevelopment.target,
summary: getKpiUserDevelopment.summary,
name: getKpiUserDevelopment.name,
achievement10: getKpiUserDevelopment.achievement10,
achievement5: getKpiUserDevelopment.achievement5,
achievement0: getKpiUserDevelopment.achievement0,
isDevelopment70: getKpiUserDevelopment.isDevelopment70,
isDevelopment20: getKpiUserDevelopment.isDevelopment20,
isDevelopment10: getKpiUserDevelopment.isDevelopment10,
};
return new HttpSuccess(mapKpiUserDevelopment);
}
/**
* API
*
* @summary - #
*
*/
@Get()
async GetKpiUserDevelopment(@Query("id") id: string) {
const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.find({
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserDevelopment = kpiUserDevelopment.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
target: item.target,
summary: item.summary,
name: item.name,
achievement10: item.achievement10,
achievement5: item.achievement5,
achievement0: item.achievement0,
isDevelopment70: item.isDevelopment70,
isDevelopment20: item.isDevelopment20,
isDevelopment10: item.isDevelopment10,
}));
return new HttpSuccess(mapKpiUserDevelopment);
2024-05-08 15:18:17 +07:00
}
2024-05-09 11:06:41 +07:00
// /**
// * API กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี
// *
// * @summary กรอกระดับคะแนนงานตามแผนปฏิบัติราชการประจำปี
// *
// *
// */
// @Post("point")
// async CreateKpiUserDevelopmentPoint(
// @Body() requestBody: KpiUserDevelopmentDataPoint[],
// @Request() request: { user: Record<string, any> },
// ) {
// for (const item of requestBody) {
// const kpiUserDevelopment = await this.kpiUserDevelopmentRepository.findOne({
// where: { id: item.id },
// });
// if (!kpiUserDevelopment) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลพัฒนาตนเองนี้: ${item.id}`);
// }
// this.kpiUserDevelopmentRepository.merge(kpiUserDevelopment, item);
// kpiUserDevelopment.lastUpdateUserId = request.user.sub;
// kpiUserDevelopment.lastUpdateFullName = request.user.name;
// await this.kpiUserDevelopmentRepository.save(kpiUserDevelopment);
// }
// return new HttpSuccess();
// }
}