hrms-api-probation/src/controllers/EvaluateResultController.ts
2025-06-06 16:36:58 +07:00

631 lines
23 KiB
TypeScript

import {
Controller,
Route,
Security,
Tags,
Request,
SuccessResponse,
Response,
Get,
Post,
Body,
Query,
Put,
Patch,
} from "tsoa";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { RequestWithUser } from "../middlewares/user";
import { findEndDate, setLogDataDiff } from "../interfaces/utils";
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";
import { CreateEvaluateResult, EvaluateResult } from "../entities/EvaluateResult";
import permission from "../interfaces/permission";
@Route("api/v1/probation/evaluate-result")
@Tags("แบบรายงานการประเมินฯ")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
export class EvaluateResultController extends Controller {
private assignDirectorRepository = AppDataSource.getRepository(AssignDirector);
private assignRepository = AppDataSource.getRepository(Assign);
private evaluateChairmanRepository = AppDataSource.getRepository(EvaluateChairman);
private personalRepository = AppDataSource.getRepository(Personal);
private evaluateResultRepository = AppDataSource.getRepository(EvaluateResult);
/**
* API ข้อมูลตอนกดสร้างแบบรายงานการประเมินฯ
*
* @summary ข้อมูลตอนกดสร้างแบบรายงานการประเมินฯ
*
*/
@Get("create")
async CreateEvaluate(@Query() assign_id: string, @Request() request: RequestWithUser) {
try {
let _workflow = await new permission().Workflow(request, assign_id, "SYS_PROBATION");
if (_workflow == false) await new permission().PermissionGet(request, "SYS_PROBATION");
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:
assign.profile.prefixName + assign.profile.firstName + " " + assign.profile.lastName,
Oc: assign.profile.organization,
}
: null);
const directorData = await this.assignDirectorRepository.find({
where: { assign_id },
order: { ordering: "ASC" },
});
if (!directorData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล ผู้บังคับบัญชาและประธาน");
}
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,
label: e.fullname + " " + (e.position ? `(${e.position}${e.posLevel})` : ""),
position: e.position,
posType: e.posType,
posLevel: e.posLevel,
});
}
}
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,
posType: commanderData.posType,
posLevel: commanderData.posLevel,
}
: null;
const chairmanData = await (directorData.find((x) => x.role == "chairman") ?? null);
const chairman =
chairmanData != null
? {
personal_id: chairmanData.personal_id,
dated: chairmanData.dated,
name: chairmanData.fullname,
label:
chairmanData.fullname +
" " +
(chairmanData.position ? `(${chairmanData.position}${chairmanData.posLevel})` : ""),
position: chairmanData.position,
posType: chairmanData.posType,
posLevel: chairmanData.posLevel,
}
: null;
const resultData = await this.evaluateChairmanRepository.find({
select: [
"develop_orientation_score",
"develop_self_learning_score",
"develop_training_seminar_score",
"evaluate_result",
"no",
],
where: {
assign_id,
},
});
if (!resultData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมินผล");
}
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;
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;
}
return {
develop_complete,
evaluate_result,
evaluate_no: Number(e.no),
isResult: check > 0 ? true : false,
start_date: startDate,
end_date: endDate,
};
}),
);
// 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,
// };
return new HttpSuccess({
person: profile,
assign,
result,
mentors,
commander,
chairman,
});
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
}
}
/**
* API ข้อมูลแบบรายงานการประเมินฯ
*
* @summary ข้อมูลแบบรายงานการประเมินฯ
*
*/
@Get("")
async GetEvaluate(
@Request() request: RequestWithUser,
@Query() assign_id: string,
@Query() evaluate_no?: string,
) {
try {
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;
const evaluate = await this.evaluateResultRepository.findOne({
where: {
director_id,
assign_id,
no: evaluate_no ?? "1",
},
});
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",
"orgRootName",
"orgChild1Name",
"orgChild2Name",
"orgChild3Name",
"orgChild4Name",
],
where: { personal_id: assign.personal_id },
});
if (!experimenteeData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคล");
}
// const splitOc = await experimenteeData.organization.split(" ");
// const splitOcAmount = await splitOc.length;
const experimentee = await {
...experimenteeData,
name:
experimenteeData.prefixName +
experimenteeData.firstName +
" " +
experimenteeData.lastName,
PositionLevel: experimenteeData.positionName + experimenteeData.positionLevelName,
OrganizationOrganization: experimenteeData.orgChild2Name
? (experimenteeData.orgChild4Name ? experimenteeData.orgChild4Name + " " : "") +
(experimenteeData.orgChild3Name ? experimenteeData.orgChild3Name + " " : "") +
(experimenteeData.orgChild2Name ? experimenteeData.orgChild2Name + " " : "")
: "-",
Department: experimenteeData.orgChild1Name ?? "-",
Oc: experimenteeData.orgRootName,
};
const directorData = await this.assignDirectorRepository.find({
where: { assign_id },
order: { ordering: "ASC" },
});
if (!directorData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล ผู้บังคับบัญชาและประธาน");
}
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,
label: e.fullname + " " + (e.position ? `(${e.position}${e.posLevel})` : ""),
position: e.position + e.posLevel,
posType: e.posType,
posLevel: e.posLevel,
});
}
}
// 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,
};
const chairmanData = await (directorData.find((x) => x.role == "chairman") ?? null);
const chairman =
chairmanData != null
? {
personal_id: chairmanData.personal_id,
dated: chairmanData.dated,
name: chairmanData.fullname,
label:
chairmanData.fullname +
" " +
(chairmanData.position ? `(${chairmanData.position}${chairmanData.posLevel})` : ""),
position: chairmanData.position + chairmanData.posLevel,
posType: chairmanData.posType,
posLevel: chairmanData.posLevel,
}
: null;
return new HttpSuccess({
commander,
chairman,
mentors,
experimentee,
assign,
evaluate: evaluate ? evaluate : null,
});
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
}
}
/**
* API บันทึกข้อมูลแบบรายงานการประเมินฯ
*
* @summary บันทึกข้อมูลแบบรายงานการประเมินฯ
*
*/
@Post("")
async PostData(
@Query() assign_id: string,
@Body() requestBody: CreateEvaluateResult,
@Request() request: RequestWithUser,
) {
try {
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,
personal_id: assign.personal_id,
date_start: requestBody.start_date,
expand_month: requestBody.pass_result == 3 ? Number(requestBody.expand_month) : 0,
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 });
return new HttpSuccess();
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
}
}
/**
* API บันทึกข้อมูลแบบรายงานการประเมินฯ
*
* @summary บันทึกข้อมูลแบบรายงานการประเมินฯ
*
*/
@Patch("")
async PatchData(
@Query() assign_id: string,
@Query() evaluate_no: number | string,
@Request() request: RequestWithUser,
) {
try {
await new permission().PermissionUpdate(request, "SYS_PROBATION");
const assign = await this.assignRepository.findOne({
relations: ["profile"],
where: { id: assign_id },
});
if (!assign) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
}
const result = await this.evaluateResultRepository.findOne({
where: { assign_id, no: evaluate_no.toString() },
});
if (!result) {
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 =
result.pass_result == 1
? 2 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้รับราชการต่อ
: result.pass_result == 2
? 3 // ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ออกจากราชการ
: result.pass_result == 4
? 1 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ทดลองปฏิบัติหน้าที่ราชการต่อไป
: personal.probation_status;
if (result.pass_result == 3) {
personal.probation_status = 7;
// #noti ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ กรณีขยายระยะเวลา
await new CallAPI()
.PostData(request, "/placement/noti", {
subject: "ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ",
body: `ผลการประเมินการทดลองปฏิบัติหน้าที่ราชการ เห็นควรให้ขยายระยะเวลาทดลองปฏิบัติหน้าที่ราชการต่อไปอีก ${result.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();
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
}
}
/**
* API แก้ไขข้อมูลแบบรายงานการประเมินฯ
*
* @summary แก้ไขข้อมูลแบบรายงานการประเมินฯ
*
*/
@Put("")
async UpdateData(
@Query() assign_id: string,
@Query() evaluate_id: string,
@Body() requestBody: CreateEvaluateResult,
@Request() request: RequestWithUser,
) {
try {
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;
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
? 2 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้รับราชการต่อ
: requestBody.pass_result == 2
? 3 // ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ออกจากราชการ
: requestBody.pass_result == 4
? 1 // ไม่ต่ำกว่ามาตรฐานที่กำหนด เห็นควรให้ทดลองปฏิบัติหน้าที่ราชการต่อไป
: personal.probation_status;
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();
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
}
}
}