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

394 lines
15 KiB
TypeScript
Raw Normal View History

2024-04-22 13:23:31 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
2024-04-26 10:18:00 +07:00
ArrayValidator,
2024-04-22 13:23:31 +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 { KpiPeriod } from "../entities/kpiPeriod";
2024-04-26 10:18:00 +07:00
import {
KpiUserEvaluation,
createKpiUserEvaluation,
updateKpiUserCheckEvaluation,
2024-04-26 10:18:00 +07:00
updateKpiUserEvaluation,
updateKpiUserPointEvaluation,
2024-04-26 10:18:00 +07:00
} from "../entities/kpiUserEvaluation";
import { Like, In, Brackets } from "typeorm";
2024-04-22 13:23:31 +07:00
import CallAPI from "../interfaces/call-api";
import {
KpiUserEvaluationReason,
updateKpiUserReasonEvaluation,
} from "../entities/kpiUserEvaluationReason";
2024-04-22 13:23:31 +07:00
@Route("api/v1/kpi/user/evaluation")
@Tags("kpiUserEvaluation")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserEvaluationController extends Controller {
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
private kpiUserEvalutionRepository = AppDataSource.getRepository(KpiUserEvaluation);
private kpiUserEvaluationReasonRepository = AppDataSource.getRepository(KpiUserEvaluationReason);
2024-04-22 13:23:31 +07:00
2024-04-26 10:18:00 +07:00
/**
* API
*
* @summary
*
*/
@Get("admin")
async listKpiAdminEvaluation(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("kpiPeriodId") kpiPeriodId?: string,
@Query("keyword") keyword?: string,
2024-04-26 10:18:00 +07:00
) {
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
.createQueryBuilder("kpiUserEvaluation")
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
2024-04-26 12:40:43 +07:00
kpiPeriodId: kpiPeriodId,
2024-04-26 10:18:00 +07:00
})
.andWhere(
new Brackets((qb) => {
qb.orWhere("kpiUserEvaluation.prefix LIKE :keyword", { keyword: `%${keyword}%` })
.orWhere("kpiUserEvaluation.firstName LIKE :keyword", { keyword: `%${keyword}%` })
.orWhere("kpiUserEvaluation.lastName LIKE :keyword", { keyword: `%${keyword}%` });
}),
)
2024-04-26 10:18:00 +07:00
.orderBy("kpiUserEvaluation.createdAt", "ASC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const mapData = kpiUserEvaluation.map((item) => ({
id: item.id,
profileId: item.profileId,
prefix: item.prefix,
firstname: item.firstName,
lastname: item.lastName,
kpiPeriodId: item.kpiPeriodId,
evaluationStatus: item.evaluationStatus,
evaluationResults: item.evaluationResults,
createdAt: item.createdAt,
2024-04-26 13:17:25 +07:00
evaluatorId: item.evaluatorId,
commanderId: item.commanderId,
commanderHighId: item.commanderHighId,
2024-04-26 10:18:00 +07:00
}));
return new HttpSuccess({ data: mapData, total });
}
2024-04-22 13:23:31 +07:00
/**
* API (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @summary (USER)
2024-04-26 10:18:00 +07:00
*
*
2024-04-22 13:23:31 +07:00
*/
@Post()
async CreateKpiUserEvaluation(
@Body() requestBody: createKpiUserEvaluation,
@Request() request: { user: Record<string, any> },
2024-04-26 10:18:00 +07:00
) {
2024-04-22 13:23:31 +07:00
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
2024-04-26 10:18:00 +07:00
2024-04-22 13:23:31 +07:00
const kpiUserEvaluation = Object.assign(new KpiUserEvaluation(), requestBody);
await new CallAPI()
.GetData(request, "org/profile/keycloak/position")
.then((x) => {
2024-04-26 10:18:00 +07:00
kpiUserEvaluation.profileId = x.profileId;
kpiUserEvaluation.prefix = x.prefix;
kpiUserEvaluation.firstName = x.firstName;
kpiUserEvaluation.lastName = x.lastName;
2024-04-22 13:23:31 +07:00
})
.catch((x) => {});
2024-05-08 14:03:13 +07:00
kpiUserEvaluation.evaluationStatus = "NEW";
2024-04-26 10:18:00 +07:00
kpiUserEvaluation.evaluationResults = "PENDING";
kpiUserEvaluation.createdUserId = request.user.sub;
kpiUserEvaluation.createdFullName = request.user.name;
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
2024-04-22 13:23:31 +07:00
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("check/{id}")
async updateKpiUserCheckEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserCheckEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
Object.assign(kpiUserEvaluation, requestBody);
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("point/{id}")
async updateKpiUserPointEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserPointEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
Object.assign(kpiUserEvaluation, requestBody);
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Put("{type}/{id}")
async updateKpiUserEvaluatorEvaluation(
@Path() id: string,
@Path() type: string,
@Body() requestBody: updateKpiUserReasonEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
const kpiUserEvaluationReason = Object.assign(new KpiUserEvaluationReason(), requestBody);
kpiUserEvaluationReason.type = type.trim().toUpperCase();
kpiUserEvaluationReason.kpiUserEvaluationId = id;
kpiUserEvaluationReason.createdUserId = request.user.sub;
kpiUserEvaluationReason.createdFullName = request.user.name;
kpiUserEvaluationReason.lastUpdateUserId = request.user.sub;
kpiUserEvaluationReason.lastUpdateFullName = request.user.name;
await this.kpiUserEvaluationReasonRepository.save(kpiUserEvaluationReason);
return new HttpSuccess(kpiUserEvaluation.id);
}
/**
* API list (USER)
*
* @summary list (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Get("{type}/{id}")
async listKpiUserCommanderEvaluation(@Path() id: string, @Path() type: string) {
const kpiUserEvaluationReason = await this.kpiUserEvaluationReasonRepository.find({
where: { kpiUserEvaluationId: id, type: type.trim().toUpperCase() },
2024-04-29 13:14:40 +07:00
order: { createdAt: "ASC" }
});
return new HttpSuccess(kpiUserEvaluationReason);
}
2024-04-22 13:23:31 +07:00
/**
* API (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @summary (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @param {string} id Guid, *Id (USER)
*/
@Put("{id}")
async updateKpiUserEvaluation(
@Path() id: string,
@Body() requestBody: updateKpiUserEvaluation,
@Request() request: { user: Record<string, any> },
) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
where: { id: id },
});
if (!kpiUserEvaluation) {
2024-04-22 13:23:31 +07:00
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
const kpiPeriod = await this.kpiPeriodRepository.findOne({
where: { id: requestBody.kpiPeriodId },
});
if (!kpiPeriod) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
);
}
kpiUserEvaluation.lastUpdateUserId = request.user.sub;
kpiUserEvaluation.lastUpdateFullName = request.user.name;
this.kpiUserEvalutionRepository.merge(kpiUserEvaluation, requestBody);
await this.kpiUserEvalutionRepository.save(kpiUserEvaluation);
return new HttpSuccess(kpiUserEvaluation.id);
2024-04-22 13:23:31 +07:00
}
/**
* API (USER)
*
* @summary (USER)
*
* @param {string} id Guid, *Id (USER)
*/
@Get("{id}")
async GetKpiUserEvaluationById(@Path() id: string) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
2024-04-22 13:23:31 +07:00
where: { id: id },
2024-04-26 10:18:00 +07:00
select: [
"id",
"profileId",
"prefix",
"firstName",
"lastName",
"kpiPeriodId",
"evaluationStatus",
"evaluationResults",
"createdAt",
2024-04-26 13:17:25 +07:00
"evaluatorId",
"commanderId",
"commanderHighId",
"plannedPoint",
"rolePoint",
"specialPoint",
"capacityPoint",
2024-04-26 10:18:00 +07:00
],
});
if (!kpiUserEvaluation) {
2024-04-22 13:23:31 +07:00
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
return new HttpSuccess(kpiUserEvaluation);
2024-04-22 13:23:31 +07:00
}
/**
* API (USER)
*
* @summary (USER)
*
*/
@Get()
async listKpiUserEvaluation(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
2024-04-22 15:24:44 +07:00
@Query("kpiPeriodId") kpiPeriodId?: string,
// @Query("keyword") keyword?: string,
2024-04-22 13:23:31 +07:00
) {
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
.createQueryBuilder("kpiUserEvaluation")
2024-04-26 10:18:00 +07:00
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
2024-04-26 12:40:43 +07:00
kpiPeriodId: kpiPeriodId,
2024-04-26 10:18:00 +07:00
})
2024-04-22 13:23:31 +07:00
.orderBy("kpiUserEvaluation.createdAt", "ASC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const mapData = kpiUserEvaluation.map((item) => ({
id: item.id,
profileId: item.profileId,
prefix: item.prefix,
firstname: item.firstName,
lastname: item.lastName,
kpiPeriodId: item.kpiPeriodId,
2024-04-22 15:24:44 +07:00
evaluationStatus: item.evaluationStatus,
evaluationResults: item.evaluationResults,
2024-04-26 13:17:25 +07:00
evaluatorId: item.evaluatorId,
commanderId: item.commanderId,
commanderHighId: item.commanderHighId,
2024-04-22 15:24:44 +07:00
createdAt: item.createdAt,
plannedPoint: item.plannedPoint,
rolePoint: item.rolePoint,
specialPoint: item.specialPoint,
capacityPoint: item.capacityPoint,
2024-04-22 13:23:31 +07:00
}));
return new HttpSuccess({ data: mapData, total });
}
/**
* API (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @summary (USER)
2024-04-26 10:18:00 +07:00
*
2024-04-22 13:23:31 +07:00
* @param {string} id Guid, *Id (USER)
*/
@Delete("{id}")
async deleteKpiUserEvaluation(@Path() id: string) {
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
2024-04-22 13:23:31 +07:00
where: { id: id },
});
if (!kpiUserEvaluation) {
2024-04-22 13:23:31 +07:00
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
);
}
await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation);
2024-04-22 13:23:31 +07:00
return new HttpSuccess();
}
}