hrms-api-probation/src/controllers/EvaluateResultController.ts

556 lines
20 KiB
TypeScript
Raw Normal View History

2025-01-11 13:29:21 +07:00
import {
Controller,
Route,
Security,
Tags,
Request,
SuccessResponse,
Response,
Get,
Post,
Body,
Query,
Put,
} from "tsoa";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { RequestWithUser } from "../middlewares/user";
2025-01-30 15:26:58 +07:00
import { findEndDate, setLogDataDiff } from "../interfaces/utils";
2025-01-11 13:29:21 +07:00
import { AppDataSource } from "../database/data-source";
import { AssignDirector } from "../entities/AssignDirector";
import HttpError from "../interfaces/http-error";
import { Assign } from "../entities/Assign";
import { Personal } from "../entities/Personal";
import CallAPI from "../interfaces/call-api";
import { EvaluateChairman } from "../entities/EvaluateChairman";
2025-01-23 10:23:05 +07:00
import { CreateEvaluateResult, EvaluateResult } from "../entities/EvaluateResult";
2025-01-11 13:29:21 +07:00
import permission from "../interfaces/permission";
2024-09-05 13:59:43 +07:00
2024-09-05 16:56:22 +07:00
@Route("api/v1/probation/evaluate-result")
2024-09-05 13:59:43 +07:00
@Tags("แบบรายงานการประเมินฯ")
@Security("bearerAuth")
2025-01-11 13:29:21 +07:00
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
2025-01-23 10:23:05 +07:00
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
2025-01-11 13:29:21 +07:00
)
2024-09-05 13:59:43 +07:00
export class EvaluateResultController extends Controller {
2025-01-23 10:23:05 +07:00
private assignDirectorRepository = AppDataSource.getRepository(AssignDirector);
2025-01-11 13:29:21 +07:00
private assignRepository = AppDataSource.getRepository(Assign);
2025-01-23 10:23:05 +07:00
private evaluateChairmanRepository = AppDataSource.getRepository(EvaluateChairman);
2025-01-11 13:29:21 +07:00
private personalRepository = AppDataSource.getRepository(Personal);
2025-01-23 10:23:05 +07:00
private evaluateResultRepository = AppDataSource.getRepository(EvaluateResult);
2025-01-11 13:29:21 +07:00
/**
* API
*
* @summary
*
*/
@Get("create")
2025-01-23 10:23:05 +07:00
async CreateEvaluate(@Query() assign_id: string, @Request() request: RequestWithUser) {
let _workflow = await new permission().Workflow(request, assign_id, "SYS_PROBATION");
if (_workflow == false) await new permission().PermissionGet(request, "SYS_PROBATION");
2025-01-11 13:29:21 +07:00
const assign = await this.assignRepository.findOne({
relations: ["profile"],
where: { id: assign_id },
});
if (!assign) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
}
const profile = await (assign.profile
? {
...assign.profile,
id: assign.profile.personal_id,
name:
2025-01-23 10:23:05 +07:00
assign.profile.prefixName + assign.profile.firstName + " " + assign.profile.lastName,
2025-01-11 13:29:21 +07:00
Oc: assign.profile.organization,
}
: null);
const directorData = await this.assignDirectorRepository.find({
where: { assign_id },
order: { ordering: "ASC" },
});
if (!directorData) {
2025-01-23 10:23:05 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล ผู้บังคับบัญชาและประธาน");
2025-01-11 13:29:21 +07:00
}
let mentors = [];
const mentorList = await directorData.filter((x) => x.role == "mentor");
if (mentorList.length > 0) {
for (let index = 0; index < mentorList.length; index++) {
const e = await mentorList[index];
mentors.push({
personal_id: e.personal_id,
dated: e.dated,
name: e.fullname,
2025-01-23 10:23:05 +07:00
label: e.fullname + " " + (e.position ? `(${e.position}${e.posLevel})` : ""),
2025-01-11 13:29:21 +07:00
position: e.position,
posType: e.posType,
posLevel: e.posLevel,
});
}
}
2025-01-23 10:23:05 +07:00
const commanderData = await (directorData.find((x) => x.role == "commander") ?? null);
2025-01-11 13:29:21 +07:00
const commander =
commanderData != null
? {
personal_id: commanderData.personal_id,
dated: commanderData.dated,
name: commanderData.fullname,
label:
commanderData.fullname +
" " +
(commanderData.position
? `(${commanderData.position}${commanderData.posLevel})`
2025-01-11 13:29:21 +07:00
: ""),
position: commanderData.position,
posType: commanderData.posType,
posLevel: commanderData.posLevel,
}
: null;
2025-01-23 10:23:05 +07:00
const chairmanData = await (directorData.find((x) => x.role == "chairman") ?? null);
2025-01-11 13:29:21 +07:00
const chairman =
chairmanData != null
? {
personal_id: chairmanData.personal_id,
dated: chairmanData.dated,
name: chairmanData.fullname,
label:
chairmanData.fullname +
" " +
2025-01-23 10:23:05 +07:00
(chairmanData.position ? `(${chairmanData.position}${chairmanData.posLevel})` : ""),
2025-01-11 13:29:21 +07:00
position: chairmanData.position,
posType: chairmanData.posType,
posLevel: chairmanData.posLevel,
}
: null;
const resultData = await this.evaluateChairmanRepository.find({
2025-01-11 13:29:21 +07:00
select: [
"develop_orientation_score",
"develop_self_learning_score",
"develop_training_seminar_score",
"evaluate_result",
2025-01-30 14:10:35 +07:00
"no",
2025-01-11 13:29:21 +07:00
],
where: {
assign_id,
},
});
if (!resultData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมินผล");
}
2025-01-30 14:10:35 +07:00
const result = await Promise.all(
resultData.map(async (e) => {
const check = await this.evaluateResultRepository.count({ where: { assign_id, no: e.no } });
const develop_complete =
e.develop_orientation_score > 0 &&
e.develop_self_learning_score > 0 &&
e.develop_training_seminar_score
? 1
: 2;
const evaluate_result = e.evaluate_result === 1 ? 1 : 2;
2025-01-30 15:26:58 +07:00
let startDate = assign.date_start;
if (e.no === "2") {
const resultNo1 = await this.evaluateResultRepository.findOne({
select: ["date_finish"],
where: { assign_id, no: "1" },
});
startDate = resultNo1?.date_finish ?? assign.date_start;
}
let endDate: any = assign.date_finish;
if (e.no === "1") {
endDate = findEndDate(3, assign.date_start) ?? assign.date_finish;
}
2025-01-30 14:10:35 +07:00
return {
develop_complete,
evaluate_result,
evaluate_no: Number(e.no),
isResult: check > 0 ? true : false,
2025-01-30 15:26:58 +07:00
start_date: startDate,
end_date: endDate,
2025-01-30 14:10:35 +07:00
};
}),
);
// const develop_complete = await (resultData.develop_orientation_score > 0 &&
// resultData.develop_self_learning_score > 0 &&
// resultData.develop_training_seminar_score > 0
// ? 1
// : 2);
// const evaluate_result = await (resultData.evaluate_result == 1 ? 1 : 2);
// const result = await {
// develop_complete: develop_complete,
// evaluate_result: evaluate_result,
// };
2025-01-11 13:29:21 +07:00
return new HttpSuccess({
person: profile,
assign,
result,
mentors,
commander,
chairman,
});
}
/**
* API
*
* @summary
*
*/
@Get("")
async GetEvaluate(
@Request() request: RequestWithUser,
@Query() assign_id: string,
2025-01-23 10:23:05 +07:00
@Query() evaluate_no?: string,
2025-01-11 13:29:21 +07:00
) {
await new permission().PermissionGet(request, "SYS_PROBATION");
// ต้องปรับเป็น id ของคนที่ access เข้ามา
const director = await this.assignDirectorRepository.findOne({
select: ["personal_id"],
where: {
assign_id,
role: "chairman",
},
});
if (!director) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
}
const director_id = director.personal_id;
2025-01-30 11:21:33 +07:00
const evaluate = await this.evaluateResultRepository.findOne({
where: {
director_id,
assign_id,
no: evaluate_no ?? "1",
},
});
2025-01-11 13:29:21 +07:00
if (!evaluate) {
return new HttpSuccess(null);
}
const assign = await this.assignRepository.findOne({
where: { id: assign_id },
});
if (!assign) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
}
const experimenteeData = await this.personalRepository.findOne({
select: [
"personal_id",
"prefixName",
"firstName",
"lastName",
"positionName",
"positionLevelName",
"organization",
2025-01-13 09:38:48 +07:00
"orgRootName",
2025-01-11 13:29:21 +07:00
],
where: { personal_id: assign.personal_id },
});
if (!experimenteeData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคล");
}
2025-01-13 09:24:25 +07:00
const splitOc = await experimenteeData.organization.split("/");
const splitOcAmount = await splitOc.length;
2025-01-11 13:29:21 +07:00
const experimentee = await {
...experimenteeData,
name:
2025-01-23 10:23:05 +07:00
experimenteeData.prefixName + experimenteeData.firstName + " " + experimenteeData.lastName,
PositionLevel: experimenteeData.positionName + experimenteeData.positionLevelName,
2025-01-13 09:24:25 +07:00
Department: splitOcAmount > 2 ? splitOc[splitOcAmount - 3] : "-",
2025-01-23 10:23:05 +07:00
OrganizationOrganization: splitOcAmount > 1 ? splitOc[splitOcAmount - 2] : "-",
2025-01-13 09:24:25 +07:00
Oc: experimenteeData.orgRootName,
2025-01-11 13:29:21 +07:00
};
const directorData = await this.assignDirectorRepository.find({
where: { assign_id },
order: { ordering: "ASC" },
});
if (!directorData) {
2025-01-23 10:23:05 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล ผู้บังคับบัญชาและประธาน");
2025-01-11 13:29:21 +07:00
}
let mentors = [];
const mentorList = await directorData.filter((x) => x.role == "mentor");
if (mentorList.length > 0) {
for (let index = 0; index < mentorList.length; index++) {
const e = await mentorList[index];
mentors.push({
personal_id: e.personal_id,
dated: e.dated,
name: e.fullname,
2025-01-23 10:23:05 +07:00
label: e.fullname + " " + (e.position ? `(${e.position}${e.posLevel})` : ""),
2025-01-11 13:29:21 +07:00
position: e.position + e.posLevel,
posType: e.posType,
posLevel: e.posLevel,
});
}
}
2025-01-30 11:21:33 +07:00
// const commanderData = await (directorData.find((x) => x.role == "commander") ?? null);
// const commander =
// commanderData != null
// ? {
// personal_id: commanderData.personal_id,
// dated: commanderData.dated,
// name: commanderData.fullname,
// label:
// commanderData.fullname +
// " " +
// (commanderData.position
// ? `(${commanderData.position}${commanderData.posLevel})`
// : ""),
// position: commanderData.position + commanderData.posLevel,
// posType: commanderData.posType,
// posLevel: commanderData.posLevel,
// }
// : null;
const commander = {
dated: evaluate.authority_dated,
name: evaluate.authority_name,
position: evaluate.authority_pos + evaluate.authority_level,
posType: evaluate.authority_type,
posLevel: evaluate.authority_level,
};
2025-01-23 10:23:05 +07:00
const chairmanData = await (directorData.find((x) => x.role == "chairman") ?? null);
2025-01-11 13:29:21 +07:00
const chairman =
chairmanData != null
? {
personal_id: chairmanData.personal_id,
dated: chairmanData.dated,
name: chairmanData.fullname,
label:
chairmanData.fullname +
" " +
2025-01-23 10:23:05 +07:00
(chairmanData.position ? `(${chairmanData.position}${chairmanData.posLevel})` : ""),
2025-01-11 13:29:21 +07:00
position: chairmanData.position + chairmanData.posLevel,
posType: chairmanData.posType,
posLevel: chairmanData.posLevel,
}
: null;
return new HttpSuccess({
commander,
chairman,
mentors,
experimentee,
assign,
evaluate: evaluate ? evaluate : null,
2025-01-11 13:29:21 +07:00
});
}
/**
* API
*
* @summary
*
*/
@Post("")
async PostData(
@Query() assign_id: string,
@Body() requestBody: CreateEvaluateResult,
2025-01-23 10:23:05 +07:00
@Request() request: RequestWithUser,
2025-01-11 13:29:21 +07:00
) {
await new permission().PermissionUpdate(request, "SYS_PROBATION");
const director = await this.assignDirectorRepository.findOne({
select: ["personal_id"],
where: {
assign_id,
role: "chairman",
},
});
if (!director) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
}
const director_id = director.personal_id;
const assign = await this.assignRepository.findOne({
relations: ["profile"],
where: { id: assign_id },
});
if (!assign) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
}
const postData: any = await {
assign_id,
...requestBody,
director_id,
no: requestBody.evaluate_no,
2025-01-11 13:29:21 +07:00
personal_id: assign.personal_id,
date_start: requestBody.start_date,
2025-01-23 10:23:05 +07:00
expand_month: requestBody.pass_result == 3 ? Number(requestBody.expand_month) : 0,
2025-01-11 13:29:21 +07:00
createdUserId: request.user.sub,
createdFullName: request.user.name,
updateUserId: request.user.sub,
updateFullName: request.user.name,
};
await this.evaluateResultRepository.save(postData, {
data: request,
});
setLogDataDiff(request, { before: null, after: postData });
const personal = await this.personalRepository.findOne({
where: { personal_id: assign.personal_id },
});
if (!personal) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคล");
}
personal.probation_status =
requestBody.pass_result == 1
2025-01-23 10:23:05 +07:00
? 2 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้รับราชการต่อ
2025-01-11 13:29:21 +07:00
: requestBody.pass_result == 2
2025-01-23 10:23:05 +07:00
? 3 // ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ออกจากราชการ
: requestBody.pass_result == 4
2025-01-28 15:52:40 +07:00
? 1 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ทดลองปฏิบัติหน้าที่ราชการต่อไป
2025-01-23 10:23:05 +07:00
: personal.probation_status;
2025-01-11 13:29:21 +07:00
if (requestBody.pass_result == 3) {
personal.probation_status = 7;
// #noti ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ กรณีขยายระยะเวลา
await new CallAPI()
.PostData(request, "/placement/noti", {
subject: "ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ",
body: `ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ เห็นควรให้ขยายระยะเวลาทดลองปฏิบัติหน้าที่ราชการต่อไปอีก ${requestBody.expand_month} เดือน`,
receiverUserId: assign.personal_id,
payload: "",
isSendMail: false,
isSendInbox: true,
isSendNotification: true,
})
.catch((error) => {
console.error("Error calling API:", error);
});
}
await this.personalRepository.save(personal, { data: request });
return new HttpSuccess();
}
/**
* API
*
* @summary
*
*/
@Put("")
async UpdateData(
@Query() assign_id: string,
@Query() evaluate_id: string,
@Body() requestBody: CreateEvaluateResult,
2025-01-23 10:23:05 +07:00
@Request() request: RequestWithUser,
2025-01-11 13:29:21 +07:00
) {
await new permission().PermissionUpdate(request, "SYS_PROBATION");
let evaluate = await this.evaluateResultRepository.findOne({
where: { id: evaluate_id },
});
const before = evaluate;
if (!evaluate) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมิน");
}
evaluate.date_start = requestBody.start_date;
evaluate.date_finish = requestBody.date_finish;
evaluate.develop_complete = requestBody.develop_complete;
evaluate.pass_result = requestBody.pass_result;
evaluate.expand_month =
requestBody.pass_result && requestBody.pass_result == 3
? Number(requestBody.expand_month)
: 0;
evaluate.reson = requestBody.reson;
evaluate.chairman_dated = requestBody.chairman_dated;
// evaluate.director1_dated = requestBody.director1_dated;
// evaluate.director2_dated = requestBody.director2_dated;
2025-01-11 13:29:21 +07:00
evaluate.updateUserId = request.user.sub;
evaluate.updateFullName = request.user.name;
await this.evaluateResultRepository.save(evaluate, { data: request });
setLogDataDiff(request, { before, after: evaluate });
const assign = await this.assignRepository.findOne({
relations: ["profile"],
where: { id: assign_id },
});
if (!assign) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
}
const personal = await this.personalRepository.findOne({
where: { personal_id: assign.personal_id },
});
if (!personal) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคล");
}
personal.probation_status =
requestBody.pass_result == 1
2025-01-23 10:23:05 +07:00
? 2 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้รับราชการต่อ
2025-01-11 13:29:21 +07:00
: requestBody.pass_result == 2
2025-01-23 10:23:05 +07:00
? 3 // ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ออกจากราชการ
: requestBody.pass_result == 4
2025-01-28 15:52:40 +07:00
? 1 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ทดลองปฏิบัติหน้าที่ราชการต่อไป
2025-01-23 10:23:05 +07:00
: personal.probation_status;
2025-01-11 13:29:21 +07:00
if (requestBody.pass_result == 3) {
personal.probation_status = 7;
// #noti ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ กรณีขยายระยะเวลา
await new CallAPI()
.PostData(request, "/placement/noti", {
subject: "ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ",
body: `ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ เห็นควรให้ขยายระยะเวลาทดลองปฏิบัติหน้าที่ราชการต่อไปอีก ${requestBody.expand_month} เดือน`,
receiverUserId: assign.personal_id,
payload: "",
isSendMail: false,
isSendInbox: true,
isSendNotification: true,
})
.catch((error) => {
console.error("Error calling API:", error);
});
}
await this.personalRepository.save(personal, { data: request });
return new HttpSuccess();
}
2024-09-05 13:59:43 +07:00
}