hrms-api-development/src/controllers/DevelopmentScholarshipController.ts

712 lines
32 KiB
TypeScript

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";
import { RequestWithUser } from "../middlewares/user";
import { setLogDataDiff } from "../interfaces/utils";
import permission from "../interfaces/permission";
@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: RequestWithUser,
) {
await new permission().PermissionCreate(request, "SYS_DEV_SCHOLARSHIP");
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 before = null;
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;
development.createdAt = new Date();
development.lastUpdatedAt = new Date();
await this.developmentScholarshipRepository.save(development, { data: request });
setLogDataDiff(request, { before, after: 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: RequestWithUser,
) {
await new permission().PermissionUpdate(request, "SYS_DEV_SCHOLARSHIP");
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, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const before = structuredClone(development);
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
development.lastUpdatedAt = new Date();
await this.developmentScholarshipRepository.save(development, { data: request });
setLogDataDiff(request, { before, after: development });
return new HttpSuccess(development.id);
}
/**
* API ลบทุนการศึกษา/ฝึกอบรม
*
* @summary DEV_013 - ลบทุนการศึกษา/ฝึกอบรม #13
*
* @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา
*/
@Delete("{id}")
async DeleteDevelopmentScholarship(@Path() id: string, @Request() request: RequestWithUser) {
await new permission().PermissionDelete(request, "SYS_DEV_SCHOLARSHIP");
const development = await this.developmentScholarshipRepository.findOne({
where: { id: id },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้");
}
await this.developmentScholarshipRepository.remove(development, { data: request });
return new HttpSuccess();
}
/**
* API รายการทุนการศึกษา/ฝึกอบรม
*
* @summary DEV_014 - รายการทุนการศึกษา/ฝึกอบรม #14
*
*/
@Get()
async GetDevelopmentScholarshipLists(
@Request() request: RequestWithUser,
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
@Query("year") year?: number,
@Query("scholarshipType") scholarshipType?: string,
) {
await new permission().PermissionList(request, "SYS_DEV_SCHOLARSHIP");
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.citizenId 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.scholarshipYear", "DESC")
.addOrderBy("developmentScholarship.createdAt", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const formattedData = development.map((item) => ({
id: item.id,
year: item.scholarshipYear,
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,
status: item.status,
}));
return new HttpSuccess({ data: formattedData, total });
}
/**
* API รายละเอียดทุนการศึกษา/ฝึกอบรม
*
* @summary DEV_015 - รายละเอียดทุนการศึกษา/ฝึกอบรม #15
*
* @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา
*/
@Get("{id}")
async GetDevelopemtScholarshipById(@Request() request: RequestWithUser, @Path() id: string) {
//await new permission().PermissionGet(request, "SYS_DEV_SCHOLARSHIP"); //USER
const getDevelopment = await this.developmentScholarshipRepository.findOne({
relations: ["posLevel", "posType", "posLevelguarantor", "posTypeguarantor"],
where: { id: id },
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้");
}
const formattedData = {
rootId: getDevelopment.rootId ? getDevelopment.rootId : null,
root: getDevelopment.root ? getDevelopment.root : null,
org: getDevelopment.org ? getDevelopment.org : null,
orgRootShortName: getDevelopment.orgRootShortName ? getDevelopment.orgRootShortName : null,
orgRevisionId: getDevelopment.orgRevisionId ? getDevelopment.orgRevisionId : 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,
guarantorRootId: getDevelopment.guarantorRootId ? getDevelopment.guarantorRootId : null,
guarantorRoot: getDevelopment.guarantorRoot ? getDevelopment.guarantorRoot : null,
guarantorOrg: getDevelopment.guarantorOrg ? getDevelopment.guarantorOrg : null,
guarantorOrgRootShortName: getDevelopment.guarantorOrgRootShortName
? getDevelopment.guarantorOrgRootShortName
: null,
guarantorOrgRevisionId: getDevelopment.guarantorOrgRevisionId
? getDevelopment.guarantorOrgRevisionId
: 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 : null,
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 แก้ไขทุนการศึกษา/ฝึกอบรม
*
* @summary DEV_012 - แก้ไขทุนการศึกษา/ฝึกอบรม #12
*
* @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา
*/
@Put("admin/{id}")
async UpdateDevelopmentScholarshipAdminById(
@Path() id: string,
@Body() requestBody: UpdateDevelopmentScholarship,
@Request() request: RequestWithUser,
) {
await new permission().PermissionUpdate(request, "SYS_DEV_SCHOLARSHIP");
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, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const before = structuredClone(development);
Object.assign(development, requestBody);
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
development.lastUpdatedAt = new Date();
await this.developmentScholarshipRepository.save(development, { data: request });
setLogDataDiff(request, { before, after: development });
return new HttpSuccess(development.id);
}
/**
* API รายละเอียดทุนการศึกษา/ฝึกอบรม ADMIN
*
* @summary DEV_015 - รายละเอียดทุนการศึกษา/ฝึกอบรม #15 ADMIN
*
* @param {string} id Id ข้าราชการฯที่ได้รับทุนการศึกษา ADMIN
*/
@Get("admin/{id}")
async GetDevelopemtScholarshipAdminById(@Request() request: RequestWithUser, @Path() id: string) {
let _workflow = await new permission().Workflow(request, id, "SYS_DEV_SCHOLARSHIP");
if (_workflow == false) await new permission().PermissionGet(request, "SYS_DEV_SCHOLARSHIP");
const getDevelopment = await this.developmentScholarshipRepository.findOne({
relations: ["posLevel", "posType", "posLevelguarantor", "posTypeguarantor"],
where: { id: id },
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้");
}
const formattedData = {
rootId: getDevelopment.rootId ? getDevelopment.rootId : null,
root: getDevelopment.root ? getDevelopment.root : null,
org: getDevelopment.org ? getDevelopment.org : null,
orgRootShortName: getDevelopment.orgRootShortName ? getDevelopment.orgRootShortName : null,
orgRevisionId: getDevelopment.orgRevisionId ? getDevelopment.orgRevisionId : 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,
guarantorRootId: getDevelopment.guarantorRootId ? getDevelopment.guarantorRootId : null,
guarantorRoot: getDevelopment.guarantorRoot ? getDevelopment.guarantorRoot : null,
guarantorOrg: getDevelopment.guarantorOrg ? getDevelopment.guarantorOrg : null,
guarantorOrgRootShortName: getDevelopment.guarantorOrgRootShortName
? getDevelopment.guarantorOrgRootShortName
: null,
guarantorOrgRevisionId: getDevelopment.guarantorOrgRevisionId
? getDevelopment.guarantorOrgRevisionId
: 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 : null,
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,
@Query("type") type?: string | null,
@Query("year") year?: number | null,
) {
const getDevelopment = await AppDataSource.getRepository(DevelopmentScholarship)
.createQueryBuilder("developmentScholarship")
.andWhere("developmentScholarship.profileId = :profileId", { profileId: profileId })
.andWhere(
year !== 0 && year != null && year != undefined
? "developmentScholarship.scholarshipYear = :scholarshipYear"
: "1=1",
{ scholarshipYear: year },
)
.andWhere(
type != null && type != undefined
? "developmentScholarship.scholarshipType = :scholarshipType"
: "1=1",
{ scholarshipType: type },
)
.select(["id", "scholarshipYear", "scholarshipYear", "scholarshipType", "fundType"])
.orderBy("developmentScholarship.createdAt", "DESC")
.getRawMany();
return new HttpSuccess(getDevelopment);
}
/**
* 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",
"org",
],
});
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: RequestWithUser,
) {
// await new permission().PermissionUpdate(request, "SYS_DEV_SCHOLARSHIP");
const getDevelopment = await this.developmentScholarshipRepository.findOne({
where: { id: id },
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้");
}
const before = structuredClone(getDevelopment);
Object.assign(getDevelopment, requestBody);
getDevelopment.lastUpdateUserId = request.user.sub;
getDevelopment.lastUpdateFullName = request.user.name;
getDevelopment.lastUpdatedAt = new Date();
await this.developmentScholarshipRepository.save(getDevelopment, { data: request });
setLogDataDiff(request, { before, after: 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: RequestWithUser,
) {
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;
let scholarshipType = "";
if (_status == "GRADUATE") {
if (getDevelopment.scholarshipType != null) {
switch (getDevelopment.scholarshipType.trim().toUpperCase()) {
case "DOMESTICE":
scholarshipType = "การศึกษาในประเทศ";
break;
case "NOABROAD":
scholarshipType =
"ฝึกอบรมในประเทศที่ส่งไปพัฒนากับหน่วยวงานภายนอก (หลักสูตรที่ไม่มีการไปต่างประเทศ)";
break;
case "ABROAD":
scholarshipType =
"ฝึกอบรมในประเทศที่ส่งไปพัฒนากับหน่วยวงานภายนอก (หลักสูตรที่มีการไปต่างประเทศ)";
break;
case "EXECUTIVE":
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: 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,
})
.then(async (x) => {
await this.developmentScholarshipRepository.save(getDevelopment, { data: request });
})
.catch((error) => {
console.error("ไม่สามารถบันทึกลงทะเบียนประวัติได้");
});
} else if (_status == "NOTGRADUATE") {
getDevelopment.status = _status;
getDevelopment.lastUpdateUserId = request.user.sub;
getDevelopment.lastUpdateFullName = request.user.name;
getDevelopment.lastUpdatedAt = new Date();
await this.developmentScholarshipRepository.save(getDevelopment, { data: request });
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบสถานะนี้ในระบบ");
}
return new HttpSuccess();
}
}