import { AppDataSource } from "../database/data-source"; import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Path, Body, Request, Example, SuccessResponse, Response, } from "tsoa"; import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @Route("api/v1/org/child2") @Tags("OrgChild2") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild2Controller extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * API รายละเอียดโครงสร้างระดับ 2 * * @summary ORG_018 - รายละเอียดโครงสร้างระดับ2 (ADMIN) #20 * * @param {string} id Id Child2 */ @Get("{id}") async GetChild2(@Path() id: string) { try { const orgChild2 = await this.child2Repository.findOne({ where: { id } }); if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2"); } const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild2.orgRootId } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); } const getOrgChild2 = { "orgChild2Id" : orgChild2.id, "orgChild2Name" : orgChild2.orgChild2Name, "orgChild2ShortName" : orgChild2.orgChild2ShortName, "orgChild2Code" : orgChild2.orgChild2Code, "orgChild2Rank" : orgChild2.orgChild2Rank, "orgChild2Order" : orgChild2.orgChild2Order, "orgChild2PhoneEx" : orgChild2.orgChild2PhoneEx, "orgChild2PhoneIn" : orgChild2.orgChild2PhoneIn, "orgChild2Fax" : orgChild2.orgChild2Fax, "orgRevisionId" : orgChild2.orgRevisionId, "orgCode" : orgRoot.orgRootCode + orgChild2.orgChild2Code } return new HttpSuccess(getOrgChild2); } catch (error) { return error; } } /** * สร้างโครงสร้างระดับ2 Child2 * * @summary ORG_007 - สร้างโครงสร้างระดับ2 (ADMIN) #7 * */ @Post() @Example([ { orgChild2Name: "string", //ชื่อหน่วยงาน orgChild2ShortName: "string", //อักษรย่อ orgChild2Code: "string", //รหัสหน่วยงาน // orgChild2Order: "number", //ลำดับที่ของหน่วยงาน orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild2Fax: "string", //หมายเลขโทรสาร // orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน orgChild1Id: "Guid", //id Child1 }, ]) async create( @Body() requestBody: CreateOrgChild2, @Request() request: { user: Record }, ) { try { //BE ใช้ orgChild1Id หา orgChild1Id, orgRootId const child1 = await this.child1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); if (!child1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child1.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 validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); } const child2 = Object.assign(new OrgChild2(), requestBody) as OrgChild2; child2.orgChild2Name = requestBody.orgChild2Name; child2.createdUserId = request.user.sub; child2.createdFullName = request.user.name; child2.lastUpdateUserId = request.user.sub; child2.lastUpdateFullName = request.user.name; child2.orgRootId = String(child1?.orgRootId); child2.orgRevisionId = String(child1?.orgRevisionId); child2.orgChild1Id = String(child1?.id); await this.child2Repository.save(child2); return new HttpSuccess(); } catch (error) { return error; } } /** * แก้ไขโครงสร้างระดับ2 Child2 * * @summary ORG_008 - แก้ไขโครงสร้างระดับ2 (ADMIN) #8 * * @param {string} id Guid, *Id Child2 */ @Put("{id}") @Example([ { orgChild2Name: "string", //ชื่อหน่วยงาน orgChild2ShortName: "string", //อักษรย่อ orgChild2Code: "string", //รหัสหน่วยงาน // orgChild2Order: "number", //ลำดับที่ของหน่วยงาน orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild2Fax: "string", //หมายเลขโทรสาร // orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน orgChild1Id: "Guid", //id Child1 }, ]) async update( @Path() id: string, @Body() requestBody: UpdateOrgChild2, @Request() request: { user: Record }, ) { try { const child1IdExits = await this.child1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); if (!child1IdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id"); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child1IdExits.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 validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( requestBody.orgChild2Rank == null || !validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase()) ) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); } const child2 = await this.child2Repository.findOne({ where: { id } }); if (!child2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } child2.lastUpdateUserId = request.user.sub; child2.lastUpdateFullName = request.user.name; child2.lastUpdatedAt = new Date(); child2.orgRootId = String(child1IdExits?.orgRootId); child2.orgRevisionId = String(child1IdExits?.orgRevisionId); child2.orgChild1Id = String(child1IdExits?.id); this.child2Repository.merge(child2, requestBody); await this.child2Repository.save(child2); return new HttpSuccess(); } catch (error) { return error; } } /** * ลบโครงสร้างระดับ Child2 * * @summary ORG_009 - ลบโครงสร้างระดับ2 (ADMIN) #9 * * @param {string} id Guid, *Id Child2 */ @Delete("{id}") async delete(@Path() id: string) { try { const child2 = await this.child2Repository.findOne({ where: { id } }); 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 exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } }); if (exitsChild3) { throw new HttpError( HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", ); } await this.child2Repository.remove(child2); return new HttpSuccess(); } catch (error) { return error; } } }