import { Controller, Get, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, Query, Example, } 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, 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"; @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); /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรม * * @summary DEV_001 - เพิ่มโครงการ/หลักสูตรการฝึกอบรม#1 * */ @Post() async CreateDevelopment( @Body() requestBody: CreateDevelopment, @Request() request: { user: Record }, ) { const chk_name = await this.developmentRepository.find({ where: { projectName: requestBody.projectName, year: requestBody.year, }, }); if (chk_name.length > 0) { throw new HttpError( HttpStatusCode.NOT_FOUND, "โครงการ/หลักสูตรการฝึกอบรม: " + requestBody.projectName + " ปีงบประมาณ: " + requestBody.year + " มีอยู่ในระบบแล้ว", ); } 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); 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); }), ); 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 }, ) { const development = await this.developmentRepository.findOne({ where: { id }, relations: { developmentActualPeoples: true, developmentPlannedPeoples: true, developmentActualGoals: true, developmentPlannedGoals: true, }, }); 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, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ"); } } const chk_name = await this.developmentRepository.find({ where: { projectName: requestBody.projectName, year: requestBody.year, id: Not(id), }, }); if (chk_name.length > 0) { throw new HttpError( HttpStatusCode.NOT_FOUND, "โครงการ/หลักสูตรการฝึกอบรม: " + requestBody.projectName + " ปีงบประมาณ: " + requestBody.year + " มีอยู่ในระบบแล้ว", ); } Object.assign(development, requestBody); 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); }), ); 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) { 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); 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, ) { 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"]) .orderBy("development.year", "DESC") .orderBy("development.createdAt", "DESC") .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, }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } return new HttpSuccess(getDevelopment); } }