457 lines
18 KiB
TypeScript
457 lines
18 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,
|
|
updateKpiUserPointEvaluation,
|
|
updateKpiUserStatusEvaluation,
|
|
updateKpiUserReqEditEvaluation,
|
|
} from "../entities/kpiUserEvaluation";
|
|
import { Like, In, Brackets } 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(
|
|
@Request() request: { user: Record<string, any> },
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("kpiPeriodId") kpiPeriodId?: string,
|
|
@Query("keyword") keyword?: string,
|
|
@Query("status") status?: string,
|
|
@Query("results") results?: string,
|
|
@Query("reqedit") reqedit?: string,
|
|
) {
|
|
let profileId: any = null;
|
|
await new CallAPI()
|
|
.GetData(request, "org/profile/keycloak/position")
|
|
.then((x) => {
|
|
profileId = x.profileId;
|
|
})
|
|
.catch((x) => {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ");
|
|
});
|
|
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
|
|
.createQueryBuilder("kpiUserEvaluation")
|
|
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
|
|
kpiPeriodId: kpiPeriodId,
|
|
})
|
|
.andWhere(status != null && status != undefined ? "evaluationStatus LIKE :status" : "1=1", {
|
|
status: status,
|
|
})
|
|
.andWhere(
|
|
results != null && results != undefined ? "evaluationResults LIKE :results" : "1=1",
|
|
{
|
|
results: results,
|
|
},
|
|
)
|
|
.andWhere(
|
|
reqedit != null && reqedit != undefined ? "evaluationReqEdit LIKE :reqedit" : "1=1",
|
|
{
|
|
reqedit: reqedit,
|
|
},
|
|
)
|
|
.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.orWhere("kpiUserEvaluation.evaluatorId LIKE :profileId", {
|
|
profileId: `%${profileId}%`,
|
|
})
|
|
.orWhere("kpiUserEvaluation.commanderId LIKE :profileId", {
|
|
profileId: `%${profileId}%`,
|
|
})
|
|
.orWhere("kpiUserEvaluation.commanderHighId LIKE :profileId", {
|
|
profileId: `%${profileId}%`,
|
|
});
|
|
}),
|
|
)
|
|
.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}%` });
|
|
}),
|
|
)
|
|
.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,
|
|
evaluatorId: item.evaluatorId,
|
|
commanderId: item.commanderId,
|
|
commanderHighId: item.commanderHighId,
|
|
}));
|
|
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 = "NEW";
|
|
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("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("{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.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)
|
|
*/
|
|
@Put("edit/{id}")
|
|
async updateKpiUserReqEditEvaluation(
|
|
@Path() id: string,
|
|
@Body() requestBody: updateKpiUserReqEditEvaluation,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiUserEvaluation) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
|
|
);
|
|
}
|
|
kpiUserEvaluation.evaluationReqEdit = requestBody.status.trim().toUpperCase();
|
|
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("status/{id}")
|
|
async updateKpiUserStatusEvaluation(
|
|
@Path() id: string,
|
|
@Body() requestBody: updateKpiUserStatusEvaluation,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiUserEvaluation) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
|
|
);
|
|
}
|
|
kpiUserEvaluation.evaluationStatus = requestBody.status.trim().toUpperCase();
|
|
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)
|
|
*/
|
|
@Get("{id}")
|
|
async GetKpiUserEvaluationById(@Path() id: string) {
|
|
const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({
|
|
relations: ["kpiPeriod"],
|
|
where: { id: id },
|
|
});
|
|
if (!kpiUserEvaluation) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้",
|
|
);
|
|
}
|
|
const mapData = {
|
|
id: kpiUserEvaluation.id,
|
|
profileId: kpiUserEvaluation.profileId,
|
|
prefix: kpiUserEvaluation.prefix,
|
|
firstName: kpiUserEvaluation.firstName,
|
|
lastName: kpiUserEvaluation.lastName,
|
|
evaluationStatus: kpiUserEvaluation.evaluationStatus,
|
|
evaluationResults: kpiUserEvaluation.evaluationResults,
|
|
evaluationReqEdit: kpiUserEvaluation.evaluationReqEdit,
|
|
createdAt: kpiUserEvaluation.createdAt,
|
|
evaluatorId: kpiUserEvaluation.evaluatorId,
|
|
commanderId: kpiUserEvaluation.commanderId,
|
|
commanderHighId: kpiUserEvaluation.commanderHighId,
|
|
plannedPoint: kpiUserEvaluation.plannedPoint,
|
|
rolePoint: kpiUserEvaluation.rolePoint,
|
|
specialPoint: kpiUserEvaluation.specialPoint,
|
|
capacityPoint: kpiUserEvaluation.capacityPoint,
|
|
kpiPeriodId: kpiUserEvaluation.kpiPeriodId,
|
|
year: kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.year,
|
|
durationKPI:
|
|
kpiUserEvaluation.kpiPeriod == null ? null : kpiUserEvaluation.kpiPeriod.durationKPI,
|
|
};
|
|
return new HttpSuccess(mapData);
|
|
}
|
|
|
|
/**
|
|
* API รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
* @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER)
|
|
*
|
|
*/
|
|
@Get()
|
|
async listKpiUserEvaluation(
|
|
@Request() request: { user: Record<string, any> },
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("kpiPeriodId") kpiPeriodId?: string,
|
|
@Query("keyword") keyword?: string,
|
|
@Query("status") status?: string,
|
|
) {
|
|
const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation)
|
|
.createQueryBuilder("kpiUserEvaluation")
|
|
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
|
|
.andWhere(kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", {
|
|
kpiPeriodId: kpiPeriodId,
|
|
})
|
|
.andWhere({ createdUserId: request.user.sub })
|
|
.andWhere(
|
|
status == null || status == undefined ? "1=1" : "evaluationStatus LIKE :evaluationStatus",
|
|
{
|
|
evaluationStatus: status == undefined ? "" : status.trim().toUpperCase(),
|
|
},
|
|
)
|
|
.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,
|
|
evaluatorId: item.evaluatorId,
|
|
commanderId: item.commanderId,
|
|
commanderHighId: item.commanderHighId,
|
|
createdAt: item.createdAt,
|
|
plannedPoint: item.plannedPoint,
|
|
rolePoint: item.rolePoint,
|
|
specialPoint: item.specialPoint,
|
|
capacityPoint: item.capacityPoint,
|
|
year: item.kpiPeriod ? item.kpiPeriod.year : null,
|
|
durationKPI: item.kpiPeriod ? item.kpiPeriod.durationKPI : null,
|
|
}));
|
|
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();
|
|
}
|
|
}
|