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

497 lines
18 KiB
TypeScript
Raw Normal View History

2024-09-05 13:59:43 +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";
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";
2024-09-05 16:56:22 +07:00
import { CreateEvaluateChairman, EvaluateChairman } from "../entities/EvaluateChairman";
2024-09-06 15:17:42 +07:00
import permission from "../interfaces/permission";
2024-09-05 16:56:22 +07:00
@Route("api/v1/probation/evaluate-chairman")
2024-09-05 13:59:43 +07:00
@Tags("แบบประเมินผล (คณะกรรมการ)")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
2024-09-05 16:56:22 +07:00
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
2024-09-05 13:59:43 +07:00
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class EvaluateChairmanController extends Controller {
2024-09-05 16:56:22 +07:00
private assignDirectorRepository = AppDataSource.getRepository(AssignDirector);
2024-09-05 13:59:43 +07:00
private assignRepository = AppDataSource.getRepository(Assign);
2024-09-05 16:56:22 +07:00
private evaluateChairmanRepository = AppDataSource.getRepository(EvaluateChairman);
2024-09-05 13:59:43 +07:00
private personalRepository = AppDataSource.getRepository(Personal);
/**
* API ()
*
* @summary ()
*
*/
@Get("create")
2024-09-06 15:17:42 +07:00
async CreateEvaluate(@Query() assign_id: string, @Request() request: RequestWithUser) {
await new permission().PermissionGet(request, "SYS_PROBATION");
2024-09-05 13:59:43 +07:00
const director = await this.assignDirectorRepository.findOne({
select: ["personal_id"],
where: {
assign_id,
role: "chairman",
},
});
if (!director) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
2024-09-05 13:59:43 +07:00
}
const director_id = director.personal_id;
const assign = await this.assignRepository.findOne({
relations: ["profile"],
where: { id: assign_id },
});
if (!assign) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
2024-09-05 13:59:43 +07:00
}
const profile = await (assign.profile
? {
...assign.profile,
id: assign.profile.personal_id,
name:
2024-09-05 16:56:22 +07:00
assign.profile.prefixName + assign.profile.firstName + " " + assign.profile.lastName,
2024-09-05 13:59:43 +07:00
Oc: assign.profile.organization,
}
: null);
const evaluate_amount = await this.evaluateChairmanRepository.count({
where: {
assign_id,
director_id,
},
});
const evaluate_no = await (evaluate_amount + 1);
const start_date =
evaluate_amount == 0
? assign.date_start
: findEndDate(evaluate_amount * 3, assign.date_start);
const directorData = await this.assignDirectorRepository.find({
where: { assign_id },
order: { ordering: "ASC" },
2024-09-05 13:59:43 +07:00
});
if (!directorData) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล ผู้บังคับบัญชาและประธาน");
2024-09-05 13:59:43 +07:00
}
let mentors = [];
2024-09-05 13:59:43 +07:00
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.posType}: ${e.posLevel})` : ""),
position: e.position,
posType: e.posType,
posLevel: e.posLevel,
});
2024-09-05 13:59:43 +07:00
}
}
2024-09-05 16:56:22 +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.posType}: ${commanderData.posLevel})`
: ""),
position: commanderData.position,
posType: commanderData.posType,
posLevel: commanderData.posLevel,
}
: null;
2024-09-05 13:59:43 +07:00
2024-09-05 16:56:22 +07:00
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.posType}: ${chairmanData.posLevel})`
: ""),
position: chairmanData.position,
posType: chairmanData.posType,
posLevel: chairmanData.posLevel,
}
: null;
2024-09-05 13:59:43 +07:00
return new HttpSuccess({
person: profile ? profile : null,
assign,
evaluate_no: evaluate_no,
start_date: start_date,
end_date: findEndDate(3, start_date),
commander,
mentors,
chairman,
});
}
/**
* API ()
*
* @summary ()
*
*/
@Get("")
2024-09-06 15:17:42 +07:00
async GetEvaluate(
@Request() request: RequestWithUser,
@Query() assign_id: string,
@Query() evaluate_no?: string,
) {
await new permission().PermissionGet(request, "SYS_PROBATION");
2024-09-05 13:59:43 +07:00
// ต้องปรับเป็น id ของคนที่ access เข้ามา
const director = await this.assignDirectorRepository.findOne({
select: ["personal_id"],
where: {
assign_id,
role: "chairman",
},
});
if (!director) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
2024-09-05 13:59:43 +07:00
}
const director_id = director.personal_id;
let evaluate: any = null;
if (evaluate_no) {
evaluate = await this.evaluateChairmanRepository.findOne({
where: {
director_id,
assign_id,
no: evaluate_no,
},
});
} else {
evaluate = await this.evaluateChairmanRepository.find({
2024-09-05 13:59:43 +07:00
where: {
director_id,
assign_id,
},
});
if (evaluate)
evaluate = await evaluate.map((element: EvaluateChairman) => ({
...element,
no: Number(element.no),
}));
2024-09-05 13:59:43 +07:00
}
if (!evaluate) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมิน");
2024-09-05 13:59:43 +07:00
}
const assign = await this.assignRepository.findOne({
where: { id: assign_id },
});
if (!assign) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
2024-09-05 13:59:43 +07:00
}
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 },
order: { ordering: "ASC" },
2024-09-05 13:59:43 +07:00
});
if (!directorData) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล ผู้บังคับบัญชาและประธาน");
2024-09-05 13:59:43 +07:00
}
let mentors = [];
2024-09-05 13:59:43 +07:00
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.posType}: ${e.posLevel})` : ""),
position: e.position,
posType: e.posType,
posLevel: e.posLevel,
});
2024-09-05 13:59:43 +07:00
}
}
2024-09-05 16:56:22 +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.posType}: ${commanderData.posLevel})`
: ""),
position: commanderData.position,
posType: commanderData.posType,
posLevel: commanderData.posLevel,
}
: null;
2024-09-05 13:59:43 +07:00
2024-09-05 16:56:22 +07:00
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.posType}: ${chairmanData.posLevel})`
: ""),
position: chairmanData.position,
posType: chairmanData.posType,
posLevel: chairmanData.posLevel,
}
: null;
2024-09-05 13:59:43 +07:00
return new HttpSuccess({
experimentee: experimentee,
mentors: mentors,
commander: commander,
chairman: chairman,
assign,
evaluate,
});
}
/**
* API ()
*
* @summary ()
*
*/
@Post("")
async PostData(
@Query() assign_id: string,
@Body() requestBody: CreateEvaluateChairman,
2024-09-05 16:56:22 +07:00
@Request() request: RequestWithUser,
2024-09-05 13:59:43 +07:00
) {
2024-09-06 15:17:42 +07:00
await new permission().PermissionUpdate(request, "SYS_PROBATION");
2024-09-05 13:59:43 +07:00
const director = await this.assignDirectorRepository.findOne({
select: ["personal_id"],
where: {
assign_id,
role: "chairman",
},
});
if (!director) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ดูแล");
2024-09-05 13:59:43 +07:00
}
const director_id = director.personal_id;
const assign = await this.assignRepository.findOne({
relations: ["profile"],
where: { id: assign_id },
});
if (!assign) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบมอบหมายงาน");
2024-09-05 13:59:43 +07:00
}
const postData: any = await {
assign_id,
...requestBody,
director_id,
no: requestBody.evaluate_no,
date_start: requestBody.start_date,
personal_id: assign.personal_id,
achievement_other_desc: requestBody.achievement_other
? requestBody.achievement_other.text
: "",
achievement_other_level: requestBody.achievement_other
? Number(requestBody.achievement_other.level)
: 0,
behavior_other_desc: requestBody.behavior_orther.text,
behavior_other_level:
2024-09-05 16:56:22 +07:00
requestBody.behavior_orther.text != "" ? Number(requestBody.behavior_orther.level) : 0,
2024-09-05 13:59:43 +07:00
createdUserId: request.user.sub,
createdFullName: request.user.name,
updateUserId: request.user.sub,
updateFullName: request.user.name,
};
await this.evaluateChairmanRepository.save(postData, {
data: request,
});
setLogDataDiff(request, { before: null, after: postData });
if (Number(requestBody.evaluate_no) < 2) {
// #noted cronjob
// แจ้งประธานเข้ามาบันทึกผลทุก 3 เดือน 2 ครั้ง
var dateSend = await findEndDate(3, requestBody.start_date);
const nextNo = await (Number(requestBody.evaluate_no) + 1);
await new CallAPI()
.PostData(request, "/placement/noti", {
subject: `ถึงกำหนดประเมินผลการทดลองปฏิบัติหน้าที่ราชการครั้งที่ ${nextNo} ${assign.profile.prefixName}${assign.profile.firstName} ${assign.profile.lastName}`,
body: `ถึงกำหนดประเมินผลการทดลองปฏิบัติหน้าที่ราชการ (สำหรับคณะกรรมการ) ครั้งที่ ${nextNo} ${assign.profile.prefixName}${assign.profile.firstName} ${assign.profile.lastName}`,
receiverUserId: director_id,
payload: "",
isSendMail: true,
isSendInbox: true,
receiveDate: dateSend,
})
.catch((error) => {
console.error("Error calling API:", error);
});
}
return new HttpSuccess();
}
/**
* API ()
*
* @summary ()
*
*/
@Put("")
async UpdateData(
2024-09-05 16:56:22 +07:00
// @Query() assign_id: string,
2024-09-05 13:59:43 +07:00
@Query() evaluate_id: string,
@Body() requestBody: CreateEvaluateChairman,
2024-09-05 16:56:22 +07:00
@Request() request: RequestWithUser,
2024-09-05 13:59:43 +07:00
) {
2024-09-06 15:17:42 +07:00
await new permission().PermissionUpdate(request, "SYS_PROBATION");
2024-09-05 13:59:43 +07:00
let evaluate = await this.evaluateChairmanRepository.findOne({
where: { id: evaluate_id },
});
const before = evaluate;
if (!evaluate) {
2024-09-06 09:37:29 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมิน");
2024-09-05 13:59:43 +07:00
}
evaluate.chairman_dated = requestBody.chairman_dated;
evaluate.director1_dated = requestBody.director1_dated;
evaluate.director2_dated = requestBody.director2_dated;
evaluate.knowledge_level = requestBody.knowledge_level;
evaluate.apply_level = requestBody.apply_level;
evaluate.success_level = requestBody.success_level;
evaluate.achievement_other_desc = requestBody.achievement_other
? requestBody.achievement_other.text
: "";
evaluate.achievement_other_level =
2024-09-05 16:56:22 +07:00
requestBody.achievement_other.text != "" ? Number(requestBody.achievement_other.level) : 0;
2024-09-05 13:59:43 +07:00
evaluate.conduct1_level = requestBody.conduct1_level;
evaluate.conduct2_level = requestBody.conduct2_level;
evaluate.conduct3_level = requestBody.conduct3_level;
evaluate.conduct4_level = requestBody.conduct4_level;
evaluate.moral1_level = requestBody.moral1_level;
evaluate.moral2_level = requestBody.moral2_level;
evaluate.moral3_level = requestBody.moral3_level;
evaluate.discipline1_level = requestBody.discipline1_level;
evaluate.discipline2_level = requestBody.discipline2_level;
evaluate.discipline3_level = requestBody.discipline3_level;
evaluate.discipline4_level = requestBody.discipline4_level;
evaluate.discipline5_level = requestBody.discipline5_level;
evaluate.behavior_other_desc = requestBody.behavior_orther.text;
evaluate.behavior_other_level =
2024-09-05 16:56:22 +07:00
requestBody.behavior_orther.text != "" ? Number(requestBody.behavior_orther.level) : 0;
2024-09-05 13:59:43 +07:00
evaluate.develop_orientation_score = requestBody.develop_orientation_score;
2024-09-05 16:56:22 +07:00
evaluate.develop_self_learning_score = requestBody.develop_self_learning_score;
evaluate.develop_training_seminar_score = requestBody.develop_training_seminar_score;
evaluate.develop_other_training_score = requestBody.develop_other_training_score;
evaluate.develop_orientation_percent = requestBody.develop_orientation_percent;
evaluate.develop_self_learning_percent = requestBody.develop_self_learning_percent;
evaluate.develop_training_seminar_percent = requestBody.develop_training_seminar_percent;
evaluate.develop_other_training_percent = requestBody.develop_other_training_percent;
2024-09-05 13:59:43 +07:00
evaluate.develop_result = requestBody.develop_result;
evaluate.achievement_score = requestBody.achievement_score;
evaluate.achievement_score_total = requestBody.achievement_score_total;
evaluate.achievement_percent = requestBody.achievement_percent;
evaluate.achievement_result = requestBody.achievement_result;
evaluate.behavior_score = requestBody.behavior_score;
evaluate.behavior_score_total = requestBody.behavior_score_total;
evaluate.behavior_percent = requestBody.behavior_percent;
evaluate.behavior_result = requestBody.behavior_result;
evaluate.sum_score = requestBody.sum_score;
evaluate.sum_percent = requestBody.sum_percent;
evaluate.evaluate_result = requestBody.evaluate_result;
evaluate.updateUserId = request.user.sub;
evaluate.updateFullName = request.user.name;
await this.evaluateChairmanRepository.save(evaluate, { data: request });
setLogDataDiff(request, { before, after: evaluate });
return new HttpSuccess();
}
}