import { Controller, Get, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { Brackets } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { CreateDevelopmentScholarship, DevelopmentScholarship, UpdateDevelopmentScholarship, UpdateDevelopmentScholarshipUser, } from "../entities/DevelopmentScholarship"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import CallAPI from "../interfaces/call-api"; @Route("api/v1/development/scholarship") @Tags("DevelopmentScholarship") @Security("bearerAuth") export class DevelopmentScholarshipController extends Controller { private developmentScholarshipRepository = AppDataSource.getRepository(DevelopmentScholarship); private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); /** * API เพิ่มทุนการศึกษา/ฝึกอบรม * * @summary DEV_011 - เพิ่มทุนการศึกษา/ฝึกอบรม#11 * */ @Post() async CreateDevelopmentScholarship( @Body() requestBody: CreateDevelopmentScholarship, @Request() request: { user: Record }, ) { if (requestBody.posTypeId != null) { const checkId = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); } } if (requestBody.posLevelId != null) { const checkId = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); } } const development = Object.assign(new DevelopmentScholarship(), requestBody); development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentScholarshipRepository.save(development); return new HttpSuccess(development.id); } /** * API แก้ไขทุนการศึกษา/ฝึกอบรม * * @summary DEV_012 - แก้ไขทุนการศึกษา/ฝึกอบรม #12 * * @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา */ @Put("{id}") async UpdateDevelopmentScholarship( @Path() id: string, @Body() requestBody: UpdateDevelopmentScholarship, @Request() request: { user: Record }, ) { const development = await this.developmentScholarshipRepository.findOne({ where: { id: id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้"); } if (requestBody.posTypeId != null) { const checkId = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); } } if (requestBody.posLevelId != null) { const checkId = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); } } Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentScholarshipRepository.save(development); return new HttpSuccess(development.id); } /** * API ลบทุนการศึกษา/ฝึกอบรม * * @summary DEV_013 - ลบทุนการศึกษา/ฝึกอบรม #13 * * @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา */ @Delete("{id}") async DeleteDevelopmentScholarship(@Path() id: string) { const development = await this.developmentScholarshipRepository.findOne({ where: { id: id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้"); } await this.developmentScholarshipRepository.remove(development); return new HttpSuccess(); } /** * API รายการทุนการศึกษา/ฝึกอบรม * * @summary DEV_014 - รายการทุนการศึกษา/ฝึกอบรม #14 * */ @Get() async GetDevelopmentScholarshipLists( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, @Query("year") year?: number, @Query("scholarshipType") scholarshipType?: string, ) { const [development, total] = await AppDataSource.getRepository(DevelopmentScholarship) .createQueryBuilder("developmentScholarship") .leftJoinAndSelect("developmentScholarship.posLevel", "posLevel") .leftJoinAndSelect("developmentScholarship.posType", "posType") .andWhere( year !== 0 && year != null && year != undefined ? "developmentScholarship.scholarshipYear = :scholarshipYear" : "1=1", { scholarshipYear: year }, ) .andWhere( scholarshipType != null && scholarshipType != undefined ? "developmentScholarship.scholarshipType = :scholarshipType" : "1=1", { scholarshipType: scholarshipType }, ) .andWhere( new Brackets((qb) => { qb.where( keyword != null && keyword != "" ? `CONCAT(developmentScholarship.prefix, developmentScholarship.firstName," ",developmentScholarship.lastName) like '%${keyword}%'` : "1=1", { keyword: `%${keyword}%`, }, ) .orWhere( keyword != null && keyword != "" ? "developmentScholarship.position LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }, ) .orWhere( keyword != null && keyword != "" ? "developmentScholarship.posExecutive LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }, ) .orWhere( keyword != null && keyword != "" ? "posType.posTypeName LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }, ) .orWhere( keyword != null && keyword != "" ? "posLevel.posLevelName LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }, ); }), ) .orderBy("developmentScholarship.createdAt", "DESC") .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); const formattedData = development.map((item) => ({ id: item.id, citizenId: item.citizenId, fullName: item.prefix + item.firstName + " " + item.lastName, position: item.position, posType: item.posType ? item.posType.posTypeName : null, posLevel: item.posLevel ? item.posLevel.posLevelName : null, posExecutive: item.posExecutive, })); return new HttpSuccess({ data: formattedData, total }); } /** * API รายละเอียดทุนการศึกษา/ฝึกอบรม * * @summary DEV_015 - รายละเอียดทุนการศึกษา/ฝึกอบรม #15 * * @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา */ @Get("{id}") async GetDevelopemtScholarshipById(@Path() id: string) { const getDevelopment = await this.developmentScholarshipRepository.findOne({ relations: ["posLevel", "posType", "posLevelguarantor", "posTypeguarantor"], where: { id: id }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้"); } const formattedData = { root: getDevelopment.root ? getDevelopment.root : null, rank: getDevelopment.rank ? getDevelopment.rank : null, prefix: getDevelopment.prefix ? getDevelopment.prefix : null, firstName: getDevelopment.firstName ? getDevelopment.firstName : null, lastName: getDevelopment.lastName ? getDevelopment.lastName : null, citizenId: getDevelopment.citizenId ? getDevelopment.citizenId : null, position: getDevelopment.position ? getDevelopment.position : null, posExecutive: getDevelopment.posExecutive ? getDevelopment.posExecutive : null, posLevelId: getDevelopment.posLevelId ? getDevelopment.posLevelId : null, posLevelName: getDevelopment.posLevel?.posLevelName ? getDevelopment.posLevel?.posLevelName : null, posTypeId: getDevelopment.posTypeId ? getDevelopment.posTypeId : null, posTypeName: getDevelopment.posType?.posTypeName ? getDevelopment.posType?.posTypeName : null, guarantorRank: getDevelopment.guarantorRank ? getDevelopment.guarantorRank : null, guarantorPrefix: getDevelopment.guarantorPrefix ? getDevelopment.guarantorPrefix : null, guarantorFirstName: getDevelopment.guarantorFirstName ? getDevelopment.guarantorFirstName : null, guarantorLastName: getDevelopment.guarantorLastName ? getDevelopment.guarantorLastName : null, guarantorCitizenId: getDevelopment.guarantorCitizenId ? getDevelopment.guarantorCitizenId : null, guarantorPosition: getDevelopment.guarantorPosition ? getDevelopment.guarantorPosition : null, guarantorPosExecutive: getDevelopment.guarantorPosExecutive ? getDevelopment.guarantorPosExecutive : null, posLevelguarantorId: getDevelopment.posLevelguarantorId ? getDevelopment.posLevelguarantorId : null, posLevelguarantorName: getDevelopment.posLevelguarantor?.posLevelName ? getDevelopment.posLevelguarantor?.posLevelName : null, posTypeguarantorId: getDevelopment.posTypeguarantorId ? getDevelopment.posTypeguarantorId : null, posTypeguarantorName: getDevelopment.posTypeguarantor?.posTypeName ? getDevelopment.posTypeguarantor?.posTypeName : null, scholarshipYear: getDevelopment.scholarshipYear ? getDevelopment.scholarshipYear : null, budgetSource: getDevelopment.budgetSource ? getDevelopment.budgetSource : null, budgetApprove: getDevelopment.budgetApprove ? getDevelopment.budgetApprove : null, bookNo: getDevelopment.bookNo ? getDevelopment.bookNo : null, bookNoDate: getDevelopment.bookNoDate ? getDevelopment.bookNoDate : null, bookApproveDate: getDevelopment.bookApproveDate ? getDevelopment.bookApproveDate : null, useOfficialTime: getDevelopment.useOfficialTime ? getDevelopment.useOfficialTime : false, changeDetail: getDevelopment.changeDetail ? getDevelopment.changeDetail : null, scholarshipType: getDevelopment.scholarshipType ? getDevelopment.scholarshipType : null, fundType: getDevelopment.fundType ? getDevelopment.fundType : null, contractNo: getDevelopment.contractNo ? getDevelopment.contractNo : null, contractDate: getDevelopment.contractDate ? getDevelopment.contractDate : null, reportBackNo: getDevelopment.reportBackNo ? getDevelopment.reportBackNo : null, reportBackNoDate: getDevelopment.reportBackNoDate ? getDevelopment.reportBackNoDate : null, reportBackDate: getDevelopment.reportBackDate ? getDevelopment.reportBackDate : null, degreeLevel: getDevelopment.degreeLevel ? getDevelopment.degreeLevel : null, course: getDevelopment.course ? getDevelopment.course : null, field: getDevelopment.field ? getDevelopment.field : null, faculty: getDevelopment.faculty ? getDevelopment.faculty : null, educationalInstitution: getDevelopment.educationalInstitution ? getDevelopment.educationalInstitution : null, startDate: getDevelopment.startDate ? getDevelopment.startDate : null, endDate: getDevelopment.endDate ? getDevelopment.endDate : null, studyPlace: getDevelopment.studyPlace ? getDevelopment.studyPlace : null, studyTopic: getDevelopment.studyTopic ? getDevelopment.studyTopic : null, studyStartDate: getDevelopment.studyStartDate ? getDevelopment.studyStartDate : null, studyEndDate: getDevelopment.studyEndDate ? getDevelopment.studyEndDate : null, studyCountry: getDevelopment.studyCountry ? getDevelopment.studyCountry : null, studyAbroadTopic: getDevelopment.studyAbroadTopic ? getDevelopment.studyAbroadTopic : null, studyAbroadStartDate: getDevelopment.studyAbroadStartDate ? getDevelopment.studyAbroadStartDate : null, studyAbroadEndDate: getDevelopment.studyAbroadEndDate ? getDevelopment.studyAbroadEndDate : null, totalPeriod: getDevelopment.totalPeriod ? getDevelopment.totalPeriod : null, status: getDevelopment.status ? getDevelopment.status : null, profileId: getDevelopment.profileId ? getDevelopment.profileId : null, planType: getDevelopment.planType ? getDevelopment.planType : null, isNoUseBudget: getDevelopment.isNoUseBudget ? getDevelopment.isNoUseBudget : null, }; return new HttpSuccess(formattedData); } /** * API รายการทุนของ user * * @summary DEV_0 - รายการทุนของ user # * * @param {string} profileId profileId ข้าราชการฯที่ได้รับทุนการศึกษา */ @Get("user/{profileId}") async GetDevelopemtScholarshipUserById(@Path() profileId: string) { const getDevelopment = await this.developmentScholarshipRepository.find({ where: { profileId: profileId }, }); const formattedData = getDevelopment.map((item) => ({ id: item.id, scholarshipYear: item.scholarshipYear, scholarshipType: item.scholarshipType, fundType: item.fundType, })); return new HttpSuccess(formattedData); } /** * API รายละเอียดทุนของ user * * @summary DEV_0 - รายละเอียดทุนของ user # * * @param {string} id id รายการ */ @Get("user/detail/{id}") async GetDevelopemtScholarshipUserDetailById(@Path() id: string) { const getDevelopment = await this.developmentScholarshipRepository.findOne({ where: { id: id }, select: [ "id", "scholarshipYear", "scholarshipType", "fundType", "bookNumber", "bookDate", "governmentDate", "governmentEndDate", "isGraduated", "graduatedDate", "graduatedReason", "root", ], }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้"); } return new HttpSuccess(getDevelopment); } /** * API แก้ไขรายการทุนของ user * * @summary DEV_015 - แก้ไขรายการทุนของ user #15 * * @param {string} id รายการ */ @Put("user/detail/{id}") async UpdateDevelopemtScholarshipUserById( @Path() id: string, @Body() requestBody: UpdateDevelopmentScholarshipUser, @Request() request: { user: Record }, ) { const getDevelopment = await this.developmentScholarshipRepository.findOne({ where: { id: id }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้"); } Object.assign(getDevelopment, requestBody); getDevelopment.lastUpdateUserId = request.user.sub; getDevelopment.lastUpdateFullName = request.user.name; await this.developmentScholarshipRepository.save(getDevelopment); return new HttpSuccess(getDevelopment.id); } /** * API เปลี่ยนสถานะ * * @summary DEV_0 - เปลี่ยนสถานะ # * * @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา * @param {string} status status สถานะ */ @Get("status/{id}/{status}") async ChangeStatusDevelopemtScholarshipById( @Path() id: string, @Path() status: string, @Request() request: { user: Record }, ) { const getDevelopment = await this.developmentScholarshipRepository.findOne({ where: { id: id }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้"); } const _status = status.trim().toUpperCase(); getDevelopment.status = _status; getDevelopment.lastUpdateUserId = request.user.sub; getDevelopment.lastUpdateFullName = request.user.name; if (_status == "GRADUATE") { if (getDevelopment.scholarshipType != null) { switch (getDevelopment.scholarshipType.trim().toUpperCase()) { case "DOMESTICE": getDevelopment.scholarshipType = "การศึกษาในประเทศ"; break; case "NOABROAD": getDevelopment.scholarshipType = "ฝึกอบรมในประเทศที่ส่งไปพัฒนากับหน่วยวงานภายนอก (หลักสูตรที่ไม่มีการไปต่างประเทศ)"; break; case "ABROAD": getDevelopment.scholarshipType = "ฝึกอบรมในประเทศที่ส่งไปพัฒนากับหน่วยวงานภายนอก (หลักสูตรที่มีการไปต่างประเทศ)"; break; case "EXECUTIVE": getDevelopment.scholarshipType = "ฝึกอบรมในประเทศที่ส่งไปพัฒนากับหน่วยวงานภายนอก (หลักสูตรประเภทนักบริหาร)"; break; default: break; } } let profileEdu = await new CallAPI().PostData(request, "org/profile/educations", { profileId: getDevelopment.profileId, institute: getDevelopment.educationalInstitution, startDate: getDevelopment.startDate, endDate: getDevelopment.endDate, finishDate: null, isEducation: false, degree: null, field: getDevelopment.field, fundName: getDevelopment.scholarshipType, gpa: null, country: getDevelopment.studyCountry, other: null, duration: getDevelopment.totalPeriod, durationYear: 0, note: null, educationLevel: getDevelopment.degreeLevel, educationLevelId: null, isDate: false, positionPath: null, positionPathId: null, }); } else if (_status == "NOTGRADUATE") { } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบสถานะนี้ในระบบ"); } return new HttpSuccess(); } }