241 lines
8.4 KiB
TypeScript
241 lines
8.4 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,
|
|
} from "../entities/DevelopmentScholarship";
|
|
import { PosType } from "../entities/PosType";
|
|
import { PosLevel } from "../entities/PosLevel";
|
|
|
|
@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<string, any> },
|
|
) {
|
|
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<string, any> },
|
|
) {
|
|
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({
|
|
where: { id: id },
|
|
});
|
|
if (!getDevelopment) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทุนการศึกษา/ฝึกอบรมนี้");
|
|
}
|
|
|
|
return new HttpSuccess(getDevelopment);
|
|
}
|
|
}
|