285 lines
11 KiB
TypeScript
285 lines
11 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
Example,
|
|
SuccessResponse,
|
|
Response,
|
|
Query,
|
|
ArrayValidator,
|
|
} 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";
|
|
import {
|
|
KpiUserEvaluation,
|
|
createKpiUserEvaluation,
|
|
updateKpiUserCheckEvaluation,
|
|
updateKpiUserEvaluation,
|
|
} from "../entities/kpiUserEvaluation";
|
|
import { Like, In } from "typeorm";
|
|
import CallAPI from "../interfaces/call-api";
|
|
|
|
@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);
|
|
|
|
/**
|
|
* API
|
|
*
|
|
* @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคลทั้งหมด
|
|
*
|
|
*/
|
|
@Get("admin")
|
|
async listKpiAdminEvaluation(
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("kpiPeriodId") kpiPeriodId?: string,
|
|
// @Query("keyword") keyword?: string,
|
|
) {
|
|
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
|
|
.createQueryBuilder("kpiUserEvaluation")
|
|
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
|
|
kpiPeriodId: kpiPeriodId,
|
|
})
|
|
.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,
|
|
}));
|
|
return new HttpSuccess({ data: mapData, total });
|
|
}
|
|
|
|
/**
|
|
* API สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
* @summary สร้างรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
*
|
|
*/
|
|
@Post()
|
|
async CreateKpiUserEvaluation(
|
|
@Body() requestBody: createKpiUserEvaluation,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: requestBody.kpiPeriodId },
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
|
|
const kpiUserEvaluation = Object.assign(new KpiUserEvaluation(), requestBody);
|
|
await new CallAPI()
|
|
.GetData(request, "org/profile/keycloak/position")
|
|
.then((x) => {
|
|
kpiUserEvaluation.profileId = x.profileId;
|
|
kpiUserEvaluation.prefix = x.prefix;
|
|
kpiUserEvaluation.firstName = x.firstName;
|
|
kpiUserEvaluation.lastName = x.lastName;
|
|
})
|
|
.catch((x) => {});
|
|
kpiUserEvaluation.evaluationStatus = "PENDING";
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 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("{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) {
|
|
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.evaluationStatus = requestBody.evaluationStatus
|
|
// kpiUserEvaluation.evaluationResults = requestBody.evaluationResults
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
* @summary รายละเอียดรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
* @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*/
|
|
@Get("{id}")
|
|
async GetKpiUserEvaluationById(@Path() id: string) {
|
|
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
|
|
where: { id: id },
|
|
select: [
|
|
"id",
|
|
"profileId",
|
|
"prefix",
|
|
"firstName",
|
|
"lastName",
|
|
"kpiPeriodId",
|
|
"evaluationStatus",
|
|
"evaluationResults",
|
|
"createdAt",
|
|
],
|
|
});
|
|
if (!kpiUserEvaluation) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
|
|
);
|
|
}
|
|
return new HttpSuccess(kpiUserEvaluation);
|
|
}
|
|
|
|
/**
|
|
* API รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
* @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
*/
|
|
@Get()
|
|
async listKpiUserEvaluation(
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("kpiPeriodId") kpiPeriodId?: string,
|
|
// @Query("keyword") keyword?: string,
|
|
) {
|
|
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
|
|
.createQueryBuilder("kpiUserEvaluation")
|
|
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
|
|
kpiPeriodId: kpiPeriodId,
|
|
})
|
|
.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,
|
|
}));
|
|
return new HttpSuccess({ data: mapData, total });
|
|
}
|
|
|
|
/**
|
|
* API ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
* @summary ลบรายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
* @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiUserEvaluation(@Path() id: string) {
|
|
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiUserEvaluation) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
|
|
);
|
|
}
|
|
await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation);
|
|
return new HttpSuccess();
|
|
}
|
|
}
|