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 { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @Route("api/v1/org/child4") @Tags("OrgChild4") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild4Controller extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * API รายละเอียดโครงสร้างระดับ 4 * * @summary ORG_019 - รายละเอียดโครงสร้างระดับ4 (ADMIN) #26 * * @param {string} id Id Child4 */ @Get("{id}") async GetChild4(@Path() id: string) { const orgChild4 = await this.child4Repository.findOne({ where: { id } }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 4"); } const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild4.orgRootId } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); } try { const getOrgChild4 = { "orgChild4Id" : orgChild4.id, "orgChild4Name" : orgChild4.orgChild4Name, "orgChild4ShortName" : orgChild4.orgChild4ShortName, "orgChild4Code" : orgChild4.orgChild4Code, "orgChild4Rank" : orgChild4.orgChild4Rank, "orgChild4Order" : orgChild4.orgChild4Order, "orgChild4PhoneEx" : orgChild4.orgChild4PhoneEx, "orgChild4PhoneIn" : orgChild4.orgChild4PhoneIn, "orgChild4Fax" : orgChild4.orgChild4Fax, "orgRevisionId" : orgChild4.orgRevisionId, "orgCode" : orgRoot.orgRootCode + orgChild4.orgChild4Code } return new HttpSuccess(getOrgChild4); } catch (error) { return error; } } /** * สร้างโครงสร้างระดับ4 Child4 * * @summary ORG_013 - สร้างโครงสร้างระดับ4 (ADMIN) #13 * */ @Post() @Example([ { orgChild4Name: "string", //ชื่อหน่วยงาน orgChild4ShortName: "string", //อักษรย่อ orgChild4Code: "string", //รหัสหน่วยงาน // orgChild4Order: "number", //ลำดับที่ของหน่วยงาน orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild4Fax: "string", //หมายเลขโทรสาร // orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน orgChild3Id: "Guid", //id Child1 }, ]) async create( @Body() requestBody: CreateOrgChild4, @Request() request: { user: Record }, ) { const child3 = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); 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 validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild4Rank"); } try { const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4; child4.orgChild4Name = requestBody.orgChild4Name; child4.createdUserId = request.user.sub; child4.createdFullName = request.user.name; child4.lastUpdateUserId = request.user.sub; child4.lastUpdateFullName = request.user.name; child4.orgRootId = String(child3?.orgRootId); child4.orgChild1Id = String(child3?.orgChild1Id); child4.orgChild2Id = String(child3?.orgChild2Id); child4.orgRevisionId = String(child3?.orgRevisionId); child4.orgChild3Id = String(child3?.id); await this.child4Repository.save(child4); return new HttpSuccess(); } catch (error) { return error; } } /** * แก้ไขโครงสร้างระดับ4 Child4 * * @summary ORG_014 - แก้ไขโครงสร้างระดับ4 (ADMIN) #14 * * @param {string} id Guid, *Id Child4 */ @Put("{id}") @Example([ { orgChild4Name: "string", //ชื่อหน่วยงาน orgChild4ShortName: "string", //อักษรย่อ orgChild4Code: "string", //รหัสหน่วยงาน // orgChild4Order: "number", //ลำดับที่ของหน่วยงาน orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild4Fax: "string", //หมายเลขโทรสาร // orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน orgChild3Id: "Guid", //id Child1 }, ]) async update( @Path() id: string, @Body() requestBody: UpdateOrgChild4, @Request() request: { user: Record }, ) { const child3IdExits = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); if (!child3IdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id"); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child3IdExits.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 validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( requestBody.orgChild4Rank == null || !validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase()) ) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); } const child4 = await this.child4Repository.findOne({ where: { id } }); if (!child4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } try { child4.lastUpdateUserId = request.user.sub; child4.lastUpdateFullName = request.user.name; child4.lastUpdatedAt = new Date(); child4.orgRootId = String(child3IdExits?.orgRootId); child4.orgChild1Id = String(child3IdExits?.orgChild1Id); child4.orgChild2Id = String(child3IdExits?.orgChild2Id); child4.orgRevisionId = String(child3IdExits?.orgRevisionId); child4.orgChild3Id = String(child3IdExits?.id); this.child4Repository.merge(child4, requestBody); await this.child4Repository.save(child4); return new HttpSuccess(); } catch (error) { return error; } } /** * ลบโครงสร้างระดับ4 Child4 * * @summary ORG_015 - ลบโครงสร้างระดับ4 (ADMIN) #15 * * @param {string} id Guid, *Id Child4 */ @Delete("{id}") async delete(@Path() id: string) { const child4 = await this.child4Repository.findOne({ where: { id } }); if (!child4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: child4.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"); } try { await this.child4Repository.remove(child4); return new HttpSuccess(); } catch (error) { return error; } } }