import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Body, Path, Request, Example, } from "tsoa"; import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/root") @Tags("OrgRoot") @Security("bearerAuth") export class OrgRootController extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); private orgChild1Repository = AppDataSource.getRepository(OrgChild1); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * API รายละเอียดโครงสร้างระดับ Root * * @summary ORG_016 - รายละเอียดโครงสร้างระดับ Root (ADMIN) #16 * * @param {string} id Id Root */ @Get("{id}") async GetRoot(@Path() id: string) { const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); } try { const getOrgRoot = { orgRootId: orgRoot.id, orgRootName: orgRoot.orgRootName, orgRootShortName: orgRoot.orgRootShortName, orgRootCode: orgRoot.orgRootCode, orgRootRank: orgRoot.orgRootRank, orgRootOrder: orgRoot.orgRootOrder, orgRootPhoneEx: orgRoot.orgRootPhoneEx, orgRootPhoneIn: orgRoot.orgRootPhoneIn, orgRootFax: orgRoot.orgRootFax, orgRevisionId: orgRoot.orgRevisionId, orgCode: orgRoot.orgRootCode + "00", }; return new HttpSuccess(getOrgRoot); } catch (error) { return error; } } /** * สร้างโครงสร้างระดับ Root * * @summary ORG_001 - สร้างโครงสร้างระดับ Root (ADMIN) #1 * */ @Post() @Example([ { orgRootName: "string", //ชื่อหน่วยงาน orgRootShortName: "string", //อักษรย่อ orgRootCode: "string", //รหัสหน่วยงาน orgRootPhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgRootPhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgRootFax: "string", //หมายเลขโทรสาร orgRootIsNormal: "boolean", //สถานะของหน่วยงาน }, ]) async create( // @Path() id: string, @Body() requestBody: CreateOrgRoot, @Request() request: { user: Record }, ) { const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"]; if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank"); } const orgRoot = Object.assign(new OrgRoot(), requestBody); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const chkCode = await this.orgRootRepository.findOne({ where: { orgRootCode: requestBody.orgRootCode }, }); if (chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } const orgRevision = await this.orgRevisionRepository.findOne({ where: { id: requestBody.orgRevisionId }, }); if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } if (orgRevision.orgRevisionIsDraft != true && orgRevision.orgRevisionIsCurrent != false) { throw new HttpError( HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false", ); } try { orgRoot.createdUserId = request.user.sub; orgRoot.createdFullName = request.user.name; orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateFullName = request.user.name; await this.orgRootRepository.save(orgRoot); return new HttpSuccess(); } catch (error) { return error; } } /** * แก้ไขโครงสร้างระดับ Root * * @summary ORG_002 - แก้ไขโครงสร้างระดับ Root (ADMIN) #2 * * @param {string} id Guid, *Id root */ @Put("{id}") @Example([ { orgRootName: "string", //ชื่อหน่วยงาน orgRootShortName: "string", //อักษรย่อ orgRootCode: "string", //รหัสหน่วยงาน orgRootPhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgRootPhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgRootFax: "string", //หมายเลขโทรสาร orgRootIsNormal: "boolean", //สถานะของหน่วยงาน }, ]) async update( @Path() id: string, @Body() requestBody: CreateOrgRoot, @Request() request: { user: Record }, ) { const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"]; if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank"); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: requestBody.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 chkCode = await this.orgRootRepository.findOne({ where: { orgRootCode: requestBody.orgRootCode }, }); if (chkCode?.id != id && chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } try { orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateFullName = request.user.name; orgRoot.lastUpdatedAt = new Date(); this.orgRootRepository.merge(orgRoot, requestBody); await this.orgRootRepository.save(orgRoot); return new HttpSuccess(); } catch (error) { return error; } } /** * ลบโครงสร้างระดับ Root * * @summary ORG_003 - ลบโครงสร้างระดับ Root (ADMIN) #3 * * @param {string} id Guid, *Id root */ @Delete("{id}") async delete(@Path() id: string) { const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); if (orgChild1 != null) { throw new HttpError( HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1", ); } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: orgRoot.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.orgRootRepository.remove(orgRoot); return new HttpSuccess(); } catch (error) { return error; } } }