import { AppDataSource } from "../database/data-source"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3, CreateOrgChild3, UpdateOrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { Body, Delete, Get, Path, Post, Put, Response, Route, SuccessResponse, Tags, Query, Request, Security, } from "tsoa"; import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; @Route("api/v1/org/child3") @Tags("OrgChild3") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild3Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * API รายละเอียดโครงสร้างระดับ 3 * * @summary ORG_019 - รายละเอียดโครงสร้างระดับ3 (ADMIN) #18 * * @param {string} id Id Child3 */ @Get("{id}") async GetChild3(@Path() id: string) { try { const orgChild3 = await this.child3Repository.findOne({ where: { id } }); if (!orgChild3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 3"); } const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild3.orgRootId } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); } const getOrgChild3 = { "orgChild3Id" : orgChild3.id, "orgChild3Name" : orgChild3.orgChild3Name, "orgChild3ShortName" : orgChild3.orgChild3ShortName, "orgChild3Code" : orgChild3.orgChild3Code, "orgChild3Rank" : orgChild3.orgChild3Rank, "orgChild3Order" : orgChild3.orgChild3Order, "orgChild3PhoneEx" : orgChild3.orgChild3PhoneEx, "orgChild3PhoneIn" : orgChild3.orgChild3PhoneIn, "orgChild3Fax" : orgChild3.orgChild3Fax, "orgRevisionId" : orgChild3.orgRevisionId, "orgCode" : orgRoot.orgRootCode + orgChild3.orgChild3Code } return new HttpSuccess(getOrgChild3); } catch (error) { return error; } } /** * API สร้างโครงสร้างระดับ3 * * @summary ORG_010 - สร้างโครงสร้างระดับ3 (ADMIN) #10 * */ @Post() async save( @Body() requestBody: CreateOrgChild3, @Request() request: { user: Record }, ) { try { //BE ใช้ orgChild2Id หา orgChild1Id, orgRootId const child2 = await this.child2Repository.findOne({ where: { id: requestBody.orgChild2Id }, }); if (!child2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child2.orgRevisionId }, }); if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); } const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild3Rank"); } const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3; child3.orgChild3Name = requestBody.orgChild3Name; child3.createdUserId = request.user.sub; child3.createdFullName = request.user.name; child3.lastUpdateUserId = request.user.sub; child3.lastUpdateFullName = request.user.name; child3.orgRootId = String(child2?.orgRootId); child3.orgChild1Id = String(child2?.orgChild1Id); child3.orgRevisionId = String(child2?.orgRevisionId); child3.orgChild2Id = String(child2?.id); await this.child3Repository.save(child3); return new HttpSuccess(); } catch (error) { return error; } } /** * API แก้ไขโครงสร้างระดับ3 * * @summary ORG_011 - แก้ไขโครงสร้างระดับ3 (ADMIN) #11 * * @param {string} id id สร้างโครงสร้างระดับ3 */ @Put("{id}") async Edit( @Path() id: string, @Body() requestBody: UpdateOrgChild3, @Request() request: { user: Record }, ) { try { const child2IdExits = await this.child2Repository.findOne({ where: { id: requestBody.orgChild2Id }, }); if (!child2IdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id"); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child2IdExits.orgRevisionId }, }); if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); } const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( requestBody.orgChild3Rank == null || !validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase()) ) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); } const child3 = await this.child3Repository.findOne({ where: { id } }); if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } child3.lastUpdateUserId = request.user.sub; child3.lastUpdateFullName = request.user.name; child3.lastUpdatedAt = new Date(); child3.orgRootId = String(child2IdExits?.orgRootId); child3.orgChild1Id = String(child2IdExits?.orgChild1Id); child3.orgRevisionId = String(child2IdExits?.orgRevisionId); child3.orgChild2Id = String(child2IdExits?.id); this.child3Repository.merge(child3, requestBody); await this.child3Repository.save(child3); return new HttpSuccess(); } catch (error) { return error; } } /** * API ลบโครงสร้างระดับ3 * * @summary ORG_012 - ลบโครงสร้างระดับ3 (ADMIN) #12 * * @param {string} id id สร้างโครงสร้างระดับ3 */ @Delete("{id}") async delete(@Path() id: string) { try { const child3 = await this.child3Repository.findOne({ where: { id } }); if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child3.orgRevisionId }, }); if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); } const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); if (exitsChild4) { throw new HttpError( HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", ); } await this.child3Repository.remove(child3); return new HttpSuccess(); } catch (error) { return error; } } }