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" import { CreateEvaluateChairman, EvaluateChairman } from "../entities/EvaluateChairman" import permission from "../interfaces/permission" @Route("api/v1/probation/evaluate-chairman") @Tags("แบบประเมินผล (คณะกรรมการ)") @Security("bearerAuth") @Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง") export class EvaluateChairmanController extends Controller { private assignDirectorRepository = AppDataSource.getRepository(AssignDirector) private assignRepository = AppDataSource.getRepository(Assign) private evaluateChairmanRepository = AppDataSource.getRepository(EvaluateChairman) private personalRepository = AppDataSource.getRepository(Personal) /** * API ข้อมูลตอนกดสร้างแบบประเมินผล (คณะกรรมการ) * * @summary ข้อมูลตอนกดสร้างแบบประเมินผล (คณะกรรมการ) * */ @Get("create") async CreateEvaluate(@Query() assign_id: string, @Request() request: RequestWithUser) { await new permission().PermissionGet(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 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 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" }, }) 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.posType}: ${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.posType}: ${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.posType}: ${chairmanData.posLevel})` : ""), position: chairmanData.position, posType: chairmanData.posType, posLevel: chairmanData.posLevel, } : null 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("") async GetEvaluate(@Request() request: RequestWithUser, @Query() assign_id: string, @Query() evaluate_no?: string) { 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 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({ where: { director_id, assign_id, }, }) if (evaluate) evaluate = await evaluate.map((element: EvaluateChairman) => ({ ...element, no: Number(element.no), })) } 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 }, 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.posType}: ${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.posType}: ${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.posType}: ${chairmanData.posLevel})` : ""), position: chairmanData.position, posType: chairmanData.posType, posLevel: chairmanData.posLevel, } : null 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, @Request() request: RequestWithUser) { 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, 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: requestBody.behavior_orther.text != "" ? Number(requestBody.behavior_orther.level) : 0, 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( // @Query() assign_id: string, @Query() evaluate_id: string, @Body() requestBody: CreateEvaluateChairman, @Request() request: RequestWithUser ) { await new permission().PermissionUpdate(request, "SYS_PROBATION") let evaluate = await this.evaluateChairmanRepository.findOne({ where: { id: evaluate_id }, }) const before = evaluate if (!evaluate) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการประเมิน") } 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 = requestBody.achievement_other.text != "" ? Number(requestBody.achievement_other.level) : 0 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 = requestBody.behavior_orther.text != "" ? Number(requestBody.behavior_orther.level) : 0 evaluate.develop_orientation_score = requestBody.develop_orientation_score 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 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() } }