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 { In, IsNull, Not } from "typeorm"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { PosMaster } from "../entities/PosMaster"; import { Position } from "../entities/Position"; @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 orgChild2Repository = AppDataSource.getRepository(OrgChild2); private orgChild3Repository = AppDataSource.getRepository(OrgChild3); private orgChild4Repository = AppDataSource.getRepository(OrgChild4); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private posMasterRepository = AppDataSource.getRepository(PosMaster); private positionRepository = AppDataSource.getRepository(Position); /** * 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"); } const getOrgRoot = { orgRootId: orgRoot.id, orgRootName: orgRoot.orgRootName, orgName: "-", 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); } /** * สร้างโครงสร้างระดับ 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: { orgRevisionId: requestBody.orgRevisionId, 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", ); } const order: any = await this.orgRootRepository.findOne({ where: { orgRevisionId: requestBody.orgRevisionId, }, order: { orgRootOrder: "DESC" }, }); orgRoot.createdUserId = request.user.sub; orgRoot.createdFullName = request.user.name; orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateFullName = request.user.name; orgRoot.orgRootOrder = order == null || order.orgRootOrder == null ? 1 : order.orgRootOrder + 1; await this.orgRootRepository.save(orgRoot); return new HttpSuccess(); } /** * แก้ไขโครงสร้างระดับ 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: { orgRevisionId: requestBody.orgRevisionId, 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, "ไม่พบข้อมูลโครงสร้างระดับ Root นี้", ); } 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(); } /** * ลบโครงสร้างระดับ 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, "ไม่พบข้อมูลโครงสร้างระดับ Root นี้" ); } // 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", ); } const posMasters = await this.posMasterRepository.find({ where: { orgRootId: id }, }); const positions = await this.positionRepository.find({ where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], }); await this.positionRepository.remove(positions); await this.posMasterRepository.remove(posMasters); await this.orgChild4Repository.delete({ orgRootId: id }); await this.orgChild3Repository.delete({ orgRootId: id }); await this.orgChild2Repository.delete({ orgRootId: id }); await this.orgChild1Repository.delete({ orgRootId: id }); await this.orgRootRepository.delete({ id }); return new HttpSuccess(); } }