import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Body, Path, Request, Example, SuccessResponse, Response, Query, } 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) => { var dataId = x.id; //clone data const orgChild1 = await this.child1Repository.find({ where: { orgRootId: dataId, 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); 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); _orgChild1.forEach(async (x: any) => { var data1Id = x.id; //clone data const orgChild2 = await this.child2Repository.find({ where: { orgRootId: dataId, orgChild1Id: data1Id, 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); delete x.id; const data1 = Object.assign(new OrgChild1(), x); data1.orgRootId = data.id; data1.orgRevisionId = revision.id; data1.createdUserId = request.user.sub; data1.createdFullName = request.user.name; data1.createdAt = new Date(); data1.lastUpdateUserId = request.user.sub; data1.lastUpdateFullName = request.user.name; data1.lastUpdatedAt = new Date(); await this.child1Repository.save(data1); _orgChild2.forEach(async (x: any) => { var data2Id = x.id; //clone data const orgChild3 = await this.child3Repository.find({ where: { orgRootId: dataId, orgChild1Id: data1Id, orgChild2Id: data2Id, 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); delete x.id; const data2 = Object.assign(new OrgChild2(), x); data2.orgChild1Id = data1.id; data2.orgRootId = data.id; data2.orgRevisionId = revision.id; data2.createdUserId = request.user.sub; data2.createdFullName = request.user.name; data2.createdAt = new Date(); data2.lastUpdateUserId = request.user.sub; data2.lastUpdateFullName = request.user.name; data2.lastUpdatedAt = new Date(); await this.child2Repository.save(data2); _orgChild3.forEach(async (x: any) => { var data3Id = x.id; //clone data const orgChild4 = await this.child4Repository.find({ where: { orgRootId: dataId, orgChild1Id: data1Id, orgChild2Id: data2Id, orgChild3Id: data3Id, 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); delete x.id; const data3 = Object.assign(new OrgChild3(), x); data3.orgChild2Id = data2.id; data3.orgChild1Id = data1.id; data3.orgRootId = data.id; data3.orgRevisionId = revision.id; data3.createdUserId = request.user.sub; data3.createdFullName = request.user.name; data3.createdAt = new Date(); data3.lastUpdateUserId = request.user.sub; data3.lastUpdateFullName = request.user.name; data3.lastUpdatedAt = new Date(); await this.child3Repository.save(data3); _orgChild4.forEach(async (x: any) => { delete x.id; const data4 = Object.assign(new OrgChild4(), x); data4.orgChild3Id = data3.id; data4.orgChild2Id = data2.id; data4.orgChild1Id = data1.id; data4.orgRootId = data.id; data4.orgRevisionId = revision.id; data4.createdUserId = request.user.sub; data4.createdFullName = request.user.name; data4.createdAt = new Date(); data4.lastUpdateUserId = request.user.sub; data4.lastUpdateFullName = request.user.name; data4.lastUpdatedAt = new Date(); await this.child4Repository.save(data4); }); }); }); }); }); // //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, orgName: orgRoot.orgRootName, 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, orgRootName: orgRoot.orgRootName, children: orgChild1Data .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) .map((orgChild1) => ({ orgTreeId: orgChild1.id, orgRootId: orgRoot.id, orgLevel: 1, orgName: `${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, 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, orgRootName: orgRoot.orgRootName, children: orgChild2Data .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) .map((orgChild2) => ({ orgTreeId: orgChild2.id, orgRootId: orgChild1.id, orgLevel: 2, orgName: `${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, 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, orgRootName: orgRoot.orgRootName, children: orgChild3Data .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) .map((orgChild3) => ({ orgTreeId: orgChild3.id, orgRootId: orgChild2.id, orgLevel: 3, orgName: `${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, 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, orgRootName: orgRoot.orgRootName, children: orgChild4Data .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) .map((orgChild4) => ({ orgTreeId: orgChild4.id, orgRootId: orgChild3.id, orgLevel: 4, orgName: `${orgChild4.orgChild4Name}/${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, 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, orgRootName: orgRoot.orgRootName, })), })), })), })), }; }); 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; } } /** * API ประวัติหน่วยงาน * * @summary ORG_039 - ประวัติหน่วยงาน (ADMIN) #42 * */ @Post("/history/publish") async GetHistoryPublish( @Body() requestBody: { id: string; type: number; }, @Request() request: { user: Record }, ) { if (requestBody.type == 1) { const orgChild1 = await this.child1Repository.findOne({ where: { id: requestBody.id }, }); if (!orgChild1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1"); } const datas = await this.child1Repository.find({ where: { isAncestorDNA: orgChild1.isAncestorDNA }, relations: ["orgRevision"], order: { lastUpdatedAt: "DESC" }, }); const _data = datas.map((item) => ({ id: item.id, orgRevisionName: item.orgRevision == null ? null : item.orgRevision.orgRevisionName, name: item.orgChild1Name, lastUpdatedAt: item.lastUpdatedAt, })); return new HttpSuccess(_data); } else if (requestBody.type == 2) { const orgChild2 = await this.child2Repository.findOne({ where: { id: requestBody.id }, }); if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2"); } const datas = await this.child2Repository.find({ where: { isAncestorDNA: orgChild2.isAncestorDNA }, relations: ["orgRevision"], order: { lastUpdatedAt: "DESC" }, }); const _data = datas.map((item) => ({ id: item.id, orgRevisionName: item.orgRevision == null ? null : item.orgRevision.orgRevisionName, name: item.orgChild2Name, lastUpdatedAt: item.lastUpdatedAt, })); return new HttpSuccess(_data); } else if (requestBody.type == 3) { const orgChild3 = await this.child3Repository.findOne({ where: { id: requestBody.id }, }); if (!orgChild3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3"); } const datas = await this.child3Repository.find({ where: { isAncestorDNA: orgChild3.isAncestorDNA }, relations: ["orgRevision"], order: { lastUpdatedAt: "DESC" }, }); const _data = datas.map((item) => ({ id: item.id, orgRevisionName: item.orgRevision == null ? null : item.orgRevision.orgRevisionName, name: item.orgChild3Name, lastUpdatedAt: item.lastUpdatedAt, })); return new HttpSuccess(_data); } else if (requestBody.type == 4) { const orgChild4 = await this.child4Repository.findOne({ where: { id: requestBody.id }, }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child4"); } const datas = await this.child4Repository.find({ where: { isAncestorDNA: orgChild4.isAncestorDNA }, relations: ["orgRevision"], order: { lastUpdatedAt: "DESC" }, }); const _data = datas.map((item) => ({ id: item.id, orgRevisionName: item.orgRevision == null ? null : item.orgRevision.orgRevisionName, name: item.orgChild4Name, lastUpdatedAt: item.lastUpdatedAt, })); return new HttpSuccess(_data); } else { const orgRoot = await this.orgRootRepository.findOne({ where: { id: requestBody.id }, }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Root"); } const datas = await this.orgRootRepository.find({ where: { isAncestorDNA: orgRoot.isAncestorDNA }, relations: ["orgRevision"], order: { lastUpdatedAt: "DESC" }, }); const _data = datas.map((item) => ({ id: item.id, orgRevisionName: item.orgRevision == null ? null : item.orgRevision.orgRevisionName, name: item.orgRootName, lastUpdatedAt: item.lastUpdatedAt, })); return new HttpSuccess(_data); } } /** * API จัดลำดับโครงสร้าง * * @summary ORG_038 - จัดลำดับโครงสร้าง (ADMIN) #41 * */ @Post("sort") async Sort(@Body() requestBody: { id: string; type: number; sortId: string[] }) { try { switch (requestBody.type) { case 0: { const revisionId = await this.orgRevisionRepository.findOne({ where: { id: requestBody.id }, }); if (!revisionId?.id) { throw new HttpError( HttpStatusCode.NOT_FOUND, "not found revisionId: " + requestBody.id, ); } const listRootId = await this.orgRootRepository.find({ where: { orgRevisionId: requestBody.id }, select: ["id", "orgRootOrder"], }); if (!listRootId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId."); } const sortData = listRootId.map((data) => ({ id: data.id, orgRootOrder: requestBody.sortId.indexOf(data.id) + 1, })); await this.orgRootRepository.save(sortData); break; } case 1: { const rootId = await this.orgRootRepository.findOne({ where: { id: requestBody.id } }); if (!rootId?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: " + requestBody.id); } const listChild1Id = await this.child1Repository.find({ where: { orgRootId: requestBody.id }, select: ["id", "orgChild1Order"], }); if (!listChild1Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id."); } const sortData = listChild1Id.map((data) => ({ id: data.id, orgChild1Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child1Repository.save(sortData); break; } case 2: { const child1Id = await this.child1Repository.findOne({ where: { id: requestBody.id } }); if (!child1Id?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: " + requestBody.id); } const listChild2Id = await this.child2Repository.find({ where: { orgChild1Id: requestBody.id }, select: ["id", "orgChild2Order"], }); if (!listChild2Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id."); } const sortData = listChild2Id.map((data) => ({ id: data.id, orgChild2Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child2Repository.save(sortData); break; } case 3: { const child2Id = await this.child2Repository.findOne({ where: { id: requestBody.id } }); if (!child2Id?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: " + requestBody.id); } const listChild3Id = await this.child3Repository.find({ where: { orgChild2Id: requestBody.id }, select: ["id", "orgChild3Order"], }); if (!listChild3Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id."); } const sortData = listChild3Id.map((data) => ({ id: data.id, orgChild3Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child3Repository.save(sortData); break; } case 4: { const child3Id = await this.child3Repository.findOne({ where: { id: requestBody.id } }); if (!child3Id?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id: " + requestBody.id); } const listChild4Id = await this.child4Repository.find({ where: { orgChild3Id: requestBody.id }, select: ["id", "orgChild4Order"], }); if (!listChild4Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id."); } const sortData = listChild4Id.map((data) => ({ id: data.id, orgChild4Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child4Repository.save(sortData); break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.type); } return new HttpSuccess(); } catch (error) { return error; } } }