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

227 lines
8.6 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { 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";
@Route("api/v1/development/history/employee")
@Tags("DevelopmentEmployeeHistory")
@Security("bearerAuth")
export class DevelopmentHistoryController extends Controller {
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")
// .andWhere(year == null ? "developmentHistory.year LIKE :year" : "1=1", { year: `${year}` })
// .orWhere("developmentHistory.projectName LIKE :keyword", { keyword: `${keyword}` })
// .select(["development.id", "development.projectName", "development.year"])
// .orderBy("developmentHistory.year", "DESC")
.orderBy("developmentHistory.createdAt", "DESC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ data: development, total });
}
/**
* 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({
where: { id: id, type: type },
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้");
}
return new HttpSuccess(getDevelopment);
}
}