2024-04-03 12:07:57 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
Query,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
2024-04-03 20:15:58 +07:00
|
|
|
import { Brackets, Not } from "typeorm";
|
2024-04-03 12:07:57 +07:00
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import { Development } from "../entities/Development";
|
|
|
|
|
import {
|
|
|
|
|
CreateDevelopmentHistory,
|
|
|
|
|
DevelopmentHistory,
|
|
|
|
|
UpdateDevelopmentHistory,
|
|
|
|
|
} from "../entities/DevelopmentHistory";
|
|
|
|
|
import { EmployeePosType } from "../entities/EmployeePosType";
|
|
|
|
|
import { EmployeePosLevel } from "../entities/EmployeePosLevel";
|
|
|
|
|
|
|
|
|
|
@Route("api/v1/development/history/employee")
|
|
|
|
|
@Tags("DevelopmentEmployeeHistory")
|
|
|
|
|
@Security("bearerAuth")
|
2024-04-03 12:10:30 +07:00
|
|
|
export class DevelopmentEmployeeHistoryController extends Controller {
|
2024-04-03 12:07:57 +07:00
|
|
|
private developmentHistoryRepository = AppDataSource.getRepository(DevelopmentHistory);
|
|
|
|
|
private developmentRepository = AppDataSource.getRepository(Development);
|
|
|
|
|
private posTypeRepository = AppDataSource.getRepository(EmployeePosType);
|
|
|
|
|
private posLevelRepository = AppDataSource.getRepository(EmployeePosLevel);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API เพิ่มประวัติการฝึกอบรม/ดูงาน
|
|
|
|
|
*
|
|
|
|
|
* @summary DEV_006 - เพิ่มประวัติการฝึกอบรม/ดูงาน#6
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Post()
|
|
|
|
|
async CreateDevelopmentHistory(
|
|
|
|
|
@Body() requestBody: CreateDevelopmentHistory,
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const type = "EMPLOYEE";
|
|
|
|
|
const chk_name = await this.developmentHistoryRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
citizenId: requestBody.citizenId,
|
|
|
|
|
developmentId: requestBody.developmentId,
|
|
|
|
|
type: type,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (chk_name.length > 0) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประวัติการฝึกอบรม/ดูงาน มีอยู่ในระบบแล้ว");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const checkId = await this.developmentRepository.findOne({
|
|
|
|
|
where: { id: requestBody.developmentId },
|
|
|
|
|
});
|
|
|
|
|
if (!checkId) {
|
|
|
|
|
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 development = Object.assign(new DevelopmentHistory(), requestBody);
|
|
|
|
|
development.type = type;
|
|
|
|
|
development.employeePosTypeId = requestBody.posTypeId;
|
|
|
|
|
development.employeePosLevelId = requestBody.posLevelId;
|
|
|
|
|
development.createdUserId = request.user.sub;
|
|
|
|
|
development.createdFullName = request.user.name;
|
|
|
|
|
development.lastUpdateUserId = request.user.sub;
|
|
|
|
|
development.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.developmentHistoryRepository.save(development);
|
|
|
|
|
return new HttpSuccess(development.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API แก้ไขประวัติการฝึกอบรม/ดูงาน
|
|
|
|
|
*
|
|
|
|
|
* @summary DEV_007 - แก้ไขประวัติการฝึกอบรม/ดูงาน #7
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Id โครงการ
|
|
|
|
|
*/
|
|
|
|
|
@Put("{id}")
|
|
|
|
|
async UpdateDevelopmentHistory(
|
|
|
|
|
@Path() id: string,
|
|
|
|
|
@Body() requestBody: UpdateDevelopmentHistory,
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const type = "EMPLOYEE";
|
|
|
|
|
const development = await this.developmentHistoryRepository.findOne({
|
|
|
|
|
where: { id: id, type: type },
|
|
|
|
|
});
|
|
|
|
|
if (!development) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้");
|
|
|
|
|
}
|
|
|
|
|
const chk_name = await this.developmentHistoryRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
citizenId: requestBody.citizenId,
|
|
|
|
|
developmentId: requestBody.developmentId,
|
|
|
|
|
type: type,
|
|
|
|
|
id: Not(id),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (chk_name.length > 0) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประวัติการฝึกอบรม/ดูงาน มีอยู่ในระบบแล้ว");
|
|
|
|
|
}
|
|
|
|
|
const checkId = await this.developmentRepository.findOne({
|
|
|
|
|
where: { id: requestBody.developmentId },
|
|
|
|
|
});
|
|
|
|
|
if (!checkId) {
|
|
|
|
|
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.type = type;
|
|
|
|
|
development.employeePosTypeId = requestBody.posTypeId;
|
|
|
|
|
development.employeePosLevelId = requestBody.posLevelId;
|
|
|
|
|
development.lastUpdateUserId = request.user.sub;
|
|
|
|
|
development.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.developmentHistoryRepository.save(development);
|
|
|
|
|
return new HttpSuccess(development.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API ลบประวัติการฝึกอบรม/ดูงาน
|
|
|
|
|
*
|
|
|
|
|
* @summary DEV_008 - ลบประวัติการฝึกอบรม/ดูงาน #8
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Id โครงการ
|
|
|
|
|
*/
|
|
|
|
|
@Delete("{id}")
|
|
|
|
|
async DeleteDevelopmentHistory(@Path() id: string) {
|
|
|
|
|
const type = "EMPLOYEE";
|
|
|
|
|
const development = await this.developmentHistoryRepository.findOne({
|
|
|
|
|
where: { id: id, type: type },
|
|
|
|
|
});
|
|
|
|
|
if (!development) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.developmentHistoryRepository.remove(development);
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API รายการประวัติการฝึกอบรม/ดูงาน
|
|
|
|
|
*
|
|
|
|
|
* @summary DEV_009 - รายการประวัติการฝึกอบรม/ดูงาน #9
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get()
|
|
|
|
|
async GetDevelopmentHistoryLists(
|
|
|
|
|
@Query("page") page: number = 1,
|
|
|
|
|
@Query("pageSize") pageSize: number = 10,
|
|
|
|
|
@Query("keyword") keyword?: string,
|
|
|
|
|
@Query("year") year?: number,
|
|
|
|
|
) {
|
|
|
|
|
const type = "EMPLOYEE";
|
|
|
|
|
const [development, total] = await AppDataSource.getRepository(DevelopmentHistory)
|
|
|
|
|
.createQueryBuilder("developmentHistory")
|
2024-04-03 20:15:58 +07:00
|
|
|
.leftJoinAndSelect("developmentHistory.development", "development")
|
|
|
|
|
.leftJoinAndSelect("developmentHistory.employeePosLevel", "employeePosLevel")
|
|
|
|
|
.leftJoinAndSelect("developmentHistory.employeePosType", "employeePosType")
|
2024-04-04 11:31:42 +07:00
|
|
|
.andWhere(year != 0 && year != null && year != undefined ? "development.year = :year" : "1=1", { year: year })
|
2024-04-03 20:15:58 +07:00
|
|
|
.andWhere("developmentHistory.type = :type", { type: type })
|
|
|
|
|
.andWhere(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.where(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "developmentHistory.prefix LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "developmentHistory.firstName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "developmentHistory.lastName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "developmentHistory.position LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "developmentHistory.position LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "development.projectName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "employeePosType.posTypeName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
keyword != null && keyword != ""
|
|
|
|
|
? "employeePosLevel.posLevelName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
)
|
2024-04-03 12:07:57 +07:00
|
|
|
.orderBy("developmentHistory.createdAt", "DESC")
|
|
|
|
|
.skip((page - 1) * pageSize)
|
|
|
|
|
.take(pageSize)
|
|
|
|
|
.getManyAndCount();
|
2024-04-03 20:15:58 +07:00
|
|
|
|
|
|
|
|
const formattedData = development.map(item => ({
|
|
|
|
|
id: item.id,
|
|
|
|
|
citizenId: item.citizenId,
|
|
|
|
|
fullName: item.prefix+item.firstName+" "+item.lastName,
|
|
|
|
|
position: item.position,
|
2024-04-04 11:31:42 +07:00
|
|
|
posType: item.employeePosType ? item.employeePosType.posTypeName : null,
|
|
|
|
|
posLevel: item.employeePosLevel ? item.employeePosLevel.posLevelName : null,
|
2024-04-03 20:15:58 +07:00
|
|
|
projectName: item.development.projectName,
|
|
|
|
|
}));
|
2024-04-03 12:07:57 +07:00
|
|
|
|
2024-04-03 20:15:58 +07:00
|
|
|
return new HttpSuccess({ data: formattedData, total });
|
2024-04-03 12:07:57 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API รายละเอียดประวัติการฝึกอบรม/ดูงาน
|
|
|
|
|
*
|
|
|
|
|
* @summary DEV_010 - รายละเอียดประวัติการฝึกอบรม/ดูงาน #10
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Id โครงการ
|
|
|
|
|
*/
|
|
|
|
|
@Get("{id}")
|
|
|
|
|
async GetDevelopemtHistoryById(@Path() id: string) {
|
|
|
|
|
const type = "EMPLOYEE";
|
|
|
|
|
const getDevelopment = await this.developmentHistoryRepository.findOne({
|
2024-04-03 14:48:59 +07:00
|
|
|
relations: ["development"],
|
2024-04-03 12:07:57 +07:00
|
|
|
where: { id: id, type: type },
|
|
|
|
|
});
|
|
|
|
|
if (!getDevelopment) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้");
|
|
|
|
|
}
|
2024-04-03 14:48:59 +07:00
|
|
|
|
|
|
|
|
const formattedData = {
|
|
|
|
|
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,
|
2024-04-04 10:21:53 +07:00
|
|
|
posLevelId: getDevelopment.employeePosLevelId ? getDevelopment.employeePosLevelId : null,
|
|
|
|
|
posTypeId: getDevelopment.employeePosTypeId ? getDevelopment.employeePosTypeId : null,
|
2024-04-03 14:48:59 +07:00
|
|
|
developmentId: getDevelopment.developmentId ? getDevelopment.developmentId : null,
|
|
|
|
|
order: getDevelopment.order ? getDevelopment.order : null,
|
|
|
|
|
dateOrder: getDevelopment.dateOrder ? getDevelopment.dateOrder : null,
|
|
|
|
|
year: getDevelopment.development.year ? getDevelopment.development.year : null,
|
|
|
|
|
projectName: getDevelopment.development.projectName ? getDevelopment.development.projectName : null,
|
|
|
|
|
dateStart: getDevelopment.development.dateStart ? getDevelopment.development.dateStart : null,
|
|
|
|
|
dateEnd: getDevelopment.development.dateEnd ? getDevelopment.development.dateEnd : null,
|
|
|
|
|
totalDate: getDevelopment.development.totalDate ? getDevelopment.development.totalDate : null,
|
|
|
|
|
addressAcademic: getDevelopment.development.addressAcademic ? getDevelopment.development.addressAcademic : null,
|
|
|
|
|
topicAcademic: getDevelopment.development.topicAcademic ? getDevelopment.development.topicAcademic : null,
|
2024-04-03 17:08:39 +07:00
|
|
|
dateStudyStart: getDevelopment.development.dateStudyStart ? getDevelopment.development.dateStudyStart : null,
|
|
|
|
|
dateStudyEnd: getDevelopment.development.dateStudyEnd ? getDevelopment.development.dateStudyEnd : null,
|
2024-04-03 14:48:59 +07:00
|
|
|
org: null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess(formattedData);
|
2024-04-03 12:07:57 +07:00
|
|
|
}
|
|
|
|
|
}
|