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

106 lines
3 KiB
TypeScript
Raw Normal View History

2024-04-18 15:42:30 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import { Like, Not } from "typeorm";
import HttpStatusCode from "../interfaces/http-status";
import { KpiEvaluation, updateKpiEvaluation } from "../entities/kpiEvaluation";
@Route("api/v1/kpi/evaluation")
@Tags("kpiEvaluation")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class kpiEvaluationController extends Controller {
private kpiEvaluationRepository = AppDataSource.getRepository(KpiEvaluation);
/**
* API
* @param id
*/
@Put()
async updateKpiEvaluations(
@Body() requestBody: updateKpiEvaluation[],
@Request() request: { user: Record<string, any> },
) {
const updatedIds: string[] = [];
for (const item of requestBody) {
const kpiEvaluation = await this.kpiEvaluationRepository.findOne({
where: { id: item.id },
});
if (!kpiEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลเกณฑ์การประเมินนี้: ${item.id}`);
}
2024-04-18 15:42:30 +07:00
this.kpiEvaluationRepository.merge(kpiEvaluation, item);
kpiEvaluation.lastUpdateUserId = request.user.sub;
kpiEvaluation.lastUpdateFullName = request.user.name;
await this.kpiEvaluationRepository.save(kpiEvaluation);
updatedIds.push(item.id);
}
return new HttpSuccess();
}
2024-04-18 15:42:30 +07:00
/**
* API list
2024-04-18 15:42:30 +07:00
* @param page
* @param pageSize
*/
@Get()
async listKpiEvaluation(
2024-04-18 17:49:34 +07:00
// @Query("page") page: number = 1,
// @Query("pageSize") pageSize: number = 10,
// @Query("keyword") keyword?: string,
2024-04-18 15:42:30 +07:00
) {
let whereClause: any = {};
2024-04-18 17:49:34 +07:00
// if (keyword !== undefined && keyword !== "") {
// whereClause = {
// where: [{ description: Like(`%${keyword}%`) }],
// };
// whereClause.where.push({ level: Like(`%${keyword}%`) });
2024-04-18 15:42:30 +07:00
2024-04-18 17:49:34 +07:00
// }
2024-04-18 17:49:34 +07:00
// const [kpiEvaluation, total] = await this.kpiEvaluationRepository.findAndCount({
// ...whereClause,
// ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
// order:{
// level: "DESC"}
// });
const kpiEvaluation = await this.kpiEvaluationRepository.find({
order:{
level: "DESC"}
2024-04-18 15:42:30 +07:00
});
2024-04-18 17:24:28 +07:00
const formatted = kpiEvaluation.map((item) => ({
id: item.id,
level: item.level,
description: item.description
}));
2024-04-18 17:49:34 +07:00
return new HttpSuccess(formatted);
2024-04-18 15:42:30 +07:00
}
}