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

400 lines
16 KiB
TypeScript
Raw Normal View History

2024-04-02 17:53:45 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Query,
Example,
2024-04-02 17:53:45 +07:00
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { Not } from "typeorm";
2024-04-02 17:53:45 +07:00
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { Development, CreateDevelopment, UpdateDevelopment } from "../entities/Development";
import { ActualPeople } from "../entities/ActualPeople";
import { PlannedPeople } from "../entities/PlannedPeople";
import { ActualGoal } from "../entities/ActualGoal";
import { PlannedGoal } from "../entities/PlannedGoal";
import { Province } from "../entities/Province";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
2024-04-02 17:53:45 +07:00
@Route("api/v1/development/main")
@Tags("Development")
@Security("bearerAuth")
export class DevelopmentController extends Controller {
private developmentRepository = AppDataSource.getRepository(Development);
private actualPeopleRepository = AppDataSource.getRepository(ActualPeople);
private plannedPeopleRepository = AppDataSource.getRepository(PlannedPeople);
private actualGoalRepository = AppDataSource.getRepository(ActualGoal);
private plannedGoalRepository = AppDataSource.getRepository(PlannedGoal);
private provinceRepository = AppDataSource.getRepository(Province);
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
2024-04-02 17:53:45 +07:00
/**
* API /
*
* @summary DEV_001 - /#1
*
*/
@Post()
async CreateDevelopment(
@Body() requestBody: CreateDevelopment,
@Request() request: { user: Record<string, any> },
) {
const chk_name = await this.developmentRepository.find({
where: {
projectName: requestBody.projectName,
year: requestBody.year,
2024-04-02 17:53:45 +07:00
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " +
requestBody.projectName +
" ปีงบประมาณ: " +
requestBody.year +
" มีอยู่ในระบบแล้ว",
2024-04-02 17:53:45 +07:00
);
}
if (requestBody.provinceId != null) {
const checkId = await this.provinceRepository.findOne({
where: { id: requestBody.provinceId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลจังหวัดสถานที่ดำเนินการ");
}
}
if (requestBody.provinceActualId != null) {
const checkId = await this.provinceRepository.findOne({
where: { id: requestBody.provinceActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ");
}
}
const development = Object.assign(new Development(), requestBody);
2024-04-02 17:53:45 +07:00
development.createdUserId = request.user.sub;
development.createdFullName = request.user.name;
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
await Promise.all(
requestBody.actualPeoples.map(async (x) => {
const data = Object.assign(new ActualPeople(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentActualPeopleId = development.id;
await this.actualPeopleRepository.save(data);
}),
);
await Promise.all(
requestBody.plannedPeoples.map(async (x) => {
const data = Object.assign(new PlannedPeople(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentPlannedPeopleId = development.id;
await this.plannedPeopleRepository.save(data);
}),
);
await Promise.all(
requestBody.actualGoals.map(async (x) => {
if (x.posTypeActualId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: x.posTypeActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (x.posLevelActualId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: x.posLevelActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new ActualGoal(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentActualGoalId = development.id;
await this.actualGoalRepository.save(data);
}),
);
await Promise.all(
requestBody.plannedGoals.map(async (x) => {
if (x.posTypePlannedId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: x.posTypePlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (x.posLevelPlannedId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: x.posLevelPlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new PlannedGoal(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentPlannedGoalId = development.id;
await this.plannedGoalRepository.save(data);
}),
);
2024-04-02 17:53:45 +07:00
return new HttpSuccess(development.id);
}
/**
* API /
*
* @summary DEV_002 - / #2
*
* @param {string} id Id
*/
@Put("{id}")
async UpdateDevelopment(
@Path() id: string,
@Body() requestBody: UpdateDevelopment,
@Request() request: { user: Record<string, any> },
) {
const development = await this.developmentRepository.findOne({
where: { id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
},
});
2024-04-02 17:53:45 +07:00
if (!development) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
if (requestBody.provinceId != null) {
const checkId = await this.provinceRepository.findOne({
where: { id: requestBody.provinceId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลจังหวัดสถานที่ดำเนินการ");
}
}
if (requestBody.provinceActualId != null) {
const checkId = await this.provinceRepository.findOne({
where: { id: requestBody.provinceActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ");
}
}
2024-04-02 17:53:45 +07:00
const chk_name = await this.developmentRepository.find({
where: {
projectName: requestBody.projectName,
2024-04-02 17:53:45 +07:00
year: requestBody.year,
id: Not(id),
2024-04-02 17:53:45 +07:00
},
});
if (chk_name.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"โครงการ/หลักสูตรการฝึกอบรม: " +
requestBody.projectName +
" ปีงบประมาณ: " +
requestBody.year +
" มีอยู่ในระบบแล้ว",
2024-04-02 17:53:45 +07:00
);
}
Object.assign(development, requestBody);
2024-04-02 17:53:45 +07:00
development.lastUpdateUserId = request.user.sub;
development.lastUpdateFullName = request.user.name;
await this.developmentRepository.save(development);
await this.actualPeopleRepository.remove(development.developmentActualPeoples);
await this.plannedPeopleRepository.remove(development.developmentPlannedPeoples);
await this.actualGoalRepository.remove(development.developmentActualGoals);
await this.plannedGoalRepository.remove(development.developmentPlannedGoals);
await Promise.all(
requestBody.actualPeoples.map(async (x) => {
const data = Object.assign(new ActualPeople(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentActualPeopleId = development.id;
await this.actualPeopleRepository.save(data);
}),
);
await Promise.all(
requestBody.plannedPeoples.map(async (x) => {
const data = Object.assign(new PlannedPeople(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentPlannedPeopleId = development.id;
await this.plannedPeopleRepository.save(data);
}),
);
await Promise.all(
requestBody.actualGoals.map(async (x) => {
if (x.posTypeActualId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: x.posTypeActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (x.posLevelActualId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: x.posLevelActualId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new ActualGoal(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentActualGoalId = development.id;
await this.actualGoalRepository.save(data);
}),
);
await Promise.all(
requestBody.plannedGoals.map(async (x) => {
if (x.posTypePlannedId != null) {
const checkId = await this.posTypeRepository.findOne({
where: { id: x.posTypePlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง");
}
}
if (x.posLevelPlannedId != null) {
const checkId = await this.posLevelRepository.findOne({
where: { id: x.posLevelPlannedId },
});
if (!checkId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
}
const data = Object.assign(new PlannedGoal(), x);
data.createdUserId = request.user.sub;
data.createdFullName = request.user.name;
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.developmentPlannedGoalId = development.id;
await this.plannedGoalRepository.save(data);
}),
);
2024-04-02 17:53:45 +07:00
return new HttpSuccess(development.id);
}
/**
* API /
*
* @summary DEV_003 - / #3
*
* @param {string} id Id
*/
@Delete("{id}")
async DeleteDevelopment(@Path() id: string) {
const development = await this.developmentRepository.findOne({
where: { id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
},
});
if (!development) {
2024-04-02 17:53:45 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
await this.actualPeopleRepository.remove(development.developmentActualPeoples);
await this.plannedPeopleRepository.remove(development.developmentPlannedPeoples);
await this.actualGoalRepository.remove(development.developmentActualGoals);
await this.plannedGoalRepository.remove(development.developmentPlannedGoals);
await this.developmentRepository.remove(development);
2024-04-02 17:53:45 +07:00
return new HttpSuccess();
}
/**
* API /
*
* @summary DEV_004 - / #4
*
*/
@Get()
async GetDevelopmentLists(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
@Query("year") year?: number,
2024-04-02 17:53:45 +07:00
) {
const [development, total] = await AppDataSource.getRepository(Development)
.createQueryBuilder("development")
.andWhere(year == null ? "development.year LIKE :year" : "1=1", { year: `${year}` })
.orWhere("development.projectName LIKE :keyword", { keyword: `${keyword}` })
.select(["development.id", "development.projectName", "development.year"])
2024-04-02 17:53:45 +07:00
.orderBy("development.year", "DESC")
.orderBy("development.createdAt", "DESC")
2024-04-02 17:53:45 +07:00
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ data: development, total });
}
/**
* API /
*
* @summary DEV_005 - / #5
*
* @param {string} id Id
*/
@Get("{id}")
async GetDevelopemtById(@Path() id: string) {
const getDevelopment = await this.developmentRepository.findOne({
where: { id: id },
relations: {
developmentActualPeoples: true,
developmentPlannedPeoples: true,
developmentActualGoals: true,
developmentPlannedGoals: true,
},
2024-04-02 17:53:45 +07:00
});
if (!getDevelopment) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้");
}
return new HttpSuccess(getDevelopment);
}
}