import { Controller, Get, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, Query, UploadedFile, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { In, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { Development, CreateDevelopment, UpdateDevelopment1, UpdateDevelopment3, UpdateDevelopment4, UpdateDevelopment5, } from "../entities/Development"; import { ActualPeople, CreateActualPeople } from "../entities/ActualPeople"; import { CreatePlannedPeople, PlannedPeople } from "../entities/PlannedPeople"; import { ActualGoal, CreateActualGoal } from "../entities/ActualGoal"; import { CreatePlannedGoal, PlannedGoal } from "../entities/PlannedGoal"; import { Province } from "../entities/Province"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { PlannedGoalPosition } from "../entities/PlannedGoalPosition"; import { DevelopmentHistory } from "../entities/DevelopmentHistory"; import { DevelopmentProjectType } from "../entities/DevelopmentProjectType"; import { CreateDevelopmentEvaluation, DevelopmentEvaluation, } from "../entities/DevelopmentEvaluation"; import { DevelopmentAddress } from "../entities/DevelopmentAddress"; import { DevelopmentProjectTechniquePlanned } from "../entities/DevelopmentProjectTechniquePlanned"; import { DevelopmentProjectTechniqueActual } from "../entities/DevelopmentProjectTechniqueActual"; import { StrategyChild1 } from "../entities/StrategyChild1"; import { StrategyChild2 } from "../entities/StrategyChild2"; import { StrategyChild3 } from "../entities/StrategyChild3"; import { StrategyChild4 } from "../entities/StrategyChild4"; import { StrategyChild5 } from "../entities/StrategyChild5"; import CallAPI from "../interfaces/call-api"; import { UseInterceptors } from "@nestjs/common"; import { FileInterceptor } from "@nestjs/platform-express"; import * as xlsx from "xlsx"; @Route("api/v1/development/main") @Tags("Development") @Security("bearerAuth") export class DevelopmentController extends Controller { private developmentRepository = AppDataSource.getRepository(Development); private developmentAddresssRepository = AppDataSource.getRepository(DevelopmentAddress); private developmentEvaluationRepository = AppDataSource.getRepository(DevelopmentEvaluation); private developmentProjectTypeRepository = AppDataSource.getRepository(DevelopmentProjectType); private developmentProjectTechniquePlannedRepository = AppDataSource.getRepository( DevelopmentProjectTechniquePlanned, ); private developmentProjectTechniqueActualRepository = AppDataSource.getRepository( DevelopmentProjectTechniqueActual, ); private developmentHistoryRepository = AppDataSource.getRepository(DevelopmentHistory); private actualPeopleRepository = AppDataSource.getRepository(ActualPeople); private plannedPeopleRepository = AppDataSource.getRepository(PlannedPeople); private actualGoalRepository = AppDataSource.getRepository(ActualGoal); private plannedGoalRepository = AppDataSource.getRepository(PlannedGoal); private plannedGoalPositionRepository = AppDataSource.getRepository(PlannedGoalPosition); private provinceRepository = AppDataSource.getRepository(Province); private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); private strategyChild1Repository = AppDataSource.getRepository(StrategyChild1); private strategyChild2Repository = AppDataSource.getRepository(StrategyChild2); private strategyChild3Repository = AppDataSource.getRepository(StrategyChild3); private strategyChild4Repository = AppDataSource.getRepository(StrategyChild4); private strategyChild5Repository = AppDataSource.getRepository(StrategyChild5); /** * 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 + 543) + " มีอยู่ในระบบแล้ว", ); } let _null: any = null; const development = Object.assign(new Development(), requestBody); switch (requestBody.strategyChildPlannedNode) { case 1: { const checkId = await this.strategyChild1Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 1", ); } development.strategyChild1PlannedId = checkId.id; development.strategyChild2ActualId = _null; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 2: { const checkId = await this.strategyChild2Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.id; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 3: { const checkId = await this.strategyChild3Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.strategyChild2Id; development.strategyChild3PlannedId = checkId.id; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 4: { const checkId = await this.strategyChild4Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 3", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.strategyChild2Id; development.strategyChild3PlannedId = checkId.strategyChild3Id; development.strategyChild4PlannedId = checkId.id; development.strategyChild5ActualId = _null; break; } case 5: { const checkId = await this.strategyChild5Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.strategyChild2Id; development.strategyChild3PlannedId = checkId.strategyChild3Id; development.strategyChild4PlannedId = checkId.strategyChild4Id; development.strategyChild5PlannedId = checkId.id; break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผน"); } switch (requestBody.strategyChildActualNode) { case 1: { const checkId = await this.strategyChild1Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 1", ); } development.strategyChild1ActualId = checkId.id; development.strategyChild2ActualId = _null; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 2: { const checkId = await this.strategyChild2Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 2", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.id; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 3: { const checkId = await this.strategyChild3Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 3", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.strategyChild2Id; development.strategyChild3ActualId = checkId.id; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 4: { const checkId = await this.strategyChild4Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 4", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.strategyChild2Id; development.strategyChild3ActualId = checkId.strategyChild3Id; development.strategyChild4ActualId = checkId.id; development.strategyChild5ActualId = _null; break; } case 5: { const checkId = await this.strategyChild5Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 5", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.strategyChild2Id; development.strategyChild3ActualId = checkId.strategyChild3Id; development.strategyChild4ActualId = checkId.strategyChild4Id; development.strategyChild5ActualId = checkId.id; break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริง"); } 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); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรม Tab1 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมTab1 # * * @param {string} id Id โครงการ */ @Put("tab1/{id}") async UpdateDevelopmentTab1( @Path() id: string, @Body() requestBody: UpdateDevelopment1, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { 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 + 543) + " มีอยู่ในระบบแล้ว", ); } let _null: any = null; switch (requestBody.strategyChildPlannedNode) { case 1: { const checkId = await this.strategyChild1Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 1", ); } development.strategyChild1PlannedId = checkId.id; development.strategyChild2ActualId = _null; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 2: { const checkId = await this.strategyChild2Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.id; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 3: { const checkId = await this.strategyChild3Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.strategyChild2Id; development.strategyChild3PlannedId = checkId.id; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 4: { const checkId = await this.strategyChild4Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 3", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.strategyChild2Id; development.strategyChild3PlannedId = checkId.strategyChild3Id; development.strategyChild4PlannedId = checkId.id; development.strategyChild5ActualId = _null; break; } case 5: { const checkId = await this.strategyChild5Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 2", ); } development.strategyChild1PlannedId = checkId.strategyChild1Id; development.strategyChild2PlannedId = checkId.strategyChild2Id; development.strategyChild3PlannedId = checkId.strategyChild3Id; development.strategyChild4PlannedId = checkId.strategyChild4Id; development.strategyChild5PlannedId = checkId.id; break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผน"); } switch (requestBody.strategyChildActualNode) { case 1: { const checkId = await this.strategyChild1Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 1", ); } development.strategyChild1ActualId = checkId.id; development.strategyChild2ActualId = _null; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 2: { const checkId = await this.strategyChild2Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 2", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.id; development.strategyChild3ActualId = _null; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 3: { const checkId = await this.strategyChild3Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 3", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.strategyChild2Id; development.strategyChild3ActualId = checkId.id; development.strategyChild4ActualId = _null; development.strategyChild5ActualId = _null; break; } case 4: { const checkId = await this.strategyChild4Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 4", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.strategyChild2Id; development.strategyChild3ActualId = checkId.strategyChild3Id; development.strategyChild4ActualId = checkId.id; development.strategyChild5ActualId = _null; break; } case 5: { const checkId = await this.strategyChild5Repository.findOne({ where: { id: requestBody.strategyChildActualId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริงระดับ 5", ); } development.strategyChild1ActualId = checkId.strategyChild1Id; development.strategyChild2ActualId = checkId.strategyChild2Id; development.strategyChild3ActualId = checkId.strategyChild3Id; development.strategyChild4ActualId = checkId.strategyChild4Id; development.strategyChild5ActualId = checkId.id; break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริง"); } Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentRepository.save(development); return new HttpSuccess(development.id); } /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-1 * * @summary DEV_00 - เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-1 # * * @param {string} id Id โครงการ */ @Put("tab2_1_add/{id}") async CreateDevelopmenttab2_1( @Path() id: string, @Body() requestBody: CreatePlannedGoal, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const data = Object.assign(new PlannedGoal(), { ...requestBody, positions: [] }); 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); await Promise.all( requestBody.positions.map(async (x) => { const _data = Object.assign(new PlannedGoalPosition(), 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, "ไม่พบข้อมูลระดับตำแหน่ง"); } } _data.createdUserId = request.user.sub; _data.createdFullName = request.user.name; _data.lastUpdateUserId = request.user.sub; _data.lastUpdateFullName = request.user.name; _data.plannedGoalId = data.id; await this.plannedGoalPositionRepository.save(_data); }), ); return new HttpSuccess(data.id); } /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-2 * * @summary DEV_00 - เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-2 # * * @param {string} id Id โครงการ */ @Put("tab2_2_add/{id}") async CreateDevelopmenttab2_2( @Path() id: string, @Body() requestBody: CreatePlannedPeople, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const data = Object.assign(new PlannedPeople(), requestBody); 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); return new HttpSuccess(data.id); } /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-3 * * @summary DEV_00 - เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-3 # * * @param {string} id Id โครงการ */ @Put("tab2_3_add/{id}") async CreateDevelopmenttab2_3( @Path() id: string, @Body() requestBody: CreateActualGoal, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } if (requestBody.posTypeActualId != null) { const checkId = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); } } if (requestBody.posLevelActualId != null) { const checkId = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); } } const data = Object.assign(new ActualGoal(), requestBody); 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); return new HttpSuccess(data.id); } /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-4 * * @summary DEV_00 - เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab2-4 # * * @param {string} id Id โครงการ */ @Put("tab2_4_add/{id}") async CreateDevelopmenttab2_4( @Path() id: string, @Body() requestBody: CreateActualPeople, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const data = Object.assign(new ActualPeople(), requestBody); 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); return new HttpSuccess(data.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-1 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-1 # * * @param {string} id Id รายการ */ @Put("tab2_1_edit/{id}") async UpdateDevelopmenttab2_1( @Path() id: string, @Body() requestBody: CreatePlannedGoal, @Request() request: { user: Record }, ) { const development = await this.plannedGoalRepository.findOne({ where: { id }, relations: { plannedGoalPositions: true, }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } await this.plannedGoalPositionRepository.remove(development.plannedGoalPositions); Object.assign(development, { ...requestBody, positions: [] }); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.plannedGoalRepository.save(development); if (requestBody.positions != null) { await Promise.all( requestBody.positions.map(async (x) => { const _data = Object.assign(new PlannedGoalPosition(), 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, "ไม่พบข้อมูลระดับตำแหน่ง"); } } _data.createdUserId = request.user.sub; _data.createdFullName = request.user.name; _data.lastUpdateUserId = request.user.sub; _data.lastUpdateFullName = request.user.name; _data.plannedGoalId = development.id; await this.plannedGoalPositionRepository.save(_data); }), ); } return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-2 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-2 # * * @param {string} id Id รายการ */ @Put("tab2_2_edit/{id}") async UpdateDevelopmenttab2_2( @Path() id: string, @Body() requestBody: CreatePlannedPeople, @Request() request: { user: Record }, ) { const development = await this.plannedPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.plannedPeopleRepository.save(development); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-3 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-3 # * * @param {string} id Id รายการ */ @Put("tab2_3_edit/{id}") async UpdateDevelopmenttab2_3( @Path() id: string, @Body() requestBody: CreateActualGoal, @Request() request: { user: Record }, ) { const development = await this.actualGoalRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } if (requestBody.posTypeActualId != null) { const checkId = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); } } if (requestBody.posLevelActualId != null) { const checkId = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); } } Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.actualGoalRepository.save(development); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-4 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab2-4 # * * @param {string} id Id รายการ */ @Put("tab2_4_edit/{id}") async UpdateDevelopmenttab2_4( @Path() id: string, @Body() requestBody: CreateActualPeople, @Request() request: { user: Record }, ) { const development = await this.actualPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.actualPeopleRepository.save(development); return new HttpSuccess(development.id); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรมtab2-1 * * @summary DEV_00 - ลบโครงการ/หลักสูตรการฝึกอบรมtab2-1 # * * @param {string} id Id รายการ */ @Delete("tab2_1/{id}") async DeleteDevelopmenttab2_1(@Path() id: string) { const development = await this.plannedGoalRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มเป้าหมายตามแผน"); } const _development = await this.plannedGoalPositionRepository.find({ where: { plannedGoalId: id }, }); await this.plannedGoalPositionRepository.remove(_development); await this.plannedGoalRepository.remove(development); return new HttpSuccess(development.id); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรมtab2-2 * * @summary DEV_00 - ลบโครงการ/หลักสูตรการฝึกอบรมtab2-2 # * * @param {string} id Id รายการ */ @Delete("tab2_2/{id}") async DeleteDevelopmenttab2_2(@Path() id: string) { const development = await this.plannedPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามแผน"); } await this.plannedPeopleRepository.remove(development); return new HttpSuccess(development.id); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรมtab2-3 * * @summary DEV_00 - ลบโครงการ/หลักสูตรการฝึกอบรมtab2-3 # * * @param {string} id Id รายการ */ @Delete("tab2_3/{id}") async DeleteDevelopmenttab2_3(@Path() id: string) { const development = await this.actualGoalRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มเป้าหมายตามจริง"); } await this.actualGoalRepository.remove(development); return new HttpSuccess(development.id); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรมtab2-4 * * @summary DEV_00 - ลบโครงการ/หลักสูตรการฝึกอบรมtab2-4 # * * @param {string} id Id รายการ */ @Delete("tab2_4/{id}") async DeleteDevelopmenttab2_4(@Path() id: string) { const development = await this.actualPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามจริง"); } await this.actualPeopleRepository.remove(development); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab3 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab3 # * * @param {string} id Id โครงการ */ @Put("tab3/{id}") async UpdateDevelopmentTab3( @Path() id: string, @Body() requestBody: UpdateDevelopment3, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, relations: { developmentProjectTypes: true, developmentProjectTechniquePlanneds: true, developmentProjectTechniqueActuals: true, }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } Object.assign(development, { ...requestBody, developmentProjectTypes: [], developmentProjectTechniquePlanneds: [], developmentProjectTechniqueActuals: [], }); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentRepository.save(development); await this.developmentProjectTypeRepository.remove(development.developmentProjectTypes); await this.developmentProjectTechniquePlannedRepository.remove( development.developmentProjectTechniquePlanneds, ); await this.developmentProjectTechniqueActualRepository.remove( development.developmentProjectTechniqueActuals, ); if (requestBody.developmentProjectTypes != null) { await Promise.all( requestBody.developmentProjectTypes.map(async (x) => { let data = new DevelopmentProjectType(); data.name = x; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.developmentId = development.id; await this.developmentProjectTypeRepository.save(data); }), ); } if (requestBody.developmentProjectTechniquePlanneds != null) { await Promise.all( requestBody.developmentProjectTechniquePlanneds.map(async (x) => { let data = new DevelopmentProjectTechniquePlanned(); data.name = x; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.developmentId = development.id; await this.developmentProjectTechniquePlannedRepository.save(data); }), ); } if (requestBody.developmentProjectTechniqueActuals != null) { await Promise.all( requestBody.developmentProjectTechniqueActuals.map(async (x) => { let data = new DevelopmentProjectTechniquePlanned(); data.name = x; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.developmentId = development.id; await this.developmentProjectTechniqueActualRepository.save(data); }), ); } return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab4 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab4 # * * @param {string} id Id โครงการ */ @Put("tab4/{id}") async UpdateDevelopmentTab4( @Path() id: string, @Body() requestBody: UpdateDevelopment4, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentRepository.save(development); return new HttpSuccess(development.id); } /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab4-1 * * @summary DEV_00 - เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab4-1 # * * @param {string} id Id โครงการ */ @Put("tab4_1_add/{id}") async CreateDevelopmenttab4_1( @Path() id: string, @Body() requestBody: CreateDevelopmentEvaluation, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const data = Object.assign(new DevelopmentEvaluation(), requestBody); data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.developmentId = development.id; await this.developmentEvaluationRepository.save(data); return new HttpSuccess(data.id); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรมtab4-1 * * @summary DEV_00 - ลบโครงการ/หลักสูตรการฝึกอบรมtab4-1 # * * @param {string} id Id รายการ */ @Delete("tab4_1/{id}") async DeleteDevelopmenttab4_1(@Path() id: string) { const development = await this.developmentEvaluationRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามแผน"); } await this.developmentEvaluationRepository.remove(development); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab4-1 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab4-1 # * * @param {string} id Id รายการ */ @Put("tab4_1_edit/{id}") async UpdateDevelopmenttab4_1( @Path() id: string, @Body() requestBody: CreateDevelopmentEvaluation, @Request() request: { user: Record }, ) { const development = await this.developmentEvaluationRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentEvaluationRepository.save(development); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab5 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab5 # * * @param {string} id Id โครงการ */ @Put("tab5/{id}") async UpdateDevelopmentTab5( @Path() id: string, @Body() requestBody: UpdateDevelopment5, @Request() request: { user: Record }, ) { const development = await this.developmentRepository.findOne({ where: { id }, relations: { developmentAddresss: true }, }); if (!development) { 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, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ"); } } Object.assign(development, { ...requestBody, developmentAddresss: [] }); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentRepository.save(development); await this.developmentAddresssRepository.remove(development.developmentAddresss); await Promise.all( requestBody.developmentAddresss.map(async (x) => { const data = Object.assign(new DevelopmentAddress(), x); const chkProvince = await this.provinceRepository.findOne({ where: { id: x.provinceId, }, }); if (chkProvince == null) return; data.developmentId = development.id; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; await this.developmentAddresssRepository.save(data); }), ); return new HttpSuccess(development.id); } /** * API ค้นหาโครงการ * * @summary DEV_00 - ค้นหาโครงการ # * */ @Get("search") async ListDevelopemt( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() searchField?: "year" | "projectName", @Query() searchKeyword: string = "", ) { let queryLike = "development.projectName LIKE :keyword"; if (searchField == "year") { queryLike = "development.year LIKE :keyword"; } const [record, total] = await this.developmentRepository .createQueryBuilder("development") .andWhere( searchKeyword != undefined && searchKeyword != null && searchKeyword != "" ? queryLike : "1=1", { keyword: `%${searchKeyword}%`, }, ) .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); const data = await Promise.all( record.map((_data) => { return { id: _data.id, year: _data.year, projectName: _data.projectName, dateStart: _data.dateStart, dateEnd: _data.dateEnd, totalDate: _data.totalDate, addressAcademic: _data.addressAcademic, topicAcademic: _data.topicAcademic, }; }), ); return new HttpSuccess({ data: data, total }); } /** * 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, developmentProjectTypes: true, developmentProjectTechniquePlanneds: true, developmentProjectTechniqueActuals: true, developmentEvaluations: true, developmentAddresss: true, }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } if ( development.developmentPlannedGoals != null && development.developmentPlannedGoals.length > 0 ) { const plannedGoalPosition = await this.plannedGoalPositionRepository.find({ where: { plannedGoalId: In(development.developmentPlannedGoals.map((x) => x.id)) }, }); await this.plannedGoalPositionRepository.remove(plannedGoalPosition); } 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.developmentProjectTypeRepository.remove(development.developmentProjectTypes); await this.developmentProjectTechniquePlannedRepository.remove( development.developmentProjectTechniquePlanneds, ); await this.developmentProjectTechniqueActualRepository.remove( development.developmentProjectTechniqueActuals, ); await this.developmentEvaluationRepository.remove(development.developmentEvaluations); await this.developmentAddresssRepository.remove(development.developmentAddresss); 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("year") year: number, @Query("status") status: string, @Query("root") root?: string | null, @Query("keyword") keyword?: string, ) { const [development, total] = await AppDataSource.getRepository(Development) .createQueryBuilder("development") .andWhere(year > 0 ? "development.year LIKE :year" : "1=1", { year: `${year.toString()}`, }) .andWhere(root != undefined && root != null ? "development.root LIKE :root" : "1=1", { root: `${root}`, }) .andWhere(status != undefined ? "development.status LIKE :status" : "1=1", { status: `%${status}%`, }) .andWhere(keyword != undefined ? "development.projectName LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }) .select(["development.id", "development.projectName", "development.year", "development.root"]) .orderBy("development.year", "DESC") .orderBy("development.createdAt", "DESC") .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); return new HttpSuccess({ data: development, total }); } /** * API ปิดโครงการ * * @summary DEV_00 - ปิดโครงการ # * * @param {string} id Id โครงการ */ @Get("finish/{id}") async FinishDevelopemtById( @Path() id: string, @Request() request: { user: Record }, ) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } getDevelopment.status = "FINISH"; getDevelopment.lastUpdateUserId = request.user.sub; getDevelopment.lastUpdateFullName = request.user.name; await this.developmentRepository.save(getDevelopment); return new HttpSuccess(getDevelopment); } /** * API สถานะโครงการ * * @summary DEV_00 - สถานะโครงการ # * * @param {string} id Id โครงการ */ @Get("status/{id}") async StatusDevelopemtById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, select: ["id", "status"], }); return new HttpSuccess(getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab1 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab1 # * * @param {string} id Id โครงการ */ @Get("tab1/{id}") async GetDevelopemtTab1ById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, select: [ "id", "year", "projectName", "reason", "objective", "root", "rootId", "orgRootShortName", "orgRevisionId", "strategyChild1Planned", "strategyChild2Planned", "strategyChild3Planned", "strategyChild4Planned", "strategyChild5Planned", "strategyChild1Actual", "strategyChild2Actual", "strategyChild3Actual", "strategyChild4Actual", "strategyChild5Actual", ], }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } return new HttpSuccess(getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab2 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab2 # * * @param {string} id Id โครงการ */ @Get("tab2/{id}") async GetDevelopemtTab2ById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, relations: [ "developmentActualPeoples", "developmentPlannedPeoples", "developmentActualGoals", "developmentActualGoals.posTypeActual", "developmentActualGoals.posLevelActual", "developmentActualGoals", "developmentPlannedGoals", "developmentPlannedGoals.plannedGoalPositions", "plannedGoalPositions.posTypePlanned", "plannedGoalPositions.posLevelPlanned", ], }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } let _getDevelopment: any = { id: getDevelopment.id, actualPeoples: getDevelopment.developmentActualPeoples == null ? null : getDevelopment.developmentActualPeoples.sort((a, b) => (a.groupTarget == null ? "" : a.groupTarget).localeCompare( b.groupTarget == null ? "" : b.groupTarget, ), ), plannedPeoples: getDevelopment.developmentPlannedPeoples == null ? null : getDevelopment.developmentPlannedPeoples.sort((a, b) => (a.groupTarget == null ? "" : a.groupTarget).localeCompare( b.groupTarget == null ? "" : b.groupTarget, ), ), actualGoals: getDevelopment.developmentActualGoals == null ? null : getDevelopment.developmentActualGoals .sort((a, b) => (a.groupTarget == null ? "" : a.groupTarget).localeCompare( b.groupTarget == null ? "" : b.groupTarget, ), ) .map((x) => ({ groupTarget: x.groupTarget, groupTargetSub: x.groupTargetSub, position: x.position, posTypeId: x.posTypeActualId, posType: x.posTypeActual == null ? null : x.posTypeActual.posTypeName, posLevelId: x.posLevelActualId, posLevel: x.posLevelActual == null ? null : x.posLevelActual.posLevelName, type: x.type, amount: x.amount, })), plannedGoals: getDevelopment.developmentPlannedGoals == null ? null : getDevelopment.developmentPlannedGoals .sort((a, b) => (a.groupTarget == null ? "" : a.groupTarget).localeCompare( b.groupTarget == null ? "" : b.groupTarget, ), ) .map((x) => ({ groupTarget: x.groupTarget, groupTargetSub: x.groupTargetSub, position: x.plannedGoalPositions.map((y) => ({ position: y.position, posTypeId: y.posTypePlannedId, posType: y.posTypePlanned == null ? null : y.posTypePlanned.posTypeName, posLevelId: y.posLevelPlannedId, posLevel: y.posLevelPlanned == null ? null : y.posLevelPlanned.posLevelName, })), amount: x.amount, })), }; return new HttpSuccess(_getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab3 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab3 # * * @param {string} id Id โครงการ */ @Get("tab3/{id}") async GetDevelopemtTab3ById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, relations: [ "developmentProjectTypes", "developmentProjectTechniquePlanneds", "developmentProjectTechniqueActuals", ], }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } let _getDevelopment: any = { developmentProjectTypes: getDevelopment.developmentProjectTypes.map((x) => x.name).sort(), projectModalActual: getDevelopment.projectModalActual, projectModalPlanned: getDevelopment.projectModalPlanned, isBackPlanned: getDevelopment.isBackPlanned, isHoldPlanned: getDevelopment.isHoldPlanned, projectDayBackPlanned: getDevelopment.projectDayBackPlanned, projectDayHoldPlanned: getDevelopment.projectDayHoldPlanned, projectNigthHoldPlanned: getDevelopment.projectNigthHoldPlanned, developmentProjectTechniquePlanneds: getDevelopment.developmentProjectTechniquePlanneds .map((x) => x.name) .sort(), isBackActual: getDevelopment.isBackActual, isHoldActual: getDevelopment.isHoldActual, projectDayBackActual: getDevelopment.projectDayBackActual, projectDayHoldActual: getDevelopment.projectDayHoldActual, projectNigthHoldActual: getDevelopment.projectNigthHoldActual, developmentProjectTechniqueActuals: getDevelopment.developmentProjectTechniqueActuals .map((x) => x.name) .sort(), }; return new HttpSuccess(_getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab4 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab4 # * * @param {string} id Id โครงการ */ @Get("tab4/{id}") async GetDevelopemtTab4ById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, relations: ["developmentEvaluations"], }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } let _getDevelopment = { developmentEvaluations: getDevelopment.developmentEvaluations == null ? null : getDevelopment.developmentEvaluations.sort((a, b) => (a.indicators == null ? "" : a.indicators).localeCompare( b.indicators == null ? "" : b.indicators, ), ), project: getDevelopment.project, isPassAllocate: getDevelopment.isPassAllocate, isPassNoAllocate: getDevelopment.isPassNoAllocate, isNoPass: getDevelopment.isNoPass, isBudget: getDevelopment.isBudget, isOutBudget: getDevelopment.isOutBudget, }; return new HttpSuccess(_getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab5 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab5 # * * @param {string} id Id โครงการ */ @Get("tab5/{id}") async GetDevelopemtTab5ById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, relations: ["developmentAddresss"], }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } let _getDevelopment = { dateStart: getDevelopment.dateStart, dateEnd: getDevelopment.dateEnd, totalDate: getDevelopment.totalDate, developmentAddresss: getDevelopment.developmentAddresss == null ? null : getDevelopment.developmentAddresss.sort((a, b) => (a.address == null ? "" : a.address).localeCompare( b.address == null ? "" : b.address, ), ), budget: getDevelopment.budget, budgetSub: getDevelopment.budgetSub, accept: getDevelopment.accept, receive: getDevelopment.receive, approved: getDevelopment.approved, budgetPay: getDevelopment.budgetPay, issues: getDevelopment.issues, chance: getDevelopment.chance, effects: getDevelopment.effects, riskLevel: getDevelopment.riskLevel, riskManagement: getDevelopment.riskManagement, expect: getDevelopment.expect, topicAcademic: getDevelopment.topicAcademic, addressAcademic: getDevelopment.addressAcademic, provinceActualId: getDevelopment.provinceActualId, }; return new HttpSuccess(_getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab6 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab6 # * * @param {string} id Id โครงการ */ @Get("tab6/{id}") async GetDevelopemtTab6ById(@Path() id: string) { const getDevelopment = await this.developmentHistoryRepository.find({ where: { developmentId: id }, relations: ["posLevel", "posType", "employeePosLevel", "employeePosType"], order: { isDone: "ASC", citizenId: "ASC", }, }); const _getDevelopment = getDevelopment.map((item) => ({ id: item.id, type: item.type, idcard: item.citizenId, fullName: item.prefix + item.firstName + " " + item.lastName, prefix: item.prefix, firstName: item.firstName, lastName: item.lastName, position: item.position, posTypeName: item.type == "OFFICER" ? item.posType ? item.posType.posTypeName : null : item.employeePosType ? item.employeePosType.posTypeName : null, posLevelName: item.type == "OFFICER" ? item.posLevel ? item.posLevel.posLevelName : null : item.employeePosLevel ? item.employeePosLevel.posLevelName : null, posExecutive: item.posExecutive, org: item.root, trainingDays: item.trainingDays, commandNumber: item.order, commandDate: item.dateOrder, isDone: item.isDone, })); return new HttpSuccess(_getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab6 ส่งบันทึกทะเบียนประวัติ * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab6 ส่งบันทึกทะเบียนประวัติ # * * @param {string} id Id โครงการ */ @Get("tab6/done/{id}") async GetDevelopemtTab6DoneById( @Path() id: string, @Request() request: { user: Record }, ) { const getDevelopment = await this.developmentHistoryRepository.find({ where: { developmentId: id, isDone: false, type: "OFFICER" }, relations: ["development"], }); await Promise.all( getDevelopment.map(async (x) => { const _data = Object.assign(new DevelopmentHistory(), x); await new CallAPI() .PostData(request, "org/profile/training", { profileId: x.profileId, name: x.development == null ? null : x.development.projectName, topic: x.development == null ? null : x.development.topicAcademic, yearly: x.development == null ? null : x.development.year, place: x.development == null ? null : x.development.addressAcademic, duration: x.development == null ? null : x.development.totalDate, department: x.development == null ? null : x.development.root, numberOrder: x.order, dateOrder: x.dateOrder, startDate: x.development == null ? null : x.development.dateStart, endDate: x.development == null ? null : x.development.dateEnd, isDate: true, }) .then((x) => { _data.isDone = true; }) .catch((x) => { _data.isDone = false; }); _data.lastUpdateUserId = request.user.sub; _data.lastUpdateFullName = request.user.name; await this.developmentHistoryRepository.save(_data); }), ); return new HttpSuccess(getDevelopment); } /** * API list หน่วยงาน * * @summary DEV_00 - list หน่วยงาน # * */ @Get("org/root") async GetOrgDevelopemt(@Request() request: { user: Record }) { const getOrg = await this.developmentRepository .createQueryBuilder("development") .select("development.root") .groupBy("development.root") .getRawMany(); if (getOrg.length > 0) { return new HttpSuccess(getOrg.map((x) => x.development_root)); } return new HttpSuccess(getOrg); } /** * API upload User * * @summary DEV_0 - upload User # * * @param {string} id Id โครงการ */ @Post("tab6/{id}") @UseInterceptors(FileInterceptor("file")) async UploadUserDevelopemtById( @Path() id: string, @UploadedFile() file: Express.Multer.File, @Request() request: { user: Record }, ) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, relations: { developmentHistorys: true, }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const workbook = xlsx.read(file.buffer, { type: "buffer" }); const sheetName = workbook.SheetNames[0]; // Assuming we're reading the first sheet const sheet = workbook.Sheets[sheetName]; const getDevelopments = xlsx.utils.sheet_to_json(sheet); await Promise.all( getDevelopments.map(async (item: any) => { if (item["รหัสประจำตัวประชาชน"] == undefined || item["รหัสประจำตัวประชาชน"].length != 13) return; const oldProfile = getDevelopment.developmentHistorys.find( (x) => x.citizenId == item["รหัสประจำตัวประชาชน"], ); if (oldProfile != null) return; if (item["ประเภท"] == undefined) return; if (item["ประเภท"] == "ข้าราชการกรุงเทพมหานครสามัญ") { await new CallAPI() .GetData(request, `org/unauthorize/officer/citizen/${item["รหัสประจำตัวประชาชน"]}`) .then(async (x: any) => { let development = Object.assign(new DevelopmentHistory(), x); development.order = item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"] == undefined ? null : item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"]; development.dateOrder = item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"] == undefined ? null : new Date(item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"]); development.trainingDays = item["จำนวนวันที่อบรม"] == undefined ? null : item["จำนวนวันที่อบรม"]; development.developmentId = id; 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); }) .catch((x) => { return; }); } else { await new CallAPI() .GetData(request, `org/unauthorize/employee/citizen/${item["รหัสประจำตัวประชาชน"]}`) .then(async (x: any) => { let development = Object.assign(new DevelopmentHistory(), x); development.order = item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"] == undefined ? null : item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"]; development.dateOrder = item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"] == undefined ? null : new Date(item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"]); development.trainingDays = item["จำนวนวันที่อบรม"] == undefined ? null : item["จำนวนวันที่อบรม"]; development.posLevelId = null; development.posTypeId = null; development.employeePosLevelId = x.posLevelId; development.employeePosTypeId = x.posTypeId; development.developmentId = id; 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); }) .catch((x) => { return; }); } }), ); return new HttpSuccess(getDevelopments); } }