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" 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) { 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.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 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(@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 const evaluate = await this.evaluateResultRepository.findOne({ where: { director_id, assign_id, }, }) 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"], where: { personal_id: assign.personal_id }, }) if (!experimenteeData) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคล") } const experimentee = await { ...experimenteeData, name: experimenteeData.prefixName + experimenteeData.firstName + " " + experimenteeData.lastName, Oc: experimenteeData.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({ commander, chairman, mentors, experimentee, assign, evaluate: evaluate, }) } /** * API บันทึกข้อมูลแบบรายงานการประเมินฯ * * @summary บันทึกข้อมูลแบบรายงานการประเมินฯ * */ @Post("") async PostData(@Query() assign_id: string, @Body() requestBody: CreateEvaluateResult, @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: 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) { 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 : 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() } }