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

400 lines
14 KiB
TypeScript

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";
import { 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";
@Route("api/v1/probation/evaluate-result")
@Tags("แบบรายงานการประเมินฯ")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
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) {
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 },
});
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 += e.fullname;
if (index < mentorList.length - 1) {
mentors += ", ";
}
}
}
const commanderData = await (directorData.find((x) => x.role == "commander") ?? null);
const commander = commanderData ? commanderData.fullname : null;
const chairmanData = await (directorData.find((x) => x.role == "chairman") ?? null);
const chairman = chairmanData ? chairmanData.fullname : null;
const resultData = await this.evaluateChairmanRepository.findOne({
select: [
"develop_orientation_score",
"develop_self_learning_score",
"develop_training_seminar_score",
"evaluate_result",
],
where: {
assign_id,
},
});
if (!resultData) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมินผล");
}
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,
});
}
/**
* API ข้อมูลแบบรายงานการประเมินฯ
*
* @summary ข้อมูลแบบรายงานการประเมินฯ
*
*/
@Get("")
async GetEvaluate(@Query() assign_id: string, @Query() evaluate_no?: string) {
// ต้องปรับเป็น 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.evaluateChairmanRepository.findOne({
where: {
director_id,
assign_id,
},
});
if (!evaluate) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมิน");
}
const assign = await this.assignRepository.findOne({
where: { id: assign_id },
});
if (!assign) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
}
const experimenteeData = await this.personalRepository.find({
select: [
"personal_id",
"prefixName",
"firstName",
"lastName",
"positionName",
"positionLevelName",
"organization",
],
where: { personal_id: assign.personal_id },
});
const experimentee = await experimenteeData.map((element) => ({
...element,
name: element.prefixName + element.firstName + " " + element.lastName,
Oc: element.organization,
}));
const directorData = await this.assignDirectorRepository.find({
where: { assign_id },
});
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 += e.fullname;
if (index < mentorList.length - 1) {
mentors += ", ";
}
}
}
const commanderData = await (directorData.find((x) => x.role == "commander") ?? null);
const commander = commanderData ? commanderData.fullname : null;
const chairmanData = await (directorData.find((x) => x.role == "chairman") ?? null);
const chairman = chairmanData ? chairmanData.fullname : null;
return new HttpSuccess({
commander,
chairman,
mentors,
experimentee,
assign,
evaluate: evaluate,
});
}
/**
* API บันทึกข้อมูลแบบรายงานการประเมินฯ
*
* @summary บันทึกข้อมูลแบบรายงานการประเมินฯ
*
*/
@Post("")
async PostData(
@Query() assign_id: string,
@Body() requestBody: CreateEvaluateResult,
@Request() request: RequestWithUser,
) {
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: 1,
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 });
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
: 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();
}
/**
* API แก้ไขข้อมูลแบบรายงานการประเมินฯ
*
* @summary แก้ไขข้อมูลแบบรายงานการประเมินฯ
*
*/
@Put("")
async UpdateData(
@Query() assign_id: string,
@Query() evaluate_id: string,
@Body() requestBody: CreateEvaluateResult,
@Request() request: RequestWithUser,
) {
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 == 3 ? 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
: 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();
}
}