import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Body, Path, Request, Example, SuccessResponse, Response, } from "tsoa"; import { CreateOrgRevision, OrgRevision } from "../entities/OrgRevision"; 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 { In, Not } from "typeorm"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; @Route("api/v1/org") @Tags("Organization") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrganizationController extends Controller { private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); /** * API รายการประวัติโครงสร้าง * * @summary ORG_020 - รายการประวัติโครงสร้าง #21 * */ @Get("history") async GetHistory() { try { const orgRevision = await this.orgRevisionRepository.find({ select: [ "id", "orgRevisionName", "orgRevisionIsCurrent", "orgRevisionCreatedAt", "orgRevisionIsDraft", ], order: { orgRevisionCreatedAt: "DESC" }, }); if (!orgRevision) { return new HttpSuccess([]); } const mapOrgRevisions = orgRevision.map((revision) => ({ orgRevisionId: revision.id, orgRevisionName: revision.orgRevisionName, orgRevisionIsCurrent: revision.orgRevisionIsCurrent, orgRevisionCreatedAt: revision.orgRevisionCreatedAt, orgRevisionIsDraft: revision.orgRevisionIsDraft, })); return new HttpSuccess(mapOrgRevisions); } catch (error) { return error; } } /** * API โครงสร้างปัจจุบันที่ใช้อยู่ * * @summary ORG_021 - โครงสร้างปัจจุบันที่ใช้อยู่ #22 * */ @Get("active") async GetActive() { try { const orgRevisionActive = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); const orgRevisionDraf = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true }, }); const mapData = { activeId: orgRevisionActive == null ? null : orgRevisionActive.id, activeName: orgRevisionActive == null ? null : orgRevisionActive.orgRevisionName, draftId: orgRevisionDraf == null ? null : orgRevisionDraf.id, draftName: orgRevisionDraf == null ? null : orgRevisionDraf.orgRevisionName, }; return new HttpSuccess(mapData); } catch (error) { return error; } } /** * API สร้างโครงสร้างระดับ4 * * @summary ORG_022 - สร้างโครงสร้างใหม่ #23 * */ @Post("draft") async CreateOrgRevision( @Body() requestBody: CreateOrgRevision, @Request() request: { user: Record }, ) { try { const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision; revision.orgRevisionIsDraft = true; revision.orgRevisionIsCurrent = false; revision.createdUserId = request.user.sub; revision.createdFullName = request.user.name; revision.lastUpdateUserId = request.user.sub; revision.lastUpdateFullName = request.user.name; await this.orgRevisionRepository.save(revision); if (requestBody.typeDraft.toUpperCase() == "ORG") { if (requestBody.orgRevisionId == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); const _revision = await this.orgRevisionRepository.findOne({ where: { id: requestBody.orgRevisionId }, }); if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); //clone data const orgRoot = await this.orgRootRepository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, }); const _orgRoot = orgRoot.map((x) => ({ ...x, isAncestorDNA: x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000" ? x.id : x.isAncestorDNA, })); await this.orgRootRepository.save(_orgRoot); _orgRoot.forEach(async (x: any) => { delete x.id; const data = Object.assign(new OrgRoot(), x); data.orgRevisionId = revision.id; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.createdAt = new Date(); data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.lastUpdatedAt = new Date(); await this.orgRootRepository.save(data); }); //clone data const orgChild1 = await this.child1Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, }); const _orgChild1 = orgChild1.map((x) => ({ ...x, isAncestorDNA: x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000" ? x.id : x.isAncestorDNA, })); await this.child1Repository.save(_orgChild1); _orgChild1.forEach(async (x: any) => { delete x.id; const data = Object.assign(new OrgChild1(), x); data.orgRevisionId = revision.id; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.createdAt = new Date(); data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.lastUpdatedAt = new Date(); await this.child1Repository.save(data); }); //clone data const orgChild2 = await this.child2Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, }); const _orgChild2 = orgChild2.map((x) => ({ ...x, isAncestorDNA: x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000" ? x.id : x.isAncestorDNA, })); await this.child2Repository.save(_orgChild2); _orgChild2.forEach(async (x: any) => { delete x.id; const data = Object.assign(new OrgChild2(), x); data.orgRevisionId = revision.id; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.createdAt = new Date(); data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.lastUpdatedAt = new Date(); await this.child2Repository.save(data); }); //clone data const orgChild3 = await this.child3Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, }); const _orgChild3 = orgChild3.map((x) => ({ ...x, isAncestorDNA: x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000" ? x.id : x.isAncestorDNA, })); await this.child3Repository.save(_orgChild3); _orgChild3.forEach(async (x: any) => { delete x.id; const data = Object.assign(new OrgChild3(), x); data.orgRevisionId = revision.id; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.createdAt = new Date(); data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.lastUpdatedAt = new Date(); await this.child3Repository.save(data); }); //clone data const orgChild4 = await this.child4Repository.find({ where: { orgRevisionId: requestBody.orgRevisionId }, }); const _orgChild4 = orgChild4.map((x) => ({ ...x, isAncestorDNA: x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000" ? x.id : x.isAncestorDNA, })); await this.child4Repository.save(_orgChild4); _orgChild4.forEach(async (x: any) => { delete x.id; const data = Object.assign(new OrgChild4(), x); data.orgRevisionId = revision.id; data.createdUserId = request.user.sub; data.createdFullName = request.user.name; data.createdAt = new Date(); data.lastUpdateUserId = request.user.sub; data.lastUpdateFullName = request.user.name; data.lastUpdatedAt = new Date(); await this.child4Repository.save(data); }); } else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { } else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { } const orgRevisions = await this.orgRevisionRepository.find({ where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }], }); await this.child4Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) }); await this.child3Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) }); await this.child2Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) }); await this.child1Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) }); await this.orgRootRepository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) }); await this.orgRevisionRepository.remove(orgRevisions); return new HttpSuccess(revision); } catch (error) { return error; } } /** * API รายละเอียดโครงสร้าง * * @summary ORG_023 - รายละเอียดโครงสร้าง (ADMIN) #25 * */ @Get("{id}") async detail(@Path() id: string) { try { const orgRevision = await this.orgRevisionRepository.findOne({ where: { id } }); if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const orgRootData = await AppDataSource.getRepository(OrgRoot) .createQueryBuilder("orgRoot") .where("orgRoot.orgRevisionId = :id", { id }) .select([ "orgRoot.id", "orgRoot.orgRootName", "orgRoot.orgRootShortName", "orgRoot.orgRootCode", "orgRoot.orgRootOrder", "orgRoot.orgRootPhoneEx", "orgRoot.orgRootPhoneIn", "orgRoot.orgRootFax", "orgRoot.orgRevisionId", "orgRoot.orgRootRank", ]) .orderBy("orgRoot.createdAt","ASC") .getMany(); const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; const orgChild1Data = orgRootIds && orgRootIds.length > 0 ? await AppDataSource.getRepository(OrgChild1) .createQueryBuilder("orgChild1") .where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds }) .select([ "orgChild1.id", "orgChild1.orgChild1Name", "orgChild1.orgChild1ShortName", "orgChild1.orgChild1Code", "orgChild1.orgChild1Order", "orgChild1.orgChild1PhoneEx", "orgChild1.orgChild1PhoneIn", "orgChild1.orgChild1Fax", "orgChild1.orgRootId", "orgChild1.orgChild1Rank", ]) .orderBy("orgChild1.createdAt","ASC") .getMany() : []; const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; const orgChild2Data = orgChild1Ids && orgChild1Ids.length > 0 ? await AppDataSource.getRepository(OrgChild2) .createQueryBuilder("orgChild2") .where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids }) .select([ "orgChild2.id", "orgChild2.orgChild2Name", "orgChild2.orgChild2ShortName", "orgChild2.orgChild2Code", "orgChild2.orgChild2Order", "orgChild2.orgChild2PhoneEx", "orgChild2.orgChild2PhoneIn", "orgChild2.orgChild2Fax", "orgChild2.orgRootId", "orgChild2.orgChild2Rank", "orgChild2.orgChild1Id", ]) .orderBy("orgChild2.createdAt","ASC") .getMany() : []; const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; const orgChild3Data = orgChild2Ids && orgChild2Ids.length > 0 ? await AppDataSource.getRepository(OrgChild3) .createQueryBuilder("orgChild3") .where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids }) .select([ "orgChild3.id", "orgChild3.orgChild3Name", "orgChild3.orgChild3ShortName", "orgChild3.orgChild3Code", "orgChild3.orgChild3Order", "orgChild3.orgChild3PhoneEx", "orgChild3.orgChild3PhoneIn", "orgChild3.orgChild3Fax", "orgChild3.orgRootId", "orgChild3.orgChild3Rank", "orgChild3.orgChild2Id", ]) .orderBy("orgChild3.createdAt","ASC") .getMany() : []; const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; const orgChild4Data = orgChild3Ids && orgChild3Ids.length > 0 ? await AppDataSource.getRepository(OrgChild4) .createQueryBuilder("orgChild4") .where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids }) .select([ "orgChild4.id", "orgChild4.orgChild4Name", "orgChild4.orgChild4ShortName", "orgChild4.orgChild4Code", "orgChild4.orgChild4Order", "orgChild4.orgChild4PhoneEx", "orgChild4.orgChild4PhoneIn", "orgChild4.orgChild4Fax", "orgChild4.orgRootId", "orgChild4.orgChild4Rank", "orgChild4.orgChild3Id", ]) .orderBy("orgChild4.createdAt","ASC") .getMany() : []; const formattedData = orgRootData.map((orgRoot) => { return { orgTreeId: orgRoot.id, orgLevel: 0, orgTreeName: orgRoot.orgRootName, orgTreeShortName: orgRoot.orgRootShortName, orgTreeCode: orgRoot.orgRootCode, orgCode: orgRoot.orgRootCode + "00", orgTreeRank: orgRoot.orgRootRank, orgTreeOrder: orgRoot.orgRootOrder, orgTreePhoneEx: orgRoot.orgRootPhoneEx, orgTreePhoneIn: orgRoot.orgRootPhoneIn, orgTreeFax: orgRoot.orgRootFax, orgRevisionId: orgRoot.orgRevisionId, children: orgChild1Data .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) .map((orgChild1) => ({ orgTreeId: orgChild1.id, orgRootId: orgRoot.id, orgLevel: 1, orgTreeName: orgChild1.orgChild1Name, orgTreeShortName: orgChild1.orgChild1ShortName, orgTreeCode: orgChild1.orgChild1Code, orgCode: orgRoot.orgRootCode + orgChild1.orgChild1Code, orgTreeRank: orgChild1.orgChild1Rank, orgTreeOrder: orgChild1.orgChild1Order, orgRootCode: orgRoot.orgRootCode, orgTreePhoneEx: orgChild1.orgChild1PhoneEx, orgTreePhoneIn: orgChild1.orgChild1PhoneIn, orgTreeFax: orgChild1.orgChild1Fax, orgRevisionId: orgRoot.orgRevisionId, children: orgChild2Data .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) .map((orgChild2) => ({ orgTreeId: orgChild2.id, orgRootId: orgChild1.id, orgLevel: 2, orgTreeName: orgChild2.orgChild2Name, orgTreeShortName: orgChild2.orgChild2ShortName, orgTreeCode: orgChild2.orgChild2Code, orgCode: orgRoot.orgRootCode + orgChild2.orgChild2Code, orgTreeRank: orgChild2.orgChild2Rank, orgTreeOrder: orgChild2.orgChild2Order, orgRootCode: orgRoot.orgRootCode, orgTreePhoneEx: orgChild2.orgChild2PhoneEx, orgTreePhoneIn: orgChild2.orgChild2PhoneIn, orgTreeFax: orgChild2.orgChild2Fax, orgRevisionId: orgRoot.orgRevisionId, children: orgChild3Data .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) .map((orgChild3) => ({ orgTreeId: orgChild3.id, orgRootId: orgChild2.id, orgLevel: 3, orgTreeName: orgChild3.orgChild3Name, orgTreeShortName: orgChild3.orgChild3ShortName, orgTreeCode: orgChild3.orgChild3Code, orgCode: orgRoot.orgRootCode + orgChild3.orgChild3Code, orgTreeRank: orgChild3.orgChild3Rank, orgTreeOrder: orgChild3.orgChild3Order, orgRootCode: orgRoot.orgRootCode, orgTreePhoneEx: orgChild3.orgChild3PhoneEx, orgTreePhoneIn: orgChild3.orgChild3PhoneIn, orgTreeFax: orgChild3.orgChild3Fax, orgRevisionId: orgRoot.orgRevisionId, children: orgChild4Data .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) .map((orgChild4) => ({ orgTreeId: orgChild4.id, orgRootId: orgChild3.id, orgLevel: 4, orgTreeName: orgChild4.orgChild4Name, orgTreeShortName: orgChild4.orgChild4ShortName, orgTreeCode: orgChild4.orgChild4Code, orgCode: orgRoot.orgRootCode + orgChild4.orgChild4Code, orgTreeRank: orgChild4.orgChild4Rank, orgTreeOrder: orgChild4.orgChild4Order, orgRootCode: orgRoot.orgRootCode, orgTreePhoneEx: orgChild4.orgChild4PhoneEx, orgTreePhoneIn: orgChild4.orgChild4PhoneIn, orgTreeFax: orgChild4.orgChild4Fax, orgRevisionId: orgRoot.orgRevisionId, })), })), })), })), }; }); return new HttpSuccess(formattedData); } catch (error) { return error; } } /** * API ตั้งเวลาเผยแพร่ * * @summary ORG_025 - ตั้งเวลาเผยแพร่ (ADMIN) #27 * * @param {string} id Id revison */ @Put("/set/publish/{id}") async Edit( @Path() id: string, @Body() requestBody: { orgPublishDate: Date }, @Request() request: { user: Record }, ) { const orgRevision = await this.orgRevisionRepository.findOne({ where: { id: id, orgRevisionIsDraft: true, orgRevisionIsCurrent: false, }, }); if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } try { orgRevision.lastUpdateUserId = request.user.sub; orgRevision.lastUpdateFullName = request.user.name; orgRevision.lastUpdatedAt = new Date(); orgRevision.orgPublishDate = requestBody.orgPublishDate; this.orgRevisionRepository.merge(orgRevision, requestBody); await this.orgRevisionRepository.save(orgRevision); return new HttpSuccess(); } catch (error) { return error; } } }