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, UpdateDevelopment7, UpdateDevelopment8, UpdateDevelopment8_1, } 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 { CreateDevelopmentHistory, CreateDevelopmentHistoryOBO, 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"; import { addLogSequence, setLogDataDiff } from "../interfaces/utils"; import { RequestWithUser } from "../middlewares/user"; import { DevelopmentRisk, UpdateDevelopmentRisk } from "../entities/DevelopmentRisk"; import { DevelopmentOther, UpdateDevelopmentOther } from "../entities/DevelopmentOther"; @Route("api/v1/development/main") @Tags("Development") @Security("bearerAuth") export class DevelopmentController extends Controller { private developmentRepository = AppDataSource.getRepository(Development); private developmentRiskRepository = AppDataSource.getRepository(DevelopmentRisk); private developmentOtherRepository = AppDataSource.getRepository(DevelopmentOther); 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: RequestWithUser, ) { 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) + " มีอยู่ในระบบแล้ว", ); } const development = Object.assign(new Development(), requestBody); await new CallAPI() .PostData(request, "/org/find/all", { node: requestBody.node, nodeId: requestBody.nodeId, }) .then((x) => { development.root = x.root; development.rootId = x.rootId; development.rootShortName = x.rootShortName; development.child1 = x.child1; development.child1Id = x.child1Id; development.child1ShortName = x.child1ShortName; development.child2 = x.child2; development.child2Id = x.child2Id; development.child2ShortName = x.child2ShortName; development.child3 = x.child3; development.child3Id = x.child3Id; development.child3ShortName = x.child3ShortName; development.child4 = x.child4; development.child4Id = x.child4Id; development.child4ShortName = x.child4ShortName; }) .catch((error) => { console.error("Error calling API:", error); }); development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store Development.", // }); const before = null; await this.developmentRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); 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) + " มีอยู่ในระบบแล้ว", ); } const before = structuredClone(development); Object.assign(development, requestBody); await new CallAPI() .PostData(request, "/org/find/all", { node: requestBody.node, nodeId: requestBody.nodeId, }) .then((x) => { development.root = x.root; development.rootId = x.rootId; development.rootShortName = x.rootShortName; development.child1 = x.child1; development.child1Id = x.child1Id; development.child1ShortName = x.child1ShortName; development.child2 = x.child2; development.child2Id = x.child2Id; development.child2ShortName = x.child2ShortName; development.child3 = x.child3; development.child3Id = x.child3Id; development.child3ShortName = x.child3ShortName; development.child4 = x.child4; development.child4Id = x.child4Id; development.child4ShortName = x.child4ShortName; }) .catch((x) => {}); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; const _null: any = null; switch (requestBody.node) { case 0: { development.child1 = _null; development.child1Id = _null; development.child1ShortName = _null; development.child2 = _null; development.child2Id = _null; development.child2ShortName = _null; development.child3 = _null; development.child3Id = _null; development.child3ShortName = _null; development.child4 = _null; development.child4Id = _null; development.child4ShortName = _null; break; } case 1: { development.child2 = _null; development.child2Id = _null; development.child2ShortName = _null; development.child3 = _null; development.child3Id = _null; development.child3ShortName = _null; development.child4 = _null; development.child4Id = _null; development.child4ShortName = _null; break; } case 2: { development.child3 = _null; development.child3Id = _null; development.child3ShortName = _null; development.child4 = _null; development.child4Id = _null; development.child4ShortName = _null; break; } case 3: { development.child4 = _null; development.child4Id = _null; development.child4ShortName = _null; break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรม"); } // addLogSequence(request, { // action: "database", // status: "success", // description: "Store Development.", // }); await this.developmentRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); 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; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store Development.", // }); await this.plannedGoalRepository.save(data, { data: request }); 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, "ไม่พบข้อมูลระดับตำแหน่ง"); } } const before = structuredClone(development); _data.createdUserId = request.user.sub; _data.createdFullName = request.user.name; _data.lastUpdateUserId = request.user.sub; _data.lastUpdateFullName = request.user.name; _data.plannedGoalId = data.id; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store Development.", // }); await this.plannedGoalPositionRepository.save(_data, { data: request }); setLogDataDiff(request, { before, after: development }); }), ); 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); 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; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store Development.", // }); await this.plannedPeopleRepository.save(data, { data: request }); setLogDataDiff(request, { before, after: development }); 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } if (requestBody.posTypeActualId != null) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Position Type.", // }); const checkId = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); } } if (requestBody.posLevelActualId != null) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Position Level.", // }); const checkId = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); } } const before = structuredClone(development); 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; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store ActualGoal.", // }); await this.actualGoalRepository.save(data, { data: request }); setLogDataDiff(request, { before, after: development }); 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); 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; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store ActualPeople.", // }); await this.actualPeopleRepository.save(data, { data: request }); setLogDataDiff(request, { before, after: development }); 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.plannedGoalRepository.findOne({ where: { id }, relations: { plannedGoalPositions: true, }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove data in the field on the PlannedGoalPosition.", // }); await this.plannedGoalPositionRepository.remove(development.plannedGoalPositions, { data: request, }); Object.assign(development, { ...requestBody, positions: [] }); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store PlannedGoal.", // }); await this.plannedGoalRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: 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; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store Planned Goal Position.", // }); await this.plannedGoalPositionRepository.save(_data, { data: { request: request, }, }); }), ); } 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.plannedPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store PlannedPeople.", // }); await this.plannedPeopleRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.actualGoalRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } if (requestBody.posTypeActualId != null) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Position Type.", // }); const checkId = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); } } if (requestBody.posLevelActualId != null) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Position Level.", // }); const checkId = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelActualId }, }); if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); } } const before = structuredClone(development); Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store ActualGoal.", // }); await this.actualGoalRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: 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: RequestWithUser, ) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.actualPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store ActualPeople.", // }); await this.actualPeopleRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: 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, @Request() request: RequestWithUser) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); 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 }, }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove data in PlannedGoalPosition.", // }); await this.plannedGoalPositionRepository.remove(_development, { data: request }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove data in PlannedGoal.", // }); await this.plannedGoalRepository.remove(development, { data: request }); 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, @Request() request: RequestWithUser) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.plannedPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามแผน"); } // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove data in PlannedPeople.", // }); await this.plannedPeopleRepository.remove(development, { data: request }); 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, @Request() request: RequestWithUser) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.actualGoalRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มเป้าหมายตามจริง"); } // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove data in ActualGoal.", // }); await this.actualGoalRepository.remove(development, { data: request }); 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, @Request() request: RequestWithUser) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development.", // }); const development = await this.actualPeopleRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามจริง"); } // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove data in ActualPeople.", // }); await this.actualPeopleRepository.remove(development, { data: request }); 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: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, relations: { developmentProjectTypes: true, developmentProjectTechniquePlanneds: true, developmentProjectTechniqueActuals: true, developmentAddresss: true }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } Object.assign(development, { ...requestBody, developmentProjectTypes: [], developmentProjectTechniquePlanneds: [], developmentProjectTechniqueActuals: [], developmentAddresss: [] }); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentProjectTypeRepository.remove(development.developmentProjectTypes, { data: request, }); await this.developmentProjectTechniquePlannedRepository.remove( development.developmentProjectTechniquePlanneds, { data: request, }, ); await this.developmentProjectTechniqueActualRepository.remove( development.developmentProjectTechniqueActuals, { data: request, }, ); const _null: any = null; if ( requestBody.strategyChildPlannedNode != undefined && requestBody.strategyChildPlannedNode != null ) { switch (requestBody.strategyChildPlannedNode) { case 1: { if (requestBody.strategyChildPlannedId) { const checkId = await this.strategyChild1Repository.findOne({ where: { id: requestBody.strategyChildPlannedId }, }); if (!checkId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผนระดับ 1", ); } development.strategyChild1PlannedId = checkId.id; development.strategyChild2PlannedId = _null; development.strategyChild3PlannedId = _null; development.strategyChild4PlannedId = _null; development.strategyChild5PlannedId = _null; } break; } case 2: { if (requestBody.strategyChildPlannedId) { 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.strategyChild3PlannedId = _null; development.strategyChild4PlannedId = _null; development.strategyChild5PlannedId = _null; } break; } case 3: { if (requestBody.strategyChildPlannedId) { 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.strategyChild4PlannedId = _null; development.strategyChild5PlannedId = _null; } break; } case 4: { if (requestBody.strategyChildPlannedId) { 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.strategyChild5PlannedId = _null; } break; } case 5: { if (requestBody.strategyChildPlannedId) { 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, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามแผน"); } } if ( requestBody.strategyChildActualNode != undefined && requestBody.strategyChildActualNode != null ) { switch (requestBody.strategyChildActualNode) { case 1: { if (requestBody.strategyChildActualId) { 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: { if (requestBody.strategyChildActualId) { 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: { if (requestBody.strategyChildActualId) { 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: { if (requestBody.strategyChildActualId) { 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: { if (requestBody.strategyChildActualId) { 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, "ไม่พบข้อมูลยุทธศาสตร์เป้าหมายตามจริง"); } } await this.developmentRepository.save(development, { data: request }); 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, { data: request }); }), ); } 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, { data: request }); }), ); } 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, { data: request }); }), ); } //move from tab5 await this.developmentAddresssRepository.remove(development.developmentAddresss, { data: request, }); // const before = structuredClone(development); 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, { data: request }); // setLogDataDiff(request, { before, after: development }); }), ); //End 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: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); Object.assign(development, requestBody); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; await this.developmentRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: 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: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); 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, { data: request }); setLogDataDiff(request, { before, after: development }); 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, @Request() request: RequestWithUser) { // addLogSequence(request, { // action: "database", // status: "success", // description: "Get Development Evaluation By ID.", // }); const development = await this.developmentEvaluationRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้เกี่ยวข้องเป้าหมายตามแผน"); } // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove Development Evaluation By ID.", // }); await this.developmentEvaluationRepository.remove(development, { data: request }); 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: RequestWithUser, ) { 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; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentEvaluation.", // }); await this.developmentEvaluationRepository.save(development, { data: request }); 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: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, relations: { developmentAddresss: true }, }); if (!development) { 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, { data: request }); return new HttpSuccess(development.id); } /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab5-1 * * @summary DEV_00 - เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab5-1 # * * @param {string} id Id โครงการ */ @Put("tab5_1_add/{id}") async CreateDevelopmenttab5_1( @Path() id: string, @Body() requestBody: UpdateDevelopmentOther, @Request() request: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); 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, "ไม่พบข้อมูลจังหวัดข้อมูลด้านวิชาการ"); } } const before = structuredClone(development); const data = Object.assign(new DevelopmentOther(), 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.developmentOtherRepository.save(data, { data: request }); setLogDataDiff(request, { before, after: development }); return new HttpSuccess(data.id); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรมtab5-1 * * @summary DEV_00 - ลบโครงการ/หลักสูตรการฝึกอบรมtab5-1 # * * @param {string} id Id รายการ */ @Delete("tab5_1/{id}") async DeleteDevelopmenttab5_1(@Path() id: string, @Request() request: RequestWithUser) { const development = await this.developmentOtherRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลความเสี่ยงของโครงการ"); } await this.developmentOtherRepository.remove(development, { data: request }); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab5-1 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab5-1 # * * @param {string} id Id รายการ */ @Put("tab5_1_edit/{id}") async UpdateDevelopmenttab5_1( @Path() id: string, @Body() requestBody: UpdateDevelopmentOther, @Request() request: RequestWithUser, ) { const development = await this.developmentOtherRepository.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.developmentOtherRepository.save(development, { data: request }); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab7 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab7 # * * @param {string} id Id โครงการ */ @Put("tab7/{id}") async UpdateDevelopmentTab7( @Path() id: string, @Body() requestBody: UpdateDevelopment7, @Request() request: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, relations: { developmentAddresss: true }, }); 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, { data: request }); return new HttpSuccess(development.id); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab7 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab7 # * * @param {string} id Id โครงการ */ @Get("tab7/{id}") async GetDevelopemtTab7ById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ where: { id }, }); const _getDevelopment = { id: getDevelopment ? getDevelopment.id : null, accept: getDevelopment ? getDevelopment.accept : null, receive: getDevelopment ? getDevelopment.receive : null, approved: getDevelopment ? getDevelopment.approved : null, budget: getDevelopment ? getDevelopment.budget : null, budgetSub: getDevelopment ? getDevelopment.budgetSub : null, budgetPay: getDevelopment ? getDevelopment.budgetPay : null, }; return new HttpSuccess(_getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab8 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab8 # * * @param {string} id Id โครงการ */ @Get("tab8/{id}") async GetDevelopemtTab8ById(@Path() id: string) { const getDevelopment = await this.developmentRepository.findOne({ relations: ["developmentRisks"], where: { id: id }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } let _getDevelopment = { developmentRisks: getDevelopment.developmentRisks == null ? null : getDevelopment.developmentRisks.sort((a, b) => (a.id == null ? "" : a.id).localeCompare(b.id == null ? "" : b.id), ), expect: getDevelopment.expect, }; return new HttpSuccess(_getDevelopment); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab8 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรม tab8 # * * @param {string} id Id โครงการ */ @Put("tab8/{id}") async UpdateDevelopmentTab8( @Path() id: string, @Body() requestBody: UpdateDevelopment8, @Request() request: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, relations: { developmentAddresss: true }, }); 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, { data: request }); return new HttpSuccess(development.id); } /** * API เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab8-1 * * @summary DEV_00 - เพิ่มโครงการ/หลักสูตรการฝึกอบรมtab8-1 # * * @param {string} id Id โครงการ */ @Put("tab8_1_add/{id}") async CreateDevelopmenttab8_1( @Path() id: string, @Body() requestBody: UpdateDevelopmentRisk, @Request() request: RequestWithUser, ) { const development = await this.developmentRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const before = structuredClone(development); const data = Object.assign(new DevelopmentRisk(), 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.developmentRiskRepository.save(data, { data: request }); setLogDataDiff(request, { before, after: development }); return new HttpSuccess(data.id); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรมtab8-1 * * @summary DEV_00 - ลบโครงการ/หลักสูตรการฝึกอบรมtab8-1 # * * @param {string} id Id รายการ */ @Delete("tab8_1/{id}") async DeleteDevelopmenttab8_1(@Path() id: string, @Request() request: RequestWithUser) { const development = await this.developmentRiskRepository.findOne({ where: { id }, }); if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลความเสี่ยงของโครงการ"); } await this.developmentRiskRepository.remove(development, { data: request }); return new HttpSuccess(development.id); } /** * API แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab8-1 * * @summary DEV_00 - แก้ไขโครงการ/หลักสูตรการฝึกอบรมtab8-1 # * * @param {string} id Id รายการ */ @Put("tab8_1_edit/{id}") async UpdateDevelopmenttab8_1( @Path() id: string, @Body() requestBody: UpdateDevelopmentRisk, @Request() request: RequestWithUser, ) { const development = await this.developmentRiskRepository.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.developmentRiskRepository.save(development, { data: request }); 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_004 - รายการโครงการ/หลักสูตรการฝึกอบรมที่เสร็จสิ้น * */ @Get("done") async GetDevelopmentListsDone(@Query("year") year: number) { const [development, total] = await AppDataSource.getRepository(Development) .createQueryBuilder("development") .andWhere(year > 0 ? "development.year LIKE :year" : "1=1", { year: `${year.toString()}`, }) .andWhere("development.status LIKE :status", { status: `%FINISH%`, }) .select(["development.id", "development.projectName", "development.year"]) .orderBy("development.year", "DESC") .orderBy("development.createdAt", "DESC") .getManyAndCount(); return new HttpSuccess({ data: development, total }); } /** * API ลบโครงการ/หลักสูตรการฝึกอบรม * * @summary DEV_003 - ลบโครงการ/หลักสูตรการฝึกอบรม #3 * * @param {string} id Id รายการ */ @Delete("{id}") async DeleteDevelopment(@Path() id: string, @Request() request: RequestWithUser) { 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)) }, }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove PlannedGoalPosition.", // }); await this.plannedGoalPositionRepository.remove(plannedGoalPosition, { data: request }); } // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove ActualPeople.", // }); await this.actualPeopleRepository.remove(development.developmentActualPeoples, { data: request, }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove PlannedPeople.", // }); await this.plannedPeopleRepository.remove(development.developmentPlannedPeoples, { data: request, }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove ActualGoal.", // }); await this.actualGoalRepository.remove(development.developmentActualGoals, { data: request }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove PlannedGoal.", // }); await this.plannedGoalRepository.remove(development.developmentPlannedGoals, { data: request }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove DevelopmentProjectType.", // }); await this.developmentProjectTypeRepository.remove(development.developmentProjectTypes, { data: request, }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove DevelopmentProjectTechniquePlanned.", // }); await this.developmentProjectTechniquePlannedRepository.remove( development.developmentProjectTechniquePlanneds, { data: request, }, ); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove DevelopmentProjectTechniqueActuals.", // }); await this.developmentProjectTechniqueActualRepository.remove( development.developmentProjectTechniqueActuals, { data: request, }, ); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove DevelopmentEvaluation.", // }); await this.developmentEvaluationRepository.remove(development.developmentEvaluations, { data: request, }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove DevelopmentAddresss.", // }); await this.developmentAddresssRepository.remove(development.developmentAddresss, { data: request, }); // addLogSequence(request, { // action: "remove", // status: "success", // description: "Remove Development.", // }); await this.developmentRepository.remove(development, { data: request }); 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("nodeId") nodeId?: string | null, @Query("node") node?: number | 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( node != undefined && node != null ? node == 4 ? "development.child4Id LIKE :nodeId" : node == 3 ? "development.child3Id LIKE :nodeId" : node == 2 ? "development.child2Id LIKE :nodeId" : node == 1 ? "development.child1Id LIKE :nodeId" : "development.rootId LIKE :nodeId" : "1=1", { nodeId: `${nodeId}`, }, ) .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", "development.child1", "development.child2", "development.child3", "development.child4", ]) .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: RequestWithUser) { 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; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store Development.", // }); await this.developmentRepository.save(getDevelopment, { data: request }); 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 }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } let node = null; let nodeId = null; if (getDevelopment.child4Id != null) { node = 4; nodeId = getDevelopment.child4Id; } else if (getDevelopment.child3Id != null) { node = 3; nodeId = getDevelopment.child3Id; } else if (getDevelopment.child2Id != null) { node = 2; nodeId = getDevelopment.child2Id; } else if (getDevelopment.child1Id != null) { node = 1; nodeId = getDevelopment.child1Id; } else if (getDevelopment.rootId != null) { node = 0; nodeId = getDevelopment.rootId; } const formattedData = { id: getDevelopment.id, revisionId: getDevelopment.orgRevisionId, year: getDevelopment.year, projectName: getDevelopment.projectName, reason: getDevelopment.reason, objective: getDevelopment.objective, node: node, nodeId: nodeId, root: getDevelopment.rootId, child1: getDevelopment.child1Id, child2: getDevelopment.child2Id, child3: getDevelopment.child3Id, child4: getDevelopment.child4Id, }; return new HttpSuccess(formattedData); } /** * 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", "developmentPlannedGoals.plannedGoalPositions.posTypePlanned", "developmentPlannedGoals.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) => ({ id: x.id, 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) => ({ id: x.id, groupTarget: x.groupTarget, groupTargetSub: x.groupTargetSub, type: x.type, position: x.plannedGoalPositions.map((y) => ({ id: y.id, 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", "developmentAddresss", ], }); 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, reasonActual70: getDevelopment.reasonActual70, reasonActual20: getDevelopment.reasonActual20, reasonActual10: getDevelopment.reasonActual10, reasonPlanned70: getDevelopment.reasonPlanned70, reasonPlanned20: getDevelopment.reasonPlanned20, reasonPlanned10: getDevelopment.reasonPlanned10, developmentProjectTechniqueActuals: getDevelopment.developmentProjectTechniqueActuals .map((x) => x.name) .sort(), strategyChild1Planned: getDevelopment.strategyChild1PlannedId, strategyChild2Planned: getDevelopment.strategyChild2PlannedId, strategyChild3Planned: getDevelopment.strategyChild3PlannedId, strategyChild4Planned: getDevelopment.strategyChild4PlannedId, strategyChild5Planned: getDevelopment.strategyChild5PlannedId, strategyChild1Actual: getDevelopment.strategyChild1ActualId, strategyChild2Actual: getDevelopment.strategyChild2ActualId, strategyChild3Actual: getDevelopment.strategyChild3ActualId, strategyChild4Actual: getDevelopment.strategyChild4ActualId, strategyChild5Actual: getDevelopment.strategyChild5ActualId, developmentAddresss: getDevelopment.developmentAddresss == null ? null : getDevelopment.developmentAddresss .sort((a, b) => (a.id == null ? "" : a.id).localeCompare( b.id == null ? "" : b.id, ), ) }; return new HttpSuccess(_getDevelopment); } /** * API รายละเอียดโครงการ/หลักสูตรการฝึกอบรม tab3 * * @summary DEV_00 - รายละเอียดโครงการ/หลักสูตรการฝึกอบรมtab3 # * * @param {string} id Id โครงการ */ @Get("tab3_1/{id}") async GetDevelopemtTab3_1ById(@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 = { projectModalActual: getDevelopment.projectModalActual, isBackActual: getDevelopment.isBackActual, isHoldActual: getDevelopment.isHoldActual, projectDayBackActual: getDevelopment.projectDayBackActual, projectDayHoldActual: getDevelopment.projectDayHoldActual, projectNigthHoldActual: getDevelopment.projectNigthHoldActual, reasonActual70: getDevelopment.reasonActual70, reasonActual20: getDevelopment.reasonActual20, reasonActual10: getDevelopment.reasonActual10, developmentProjectTechniqueActuals: getDevelopment.developmentProjectTechniqueActuals .map((x) => x.name) .sort(), strategyChild1Actual: getDevelopment.strategyChild1ActualId, strategyChild2Actual: getDevelopment.strategyChild2ActualId, strategyChild3Actual: getDevelopment.strategyChild3ActualId, strategyChild4Actual: getDevelopment.strategyChild4ActualId, strategyChild5Actual: getDevelopment.strategyChild5ActualId, }; 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, ), ), progressTracking: getDevelopment.progressTracking, projectEvaluation: getDevelopment.projectEvaluation, }; 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({ relations: ["developmentOthers"], where: { id: id }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } let _getDevelopment = { developmentOthers: getDevelopment.developmentOthers == null ? null : getDevelopment.developmentOthers.sort((a, b) => (a.id == null ? "" : a.id).localeCompare(b.id == null ? "" : b.id), ), obstacle: getDevelopment.obstacle, suggestion: getDevelopment.suggestion, project: getDevelopment.project, isPassAllocate: getDevelopment.isPassAllocate, isPassNoAllocate: getDevelopment.isPassNoAllocate, isNoPass: getDevelopment.isNoPass, isBudget: getDevelopment.isBudget, isOutBudget: getDevelopment.isOutBudget, }; 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.org, trainingDays: item.trainingDays, commandNumber: item.order, commandDate: item.dateOrder, dateStart: item.dateStart, dateEnd: item.dateEnd, isDone: item.isDone, isProfile: item.isProfile, })); 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: RequestWithUser) { const getDevelopment = await this.developmentHistoryRepository.find({ where: { developmentId: id, isDone: false }, relations: ["development"], }); await Promise.all( getDevelopment.map(async (x) => { const _data = Object.assign(new DevelopmentHistory(), x); if (x.type == "OFFICER") { 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.trainingDays, department: x.development == null ? null : x.development.root, numberOrder: x.order, dateOrder: x.dateOrder, startDate: x.dateStart, endDate: x.dateEnd, isDate: true, }) .then((x) => { _data.isDone = true; }) .catch((x) => { _data.isDone = false; }); } else if (x.type == "EMPLOYEE") { await new CallAPI() .PostData(request, "/org/profile-employee/training", { profileEmployeeId: 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.trainingDays, department: x.development == null ? null : x.development.root, numberOrder: x.order, dateOrder: x.dateOrder, startDate: x.dateStart, endDate: x.dateEnd, isDate: true, }) .then((x) => { _data.isDone = true; }) .catch((x) => { _data.isDone = false; }); } _data.lastUpdateUserId = request.user.sub; _data.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(_data, { data: request }); }), ); return new HttpSuccess(getDevelopment); } /** * API list หน่วยงาน * * @summary DEV_00 - list หน่วยงาน # * */ @Get("org/root") async GetOrgDevelopemt() { 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: RequestWithUser, ) { 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); const before = null; await Promise.all( getDevelopments.map(async (item: any) => { if (item["รหัสประจำตัวประชาชน"] == undefined || item["รหัสประจำตัวประชาชน"].length != 13) return; const oldProfile: any = getDevelopment.developmentHistorys.find( (x) => x.citizenId == item["รหัสประจำตัวประชาชน"], ); if (oldProfile != null) { if (oldProfile.isDone == true) return; oldProfile.dateStart = item["วันที่เริ่มต้น"] == undefined ? null : item["วันที่เริ่มต้น"]; oldProfile.dateEnd = item["วันที่สิ้นสุด"] == undefined ? null : item["วันที่สิ้นสุด"]; oldProfile.order = item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"] == undefined ? null : item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"]; oldProfile.dateOrder = item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"] == undefined ? null : new Date(item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"]); oldProfile.trainingDays = item["จำนวนวันที่อบรม"] == undefined ? null : item["จำนวนวันที่อบรม"]; oldProfile.createdUserId = request.user.sub; oldProfile.createdFullName = request.user.name; oldProfile.lastUpdateUserId = request.user.sub; oldProfile.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(oldProfile, { data: request }); setLogDataDiff(request, { before, after: oldProfile }); 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.dateStart = item["วันที่เริ่มต้น"] == undefined ? null : item["วันที่เริ่มต้น"]; development.dateEnd = item["วันที่สิ้นสุด"] == undefined ? null : item["วันที่สิ้นสุด"]; development.order = item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"] == undefined ? null : item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"]; development.dateOrder = item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"] == undefined ? null : new Date(item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"]); development.trainingDays = item["จำนวนวันที่อบรม"] == undefined ? null : item["จำนวนวันที่อบรม"]; development.posLevelId = x.posLevelId; development.posTypeId = x.posTypeId; development.employeePosLevelId = null; development.employeePosTypeId = null; development.developmentId = id; development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; development.isProfile = true; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); }) .catch(async (x) => { let development = new DevelopmentHistory(); let _null: any = null; development.prefix = item["คำนำหน้า"] == undefined ? null : item["คำนำหน้า"]; development.firstName = item["ชื่อ"] == undefined ? null : item["ชื่อ"]; development.lastName = item["นามสกุล"] == undefined ? null : item["นามสกุล"]; development.position = item["ตำแหน่ง"] == undefined ? null : item["ตำแหน่ง"]; development.org = item["สังกัด"] == undefined ? null : item["สังกัด"]; development.dateStart = item["วันที่เริ่มต้น"] == undefined ? null : item["วันที่เริ่มต้น"]; development.dateEnd = item["วันที่สิ้นสุด"] == undefined ? null : item["วันที่สิ้นสุด"]; development.citizenId = item["รหัสประจำตัวประชาชน"] == undefined ? _null : item["รหัสประจำตัวประชาชน"]; development.type = "OFFICER" == undefined ? _null : "OFFICER"; development.order = item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"] == undefined ? null : item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"]; development.dateOrder = item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"] == undefined ? _null : new Date(item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"]); development.trainingDays = item["จำนวนวันที่อบรม"] == undefined ? null : item["จำนวนวันที่อบรม"]; development.posLevelId = x.posLevelId; development.posTypeId = x.posTypeId; development.employeePosLevelId = null; development.employeePosTypeId = null; development.developmentId = id; development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; development.isProfile = false; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); }); } else { await new CallAPI() .GetData(request, `/org/unauthorize/employee/citizen/${item["รหัสประจำตัวประชาชน"]}`) .then(async (x: any) => { let development = Object.assign(new DevelopmentHistory(), x); development.dateStart = item["วันที่เริ่มต้น"] == undefined ? null : item["วันที่เริ่มต้น"]; development.dateEnd = item["วันที่สิ้นสุด"] == undefined ? null : item["วันที่สิ้นสุด"]; 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; development.isProfile = true; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); }) .catch(async (x) => { let development = new DevelopmentHistory(); let _null: any = null; development.prefix = item["คำนำหน้า"] == undefined ? null : item["คำนำหน้า"]; development.firstName = item["ชื่อ"] == undefined ? null : item["ชื่อ"]; development.lastName = item["นามสกุล"] == undefined ? null : item["นามสกุล"]; development.position = item["ตำแหน่ง"] == undefined ? null : item["ตำแหน่ง"]; development.org = item["สังกัด"] == undefined ? null : item["สังกัด"]; development.dateStart = item["วันที่เริ่มต้น"] == undefined ? null : item["วันที่เริ่มต้น"]; development.dateEnd = item["วันที่สิ้นสุด"] == undefined ? null : item["วันที่สิ้นสุด"]; development.citizenId = item["รหัสประจำตัวประชาชน"] == undefined ? _null : item["รหัสประจำตัวประชาชน"]; development.type = "EMPLOYEE" == undefined ? _null : "EMPLOYEE"; development.order = item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"] == undefined ? null : item["เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"]; development.dateOrder = item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"] == undefined ? _null : new Date(item["คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่"]); development.trainingDays = item["จำนวนวันที่อบรม"] == undefined ? null : item["จำนวนวันที่อบรม"]; development.posLevelId = x.posLevelId; development.posTypeId = x.posTypeId; development.employeePosLevelId = null; development.employeePosTypeId = null; development.developmentId = id; development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; development.isProfile = false; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); }); } }), ); return new HttpSuccess(getDevelopments); } /** * API upload User One by one * * @summary DEV_0 - upload User One by one # * * @param {string} id Id โครงการ */ @Post("uploadUser/{id}") async UploadUserDevelopemtOBO( @Path() id: string, @Body() requestBody: CreateDevelopmentHistoryOBO, @Request() request: RequestWithUser, ) { const getDevelopment = await this.developmentRepository.findOne({ where: { id: id }, relations: { developmentHistorys: true, }, }); if (!getDevelopment) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } const oldProfile: any = await this.developmentHistoryRepository.findOne({ where: { citizenId: requestBody.citizenId }, }); const before = null; let status = null; let _null: any = null; if (oldProfile != null) { if (oldProfile.isDone == true) throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ข้อมูลนี้ได้ถูกบันทึกแล้ว"); oldProfile.dateStart = requestBody.dateStart == undefined ? _null : requestBody.dateStart; oldProfile.dateEnd = requestBody.dateEnd == undefined ? _null : requestBody.dateEnd; oldProfile.order = requestBody.commandNumber == undefined ? _null : requestBody.commandNumber; oldProfile.dateOrder = requestBody.commandDate == undefined ? _null : new Date(requestBody.commandDate); oldProfile.trainingDays = requestBody.trainingDays == undefined ? _null : requestBody.trainingDays; oldProfile.createdUserId = request.user.sub; oldProfile.createdFullName = request.user.name; oldProfile.lastUpdateUserId = request.user.sub; oldProfile.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(oldProfile, { data: request }); setLogDataDiff(request, { before, after: oldProfile }); status = oldProfile.isProfile; } if (requestBody.type == "OFFICER") { await new CallAPI() .GetData(request, `/org/unauthorize/officer/citizen/${requestBody.citizenId}`) .then(async (x: any) => { let development = Object.assign(new DevelopmentHistory(), x); development.dateStart = requestBody.dateStart == undefined ? _null : requestBody.dateStart; development.dateEnd = requestBody.dateEnd == undefined ? _null : requestBody.dateEnd; development.order = requestBody.commandNumber == undefined ? _null : requestBody.commandNumber; development.dateOrder = requestBody.commandDate == undefined ? _null : requestBody.commandDate; development.trainingDays = requestBody.trainingDays == undefined ? _null : requestBody.trainingDays; development.posLevelId = x.posLevelId == undefined ? _null : x.posLevelId; development.posTypeId = x.posTypeId == undefined ? _null : x.posTypeId; development.employeePosLevelId = null; development.employeePosTypeId = null; development.developmentId = id; development.isProfile = true; development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); status = development.isProfile; }) .catch(async (x) => { let development = new DevelopmentHistory(); let _null: any = null; development.prefix = requestBody.prefix == undefined ? _null : requestBody.prefix; development.firstName = requestBody.firstName == undefined ? _null : requestBody.firstName; development.lastName = requestBody.lastName == undefined ? _null : requestBody.lastName; development.position = requestBody.position == undefined ? _null : requestBody.position; development.org = requestBody.org == undefined ? _null : requestBody.org; development.dateStart = requestBody.dateStart == undefined ? _null : requestBody.dateStart; development.dateEnd = requestBody.dateEnd == undefined ? _null : requestBody.dateEnd; development.citizenId = requestBody.citizenId == undefined ? _null : requestBody.citizenId; development.type = requestBody.type == undefined ? _null : requestBody.type; development.order = requestBody.commandNumber == undefined ? _null : requestBody.commandNumber; development.dateOrder = requestBody.commandDate == undefined ? _null : requestBody.commandDate; development.trainingDays = requestBody.trainingDays == undefined ? _null : requestBody.trainingDays; development.posLevelId = x.posLevelId == undefined ? _null : x.posLevelId; development.posTypeId = x.posTypeId == undefined ? _null : x.posTypeId; development.employeePosLevelId = null; development.employeePosTypeId = null; development.developmentId = id; development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; development.isProfile = false; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); console.log(development.isProfile); status = development.isProfile; }); } else { await new CallAPI() .GetData(request, `/org/unauthorize/employee/citizen/${requestBody.citizenId}`) .then(async (x: any) => { let development = Object.assign(new DevelopmentHistory(), x); development.dateStart = requestBody.dateStart == undefined ? _null : requestBody.dateStart; development.dateEnd = requestBody.dateEnd == undefined ? _null : requestBody.dateEnd; development.order = requestBody.commandNumber == undefined ? _null : requestBody.commandNumber; development.dateOrder = requestBody.commandDate == undefined ? _null : requestBody.commandDate; development.trainingDays = requestBody.trainingDays == undefined ? _null : requestBody.trainingDays; 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; development.isProfile = true; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); status = development.isProfile; }) .catch(async (x) => { let development = new DevelopmentHistory(); let _null: any = null; development.prefix = requestBody.prefix == undefined ? _null : requestBody.prefix; development.firstName = requestBody.firstName == undefined ? _null : requestBody.firstName; development.lastName = requestBody.lastName == undefined ? _null : requestBody.lastName; development.position = requestBody.position == undefined ? _null : requestBody.position; development.org = requestBody.org == undefined ? _null : requestBody.org; development.dateStart = requestBody.dateStart == undefined ? _null : requestBody.dateStart; development.dateEnd = requestBody.dateEnd == undefined ? _null : requestBody.dateEnd; development.citizenId = requestBody.citizenId == undefined ? _null : requestBody.citizenId; development.type = requestBody.type == undefined ? _null : requestBody.type; development.order = requestBody.commandNumber == undefined ? _null : requestBody.commandNumber; development.dateOrder = requestBody.commandDate == undefined ? _null : requestBody.commandDate; development.trainingDays = requestBody.trainingDays == undefined ? _null : requestBody.trainingDays; development.posLevelId = x.posLevelId; development.posTypeId = x.posTypeId; development.employeePosLevelId = null; development.employeePosTypeId = null; development.developmentId = id; development.createdUserId = request.user.sub; development.createdFullName = request.user.name; development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; development.isProfile = false; // addLogSequence(request, { // action: "database", // status: "success", // description: "Store DevelopmentHistory.", // }); await this.developmentHistoryRepository.save(development, { data: request }); setLogDataDiff(request, { before, after: development }); status = development.isProfile; }); } return new HttpSuccess(status); } }