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

422 lines
16 KiB
TypeScript

import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { Brackets, Not } from "typeorm";
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";
import { RequestWithUser } from "../middlewares/user";
import { setLogDataDiff } from "../interfaces/utils";
import permission from "../interfaces/permission";
@Route("api/v1/development/history/employee")
@Tags("DevelopmentEmployeeHistory")
@Security("bearerAuth")
export class DevelopmentEmployeeHistoryController extends Controller {
private developmentHistoryRepository = AppDataSource.getRepository(DevelopmentHistory);
private developmentRepository = AppDataSource.getRepository(Development);
private posTypeRepository = AppDataSource.getRepository(EmployeePosType);
private posLevelRepository = AppDataSource.getRepository(EmployeePosLevel);
/**
* API list หน่วยงาน
*
* @summary DEV_00 - list หน่วยงาน #
*
*/
@Get("org/{year}")
async GetOrgDevelopemt(@Request() request: RequestWithUser, @Path() year: number) {
await new permission().PermissionList(request, "SYS_DEV_HISTORY_EMP");
const type = "EMPLOYEE";
const getOrg = await this.developmentHistoryRepository
.createQueryBuilder("developmentHistory")
.leftJoinAndSelect("developmentHistory.development", "development")
.andWhere("developmentHistory.root IS NOT NULL")
.andWhere(year > 0 ? "development.year LIKE :year" : "1=1", {
year: `${year.toString()}`,
})
.andWhere("developmentHistory.type LIKE :type", {
type: `${type}`,
})
.select("developmentHistory.root")
.groupBy("developmentHistory.root")
.getRawMany();
if (getOrg.length > 0) {
return new HttpSuccess(getOrg.map((x) => x.developmentHistory_root));
}
return new HttpSuccess(getOrg);
}
/**
* API เพิ่มประวัติการฝึกอบรม/ดูงาน
*
* @summary DEV_006 - เพิ่มประวัติการฝึกอบรม/ดูงาน#6
*
*/
@Post()
async CreateDevelopmentHistory(
@Body() requestBody: CreateDevelopmentHistory,
@Request() request: RequestWithUser,
) {
await new permission().PermissionCreate(request, "SYS_DEV_HISTORY_EMP");
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 before = null;
const development = Object.assign(new DevelopmentHistory(), requestBody);
development.type = type;
development.employeePosTypeId = requestBody.posTypeId;
development.employeePosLevelId = requestBody.posLevelId;
development.posTypeId = null;
development.posLevelId = null;
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();
// addLogSequence(request, {
// action: "database",
// status: "success",
// description: "Store DevelopmentHistory.",
// });
await this.developmentHistoryRepository.save(development, { data: request });
setLogDataDiff(request, { before, after: 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: RequestWithUser,
) {
await new permission().PermissionUpdate(request, "SYS_DEV_HISTORY_EMP");
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.posTypeId = null;
development.posLevelId = null;
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
development.lastUpdatedAt = new Date();
// addLogSequence(request, {
// action: "database",
// status: "success",
// description: "Store DevelopmentHistory.",
// });
await this.developmentHistoryRepository.save(development, { data: request });
return new HttpSuccess(development.id);
}
/**
* API ลบประวัติการฝึกอบรม/ดูงาน
*
* @summary DEV_008 - ลบประวัติการฝึกอบรม/ดูงาน #8
*
* @param {string} id Id โครงการ
*/
@Delete("{id}")
async DeleteDevelopmentHistory(@Path() id: string, @Request() request: RequestWithUser) {
await new permission().PermissionDelete(request, "SYS_DEV_HISTORY_EMP");
const type = "EMPLOYEE";
const development = await this.developmentHistoryRepository.findOne({
where: { id: id, type: type },
});
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้");
}
// addLogSequence(request, {
// action: "remove",
// status: "success",
// description: "Remove DevelopmentHistory.",
// });
await this.developmentHistoryRepository.remove(development, { data: request });
return new HttpSuccess();
}
/**
* API รายการประวัติการฝึกอบรม/ดูงาน
*
* @summary DEV_009 - รายการประวัติการฝึกอบรม/ดูงาน #9
*
*/
@Post("filter")
async GetDevelopmentHistoryLists(
@Request() request: RequestWithUser,
@Body()
body: {
page: number;
pageSize: number;
keyword?: string;
year?: number;
root: string | null;
},
) {
await new permission().PermissionList(request, "SYS_DEV_HISTORY_EMP");
const type = "EMPLOYEE";
const [development, total] = await AppDataSource.getRepository(DevelopmentHistory)
.createQueryBuilder("developmentHistory")
.leftJoinAndSelect("developmentHistory.development", "development")
.leftJoinAndSelect("developmentHistory.employeePosLevel", "employeePosLevel")
.leftJoinAndSelect("developmentHistory.employeePosType", "employeePosType")
.andWhere(
body.year != 0 && body.year != null && body.year != undefined
? "development.year = :year"
: "1=1",
{ year: body.year },
)
.andWhere(body.root != null ? "developmentHistory.root = :root" : "1=1", { root: body.root })
.andWhere("developmentHistory.type = :type", { type: type })
.andWhere(
new Brackets((qb) => {
qb.where(
body.keyword != null && body.keyword != ""
? "developmentHistory.prefix LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "developmentHistory.firstName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "developmentHistory.lastName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "developmentHistory.position LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "developmentHistory.position LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "development.projectName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "employeePosType.posTypeName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "employeePosLevel.posLevelName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "developmentHistory.citizenId LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
);
}),
)
.orderBy("developmentHistory.createdAt", "DESC")
.skip((body.page - 1) * body.pageSize)
.take(body.pageSize)
.getManyAndCount();
const formattedData = development.map((item) => ({
id: item.id,
citizenId: item.citizenId,
fullName: item.prefix + item.firstName + " " + item.lastName,
position: item.position,
year: item.development.year,
posType: item.employeePosType ? item.employeePosType.posTypeName : null,
posLevel: item.employeePosLevel ? item.employeePosLevel.posLevelName : null,
projectName: item.development.projectName,
}));
return new HttpSuccess({ data: formattedData, total });
}
/**
* API รายละเอียดประวัติการฝึกอบรม/ดูงาน
*
* @summary DEV_010 - รายละเอียดประวัติการฝึกอบรม/ดูงาน #10
*
* @param {string} id Id โครงการ
*/
@Get("{id}")
async GetDevelopemtHistoryById(@Request() request: RequestWithUser, @Path() id: string) {
await new permission().PermissionGet(request, "SYS_DEV_HISTORY_EMP");
const type = "EMPLOYEE";
const getDevelopment = await this.developmentHistoryRepository.findOne({
relations: ["development", "employeePosLevel", "employeePosType"],
where: { id: id, type: type },
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้");
}
const formattedData = {
rank: getDevelopment.rank,
org: getDevelopment.org,
prefix: getDevelopment.prefix,
firstName: getDevelopment.firstName,
lastName: getDevelopment.lastName,
citizenId: getDevelopment.citizenId,
position: getDevelopment.position,
posLevelId: getDevelopment.employeePosLevelId,
posLevelName:
getDevelopment.employeePosLevel != null
? getDevelopment.employeePosLevel.posLevelName
: null,
posTypeId: getDevelopment.employeePosTypeId,
posTypeName:
getDevelopment.employeePosType != null ? getDevelopment.employeePosType.posTypeName : null,
developmentId: getDevelopment.developmentId,
order: getDevelopment.order,
dateOrder: getDevelopment.dateOrder,
dateHisStart: getDevelopment.dateStart,
dateHisEnd: getDevelopment.dateEnd,
year: getDevelopment.development != null ? getDevelopment.development.year : null,
projectName:
getDevelopment.development != null ? getDevelopment.development.projectName : null,
dateStart: getDevelopment.development != null ? getDevelopment.development.dateStart : null,
dateEnd: getDevelopment.development != null ? getDevelopment.development.dateEnd : null,
totalDate: getDevelopment.development != null ? getDevelopment.development.totalDate : null,
addressAcademic:
getDevelopment.development != null ? getDevelopment.development.addressAcademic : null,
topicAcademic:
getDevelopment.development != null ? getDevelopment.development.topicAcademic : null,
dateStudyStart:
getDevelopment.development != null ? getDevelopment.development.dateStudyStart : null,
dateStudyEnd:
getDevelopment.development != null ? getDevelopment.development.dateStudyEnd : null,
};
return new HttpSuccess(formattedData);
}
}