diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 1a1c3f69..107d7fc2 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -57,29 +57,28 @@ export class OrganizationController extends Controller { */ @Get("history") async GetHistory() { - 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, - })); + 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); - + return new HttpSuccess(mapOrgRevisions); } /** @@ -90,23 +89,22 @@ export class OrganizationController extends Controller { */ @Get("active") async GetActive() { - 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, + 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, - orgPublishDate: orgRevisionDraf == null ? null : orgRevisionDraf.orgPublishDate, - isPublic: orgRevisionDraf == null || orgRevisionDraf.orgRevisionName == null ? false : true, - }; - return new HttpSuccess(mapData); - + orgPublishDate: orgRevisionDraf == null ? null : orgRevisionDraf.orgPublishDate, + isPublic: orgRevisionDraf == null || orgRevisionDraf.orgRevisionName == null ? false : true, + }; + return new HttpSuccess(mapData); } /** @@ -120,467 +118,464 @@ export class OrganizationController extends Controller { @Body() requestBody: CreateOrgRevision, @Request() request: { user: Record }, ) { - 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); + 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" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" + ) { + 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."); + + let positions: Position[] = []; + //clone data + const orgRoot = await this.orgRootRepository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + }); + let _orgRoot: any = orgRoot.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await this.orgRootRepository.save(_orgRoot); + + const orgChild1 = await this.child1Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + }); + let _orgChild1: any = orgChild1.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await this.child1Repository.save(_orgChild1); + + const orgChild2 = await this.child2Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + }); + let _orgChild2: any = orgChild2.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await this.child2Repository.save(_orgChild2); + + const orgChild3 = await this.child3Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + }); + let _orgChild3: any = orgChild3.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await this.child3Repository.save(_orgChild3); + + const orgChild4 = await this.child4Repository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + }); + let _orgChild4: any = orgChild4.map((x) => ({ + ...x, + ancestorDNA: + x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + ? x.id + : x.ancestorDNA, + })); + await this.child4Repository.save(_orgChild4); + + const orgPosMaster = await this.posMasterRepository.find({ + where: { orgRevisionId: requestBody.orgRevisionId }, + relations: ["positions"], + }); + let _orgPosMaster: PosMaster[]; if ( - requestBody.typeDraft.toUpperCase() == "ORG" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ) { - 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."); - - let positions: Position[] = []; - //clone data - const orgRoot = await this.orgRootRepository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - }); - let _orgRoot: any = orgRoot.map((x) => ({ + _orgPosMaster = orgPosMaster.map((x) => ({ ...x, ancestorDNA: x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" ? x.id : x.ancestorDNA, })); - await this.orgRootRepository.save(_orgRoot); + await this.posMasterRepository.save(_orgPosMaster); + } - const orgChild1 = await this.child1Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - }); - let _orgChild1: any = orgChild1.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child1Repository.save(_orgChild1); - - const orgChild2 = await this.child2Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - }); - let _orgChild2: any = orgChild2.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child2Repository.save(_orgChild2); - - const orgChild3 = await this.child3Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - }); - let _orgChild3: any = orgChild3.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child3Repository.save(_orgChild3); - - const orgChild4 = await this.child4Repository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - }); - let _orgChild4: any = orgChild4.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.child4Repository.save(_orgChild4); - - const orgPosMaster = await this.posMasterRepository.find({ - where: { orgRevisionId: requestBody.orgRevisionId }, - relations: ["positions"], - }); - let _orgPosMaster: PosMaster[]; + _orgRoot.forEach(async (x: any) => { + var dataId = x.id; + 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); if ( requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ) { - _orgPosMaster = orgPosMaster.map((x) => ({ - ...x, - ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" - ? x.id - : x.ancestorDNA, - })); - await this.posMasterRepository.save(_orgPosMaster); + await Promise.all( + _orgPosMaster + .filter((x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null) + .map(async (item: any) => { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); + + item.positions.map(async (pos: any) => { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { + position.positionIsSelected = false; + } + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + // positions.push(position); + await this.positionRepository.save(position); + }); + }), + ); } - _orgRoot.forEach(async (x: any) => { - var dataId = x.id; - 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); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" - ) { - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { - position.positionIsSelected = false; + _orgChild1 + .filter((x: OrgChild1) => x.orgRootId == dataId) + .forEach(async (x: any) => { + var data1Id = x.id; + 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); + if ( + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" + ) { + await Promise.all( + _orgPosMaster + .filter((x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null) + .map(async (item: any) => { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - // positions.push(position); - await this.positionRepository.save(position); - }); - }), - ); - } + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); - _orgChild1 - .filter((x: OrgChild1) => x.orgRootId == dataId) - .forEach(async (x: any) => { - var data1Id = x.id; - 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); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" - ) { - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; + item.positions.map(async (pos: any) => { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { + position.positionIsSelected = false; } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - // positions.push(position); - await this.positionRepository.save(position); - }); - }), - ); - } - - _orgChild2 - .filter((x: OrgChild2) => x.orgChild1Id == data1Id) - .forEach(async (x: any) => { - var data2Id = x.id; - 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); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" - ) { - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - // positions.push(position); - await this.positionRepository.save(position); - }); - }), - ); - } - - _orgChild3 - .filter((x: OrgChild3) => x.orgChild2Id == data2Id) - .forEach(async (x: any) => { - var data3Id = x.id; - 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); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" - ) { - await Promise.all( - _orgPosMaster - .filter( - (x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, - ) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.orgChild3Id = data3.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - // positions.push(position); - await this.positionRepository.save(position); - }); - }), - ); - } - - _orgChild4 - .filter((x: OrgChild4) => x.orgChild3Id == data3Id) - .forEach(async (x: any) => { - var data4Id = x.id; - 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); - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" - ) { - await Promise.all( - _orgPosMaster - .filter((x: PosMaster) => x.orgChild4Id == data4Id) - .map(async (item: any) => { - delete item.id; - const posMaster = Object.assign(new PosMaster(), item); - posMaster.positions = []; - if ( - requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" - ) { - posMaster.next_holderId = item.current_holderId; - } else { - posMaster.next_holderId = null; - posMaster.isSit = false; - } - posMaster.current_holderId = null; - posMaster.orgRevisionId = revision.id; - posMaster.orgRootId = data.id; - posMaster.orgChild1Id = data1.id; - posMaster.orgChild2Id = data2.id; - posMaster.orgChild3Id = data3.id; - posMaster.orgChild4Id = data4.id; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.createdAt = new Date(); - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster); - - item.positions.map(async (pos: any) => { - delete pos.id; - const position = Object.assign(new Position(), pos); - position.posMasterId = posMaster.id; - if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { - position.positionIsSelected = false; - } - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.lastUpdatedAt = new Date(); - // positions.push(position); - await this.positionRepository.save(position); - }); - }), - ); - } - }); + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + // positions.push(position); + await this.positionRepository.save(position); }); - }); - }); - }); - // await this.positionRepository.save(positions); - } + }), + ); + } - const _orgRevisions = await this.orgRevisionRepository.find({ - where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }], - }); - const _posMasters = await this.posMasterRepository.find({ - where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.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.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); + _orgChild2 + .filter((x: OrgChild2) => x.orgChild1Id == data1Id) + .forEach(async (x: any) => { + var data2Id = x.id; + 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); + if ( + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" + ) { + await Promise.all( + _orgPosMaster + .filter((x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null) + .map(async (item: any) => { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); - return new HttpSuccess(revision); - + item.positions.map(async (pos: any) => { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { + position.positionIsSelected = false; + } + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + // positions.push(position); + await this.positionRepository.save(position); + }); + }), + ); + } + + _orgChild3 + .filter((x: OrgChild3) => x.orgChild2Id == data2Id) + .forEach(async (x: any) => { + var data3Id = x.id; + 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); + if ( + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" + ) { + await Promise.all( + _orgPosMaster + .filter( + (x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, + ) + .map(async (item: any) => { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.orgChild3Id = data3.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); + + item.positions.map(async (pos: any) => { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { + position.positionIsSelected = false; + } + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + // positions.push(position); + await this.positionRepository.save(position); + }); + }), + ); + } + + _orgChild4 + .filter((x: OrgChild4) => x.orgChild3Id == data3Id) + .forEach(async (x: any) => { + var data4Id = x.id; + 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); + if ( + requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" + ) { + await Promise.all( + _orgPosMaster + .filter((x: PosMaster) => x.orgChild4Id == data4Id) + .map(async (item: any) => { + delete item.id; + const posMaster = Object.assign(new PosMaster(), item); + posMaster.positions = []; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") { + posMaster.next_holderId = item.current_holderId; + } else { + posMaster.next_holderId = null; + posMaster.isSit = false; + } + posMaster.current_holderId = null; + posMaster.orgRevisionId = revision.id; + posMaster.orgRootId = data.id; + posMaster.orgChild1Id = data1.id; + posMaster.orgChild2Id = data2.id; + posMaster.orgChild3Id = data3.id; + posMaster.orgChild4Id = data4.id; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.createdAt = new Date(); + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + posMaster.lastUpdatedAt = new Date(); + await this.posMasterRepository.save(posMaster); + + item.positions.map(async (pos: any) => { + delete pos.id; + const position = Object.assign(new Position(), pos); + position.posMasterId = posMaster.id; + if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") { + position.positionIsSelected = false; + } + position.createdUserId = request.user.sub; + position.createdFullName = request.user.name; + position.createdAt = new Date(); + position.lastUpdateUserId = request.user.sub; + position.lastUpdateFullName = request.user.name; + position.lastUpdatedAt = new Date(); + // positions.push(position); + await this.positionRepository.save(position); + }); + }), + ); + } + }); + }); + }); + }); + }); + // await this.positionRepository.save(positions); + } + + const _orgRevisions = await this.orgRevisionRepository.find({ + where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }], + }); + const _posMasters = await this.posMasterRepository.find({ + where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.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.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); } /** @@ -591,674 +586,674 @@ export class OrganizationController extends Controller { */ @Get("{id}") async detail(@Path() id: string) { - const orgRevision = await this.orgRevisionRepository.findOne({ where: { id } }); - if (!orgRevision) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } + 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.orgRootOrder", "ASC") - .getMany(); + 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.orgRootOrder", "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.orgChild1Order", "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.orgChild1Order", "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.orgChild2Order", "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.orgChild2Order", "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.orgChild3Order", "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.orgChild3Order", "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.orgChild4Order", "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.orgChild4Order", "ASC") + .getMany() + : []; - // const formattedData = orgRootData.map((orgRoot) => { - const formattedData = await Promise.all( - orgRootData.map(async (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, - totalPosition: await this.posMasterRepository.count({ - where: { orgRevisionId: orgRoot.orgRevisionId, orgRootId: orgRoot.id }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, + // const formattedData = orgRootData.map((orgRoot) => { + const formattedData = await Promise.all( + orgRootData.map(async (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, + totalPosition: await this.posMasterRepository.count({ + where: { orgRevisionId: orgRoot.orgRevisionId, orgRootId: orgRoot.id }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), + + children: await Promise.all( + orgChild1Data + .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) + .map(async (orgChild1) => ({ + orgTreeId: orgChild1.id, orgRootId: orgRoot.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { + 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, - orgRootId: orgRoot.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: IsNull() || "", - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), + orgRootName: orgRoot.orgRootName, + totalPosition: await this.posMasterRepository.count({ + where: { orgRevisionId: orgRoot.orgRevisionId, orgChild1Id: orgChild1.id }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), - children: await Promise.all( - orgChild1Data - .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) - .map(async (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, - totalPosition: await this.posMasterRepository.count({ - where: { orgRevisionId: orgRoot.orgRevisionId, orgChild1Id: orgChild1.id }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { + children: await Promise.all( + orgChild2Data + .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) + .map(async (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, - orgChild1Id: orgChild1.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild1Id: orgChild1.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild1Id: orgChild1.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild1Id: orgChild1.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: IsNull() || "", - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), + orgRootName: orgRoot.orgRootName, + totalPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), - children: await Promise.all( - orgChild2Data - .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) - .map(async (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, - totalPosition: await this.posMasterRepository.count({ - where: { + children: await Promise.all( + orgChild3Data + .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) + .map(async (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, - orgChild2Id: orgChild2.id, - }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild2Id: orgChild2.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: IsNull() || "", - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), + orgRootName: orgRoot.orgRootName, + totalPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), - children: await Promise.all( - orgChild3Data - .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) - .map(async (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, - totalPosition: await this.posMasterRepository.count({ - where: { + children: await Promise.all( + orgChild4Data + .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) + .map(async (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, - orgChild3Id: orgChild3.id, - }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild3Id: orgChild3.id, - next_holderId: IsNull() || "", - }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - }, - }), - totalRootPositionCurrentUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: IsNull() || "", - next_holderId: IsNull() || "", - }, - }), - - children: await Promise.all( - orgChild4Data - .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) - .map(async (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, - totalPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild4Id: orgChild4.id, - }, - }), - totalPositionCurrentUse: await this.posMasterRepository.count({ + orgRootName: orgRoot.orgRootName, + totalPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count( + { where: { orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, orgChild4Id: orgChild4.id, current_holderId: Not(IsNull()) || Not(""), }, - }), - totalPositionCurrentVacant: - await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - totalPositionNextUse: await this.posMasterRepository.count({ + }, + ), + totalRootPositionCurrentVacant: + await this.posMasterRepository.count({ where: { orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, orgChild4Id: orgChild4.id, - next_holderId: Not(IsNull()) || Not(""), + current_holderId: IsNull() || "", }, }), - totalPositionNextVacant: await this.posMasterRepository.count({ + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count( + { where: { orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, orgChild4Id: orgChild4.id, next_holderId: IsNull() || "", }, - }), - totalRootPosition: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - }, - }), - totalRootPositionCurrentUse: - await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - current_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionCurrentVacant: - await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - totalRootPositionNextUse: await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - next_holderId: Not(IsNull()) || Not(""), - }, - }), - totalRootPositionNextVacant: - await this.posMasterRepository.count({ - where: { - orgRevisionId: orgRoot.orgRevisionId, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - next_holderId: IsNull() || "", - }, - }), - })), - ), - })), - ), - })), - ), - })), - ), - }; - }), - ); + }, + ), + })), + ), + })), + ), + })), + ), + })), + ), + }; + }), + ); - return new HttpSuccess(formattedData); - + return new HttpSuccess(formattedData); } /** @@ -1284,14 +1279,13 @@ export class OrganizationController extends Controller { if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } - 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(); - + 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(); } /** @@ -1440,117 +1434,113 @@ export class OrganizationController extends Controller { */ @Post("sort") async Sort(@Body() requestBody: { id: string; type: number; sortId: string[] }) { - 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; + 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); } - - 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; + const listRootId = await this.orgRootRepository.find({ + where: { orgRevisionId: requestBody.id }, + select: ["id", "orgRootOrder"], + }); + if (!listRootId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId."); } - - 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); + const sortData = listRootId.map((data) => ({ + id: data.id, + orgRootOrder: requestBody.sortId.indexOf(data.id) + 1, + })); + await this.orgRootRepository.save(sortData); + break; } - return new HttpSuccess(); - + + 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(); } /** @@ -1654,1092 +1644,1090 @@ export class OrganizationController extends Controller { */ @Get("struct-chart/{idNode}/{type}") //bright async structchart(@Path() idNode: string, type: number) { - switch (type) { - case 0: { - const data = await this.orgRevisionRepository.findOne({ - where: { id: idNode }, - relations: [ - "orgRoots", - "orgRoots.orgChild1s", - "orgRoots.orgChild1s.orgChild2s", - "orgRoots.orgChild1s.orgChild2s.orgChild3s", - "orgRoots.orgChild1s.orgChild2s.orgChild3s.orgChild4s", - ], - }); - if (!data) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found revision"); - } - - const formattedData = { - departmentName: data.orgRevisionName, - deptID: data.id, - type: 0, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgRevisionId: data.id }, - }), - totalPositionVacant: - data.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // next_holderId: IsNull() || "", - // }, - // }), - children: await Promise.all( - data.orgRoots - .sort((a, b) => a.orgRootOrder - b.orgRootOrder) - .map(async (orgRoot: OrgRoot) => { - return { - departmentName: orgRoot.orgRootName, - deptID: orgRoot.id, - type: 1, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgRevisionId: data.id, orgRootId: orgRoot.id }, - }), - totalPositionVacant: - data.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // next_holderId: IsNull() || "", - // }, - // }), - children: await Promise.all( - orgRoot.orgChild1s - .sort((a, b) => a.orgChild1Order - b.orgChild1Order) - .map(async (orgChild1) => ({ - departmentName: orgChild1.orgChild1Name, - deptID: orgChild1.id, - type: 2, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - }, - }), - totalPositionVacant: - data.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // next_holderId: IsNull() || "", - // }, - // }), - children: await Promise.all( - orgChild1.orgChild2s - .sort((a, b) => a.orgChild2Order - b.orgChild2Order) - .map(async (orgChild2) => ({ - departmentName: orgChild2.orgChild2Name, - deptID: orgChild2.id, - type: 3, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - }, - }), - totalPositionVacant: - data.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // next_holderId: IsNull() || "", - // }, - // }), - children: await Promise.all( - orgChild2.orgChild3s - .sort((a, b) => a.orgChild3Order - b.orgChild3Order) - .map(async (orgChild3) => ({ - departmentName: orgChild3.orgChild3Name, - deptID: orgChild3.id, - type: 4, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - }, - }), - totalPositionVacant: - data.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: - // await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count( - // { - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // next_holderId: IsNull() || "", - // }, - // }, - // ), - children: await Promise.all( - orgChild3.orgChild4s - .sort((a, b) => a.orgChild4Order - b.orgChild4Order) - .map(async (orgChild4) => ({ - departmentName: orgChild4.orgChild4Name, - deptID: orgChild4.id, - type: 5, - // heads: - totalPositionCount: - await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - }, - }), - totalPositionVacant: - data.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgRootId: orgRoot.id, - orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: - // await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: - // await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgRootId: orgRoot.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // next_holderId: IsNull() || "", - // }, - // }), - })), - ), - })), - ), - })), - ), - })), - ), - }; - }), - ), - }; - return new HttpSuccess([formattedData]); + switch (type) { + case 0: { + const data = await this.orgRevisionRepository.findOne({ + where: { id: idNode }, + relations: [ + "orgRoots", + "orgRoots.orgChild1s", + "orgRoots.orgChild1s.orgChild2s", + "orgRoots.orgChild1s.orgChild2s.orgChild3s", + "orgRoots.orgChild1s.orgChild2s.orgChild3s.orgChild4s", + ], + }); + if (!data) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found revision"); } - case 1: { - const data = await this.orgRootRepository.findOne({ - where: { id: idNode }, - relations: [ - "orgRevision", - "orgChild1s", - "orgChild1s.orgChild2s", - "orgChild1s.orgChild2s.orgChild3s", - "orgChild1s.orgChild2s.orgChild3s.orgChild4s", - ], - }); - if (!data) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId"); - } - const formattedData = { - departmentName: data.orgRootName, - deptID: data.id, - type: 1, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgRootId: data.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRootId: data.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRootId: data.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // next_holderId: IsNull() || "", - // }, - // }), - - children: await Promise.all( - data.orgChild1s - .sort((a, b) => a.orgChild1Order - b.orgChild1Order) - .map(async (orgChild1) => ({ - departmentName: orgChild1.orgChild1Name, - deptID: orgChild1.id, - type: 2, + const formattedData = { + departmentName: data.orgRevisionName, + deptID: data.id, + type: 0, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgRevisionId: data.id }, + }), + totalPositionVacant: + data.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // next_holderId: IsNull() || "", + // }, + // }), + children: await Promise.all( + data.orgRoots + .sort((a, b) => a.orgRootOrder - b.orgRootOrder) + .map(async (orgRoot: OrgRoot) => { + return { + departmentName: orgRoot.orgRootName, + deptID: orgRoot.id, + type: 1, // heads: totalPositionCount: await this.posMasterRepository.count({ - where: { orgRootId: data.id, orgChild1Id: orgChild1.id }, + where: { orgRevisionId: data.id, orgRootId: orgRoot.id }, }), totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true + data.orgRevisionIsDraft == true ? await this.posMasterRepository.count({ where: { - orgRootId: data.id, - orgChild1Id: orgChild1.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, next_holderId: IsNull() || "", }, }) : await this.posMasterRepository.count({ where: { - orgRootId: data.id, - orgChild1Id: orgChild1.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, current_holderId: IsNull() || "", }, }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // next_holderId: IsNull() || "", - // }, - // }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // next_holderId: IsNull() || "", + // }, + // }), children: await Promise.all( - orgChild1.orgChild2s - .sort((a, b) => a.orgChild2Order - b.orgChild2Order) - .map(async (orgChild2) => ({ - departmentName: orgChild2.orgChild2Name, - deptID: orgChild2.id, - type: 3, + orgRoot.orgChild1s + .sort((a, b) => a.orgChild1Order - b.orgChild1Order) + .map(async (orgChild1) => ({ + departmentName: orgChild1.orgChild1Name, + deptID: orgChild1.id, + type: 2, // heads: totalPositionCount: await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, }, }), - totalPositionCurrentVacant: - data.orgRevision.orgRevisionIsDraft == true + totalPositionVacant: + data.orgRevisionIsDraft == true ? await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, next_holderId: IsNull() || "", }, }) : await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, - orgChild2Id: orgChild2.id, current_holderId: IsNull() || "", }, }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // next_holderId: IsNull() || "", - // }, - // }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // next_holderId: IsNull() || "", + // }, + // }), children: await Promise.all( - orgChild2.orgChild3s - .sort((a, b) => a.orgChild3Order - b.orgChild3Order) - .map(async (orgChild3) => ({ - departmentName: orgChild3.orgChild3Name, - deptID: orgChild3.id, - type: 4, + orgChild1.orgChild2s + .sort((a, b) => a.orgChild2Order - b.orgChild2Order) + .map(async (orgChild2) => ({ + departmentName: orgChild2.orgChild2Name, + deptID: orgChild2.id, + type: 3, // heads: totalPositionCount: await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, }, }), totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true + data.orgRevisionIsDraft == true ? await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, next_holderId: IsNull() || "", }, }) : await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, current_holderId: IsNull() || "", }, }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // next_holderId: IsNull() || "", - // }, - // }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // next_holderId: IsNull() || "", + // }, + // }), children: await Promise.all( - orgChild3.orgChild4s - .sort((a, b) => a.orgChild4Order - b.orgChild4Order) - .map(async (orgChild4) => ({ - departmentName: orgChild4.orgChild4Name, - deptID: orgChild4.id, - type: 5, + orgChild2.orgChild3s + .sort((a, b) => a.orgChild3Order - b.orgChild3Order) + .map(async (orgChild3) => ({ + departmentName: orgChild3.orgChild3Name, + deptID: orgChild3.id, + type: 4, // heads: totalPositionCount: await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, orgChild2Id: orgChild2.id, orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, }, }), totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true + data.orgRevisionIsDraft == true ? await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, orgChild2Id: orgChild2.id, orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, next_holderId: IsNull() || "", }, }) : await this.posMasterRepository.count({ where: { - orgRootId: data.id, + orgRevisionId: data.id, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: + // await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count( + // { + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // next_holderId: IsNull() || "", + // }, + // }, + // ), + children: await Promise.all( + orgChild3.orgChild4s + .sort((a, b) => a.orgChild4Order - b.orgChild4Order) + .map(async (orgChild4) => ({ + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + type: 5, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgRootId: orgRoot.id, orgChild1Id: orgChild1.id, orgChild2Id: orgChild2.id, orgChild3Id: orgChild3.id, orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", }, }), - // totalPositionCurrentVacant: - // await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRootId: data.id, - // orgChild1Id: orgChild1.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // next_holderId: IsNull() || "", - // }, - // }), + totalPositionVacant: + data.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: + // await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: + // await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgRootId: orgRoot.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // next_holderId: IsNull() || "", + // }, + // }), + })), + ), })), ), })), ), })), ), - })), - ), - }; - return new HttpSuccess([formattedData]); - } - case 2: { - const data = await this.child1Repository.findOne({ - where: { id: idNode }, - relations: [ - "orgRevision", - "orgChild2s", - "orgChild2s.orgChild3s", - "orgChild2s.orgChild3s.orgChild4s", - ], - }); - if (!data) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id"); - } - - const formattedData = { - departmentName: data.orgChild1Name, - deptID: data.id, - type: 2, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgRevisionId: data.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild1Id: data.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild1Id: data.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild1Id: data.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild1Id: data.id, - // next_holderId: IsNull() || "", - // }, - // }), - - children: await Promise.all( - data.orgChild2s - .sort((a, b) => a.orgChild2Order - b.orgChild2Order) - .map(async (orgChild2) => ({ - departmentName: orgChild2.orgChild2Name, - deptID: orgChild2.id, - type: 3, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgChild1Id: data.id, orgChild2Id: orgChild2.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild1Id: data.id, - orgChild2Id: orgChild2.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild1Id: data.id, - orgChild2Id: orgChild2.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild1Id: data.id, - // orgChild2Id: orgChild2.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild1Id: data.id, - // orgChild2Id: orgChild2.id, - // next_holderId: IsNull() || "", - // }, - // }), - children: await Promise.all( - orgChild2.orgChild3s - .sort((a, b) => a.orgChild3Order - b.orgChild3Order) - .map(async (orgChild3) => ({ - departmentName: orgChild3.orgChild3Name, - deptID: orgChild3.id, - type: 4, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // next_holderId: IsNull() || "", - // }, - // }), - children: await Promise.all( - orgChild3.orgChild4s - .sort((a, b) => a.orgChild4Order - b.orgChild4Order) - .map(async (orgChild4) => ({ - departmentName: orgChild4.orgChild4Name, - deptID: orgChild4.id, - type: 5, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgRevisionId: data.id, - orgChild2Id: orgChild2.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgRevisionId: data.id, - // orgChild2Id: orgChild2.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // next_holderId: IsNull() || "", - // }, - // }), - })), - ), - })), - ), - })), - ), - }; - return new HttpSuccess([formattedData]); - } - case 3: { - const data = await this.child2Repository.findOne({ - where: { id: idNode }, - relations: ["orgRevision", "orgChild3s", "orgChild3s.orgChild4s"], - }); - if (!data) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id"); - } - - const formattedData = { - departmentName: data.orgChild2Name, - deptID: data.id, - type: 3, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgChild2Id: data.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild2Id: data.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild2Id: data.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild2Id: data.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild2Id: data.id, - // next_holderId: IsNull() || "", - // }, - // }), - - children: await Promise.all( - data.orgChild3s - .sort((a, b) => a.orgChild3Order - b.orgChild3Order) - .map(async (orgChild3) => ({ - departmentName: orgChild3.orgChild3Name, - deptID: orgChild3.id, - type: 4, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgChild2Id: data.id, orgChild3Id: orgChild3.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild2Id: data.id, - orgChild3Id: orgChild3.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild2Id: data.id, - orgChild3Id: orgChild3.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild2Id: data.id, - // orgChild3Id: orgChild3.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild2Id: data.id, - // orgChild3Id: orgChild3.id, - // next_holderId: IsNull() || "", - // }, - // }), - children: await Promise.all( - orgChild3.orgChild4s - .sort((a, b) => a.orgChild4Order - b.orgChild4Order) - .map(async (orgChild4) => ({ - departmentName: orgChild4.orgChild4Name, - deptID: orgChild4.id, - type: 5, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { - orgChild2Id: data.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild2Id: data.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild2Id: data.id, - orgChild3Id: orgChild3.id, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild2Id: data.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild2Id: data.id, - // orgChild3Id: orgChild3.id, - // orgChild4Id: orgChild4.id, - // next_holderId: IsNull() || "", - // }, - // }), - })), - ), - })), - ), - }; - return new HttpSuccess([formattedData]); - } - case 4: { - const data = await this.child3Repository.findOne({ - where: { id: idNode }, - relations: ["orgRevision", "orgChild4s"], - }); - if (!data) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id"); - } - - const formattedData = { - departmentName: data.orgChild3Name, - deptID: data.id, - type: 4, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgChild3Id: data.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild3Id: data.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild3Id: data.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild3Id: data.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild3Id: data.id, - // next_holderId: IsNull() || "", - // }, - // }), - - children: await Promise.all( - data.orgChild4s - .sort((a, b) => a.orgChild4Order - b.orgChild4Order) - .map(async (orgChild4) => ({ - departmentName: orgChild4.orgChild4Name, - deptID: orgChild4.id, - type: 5, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgChild3Id: data.id, orgChild4Id: orgChild4.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild3Id: data.id, - orgChild4Id: orgChild4.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild3Id: data.id, - orgChild4Id: orgChild4.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild3Id: data.id, - // orgChild4Id: orgChild4.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild3Id: data.id, - // orgChild4Id: orgChild4.id, - // next_holderId: IsNull() || "", - // }, - // }), - })), - ), - }; - return new HttpSuccess([formattedData]); - } - case 5: { - const data = await this.child4Repository.findOne({ - where: { id: idNode }, - relations: ["orgRevision"], - }); - if (!data) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id"); - } - - const formattedData = { - departmentName: data.orgChild4Name, - deptID: data.id, - type: 5, - // heads: - totalPositionCount: await this.posMasterRepository.count({ - where: { orgChild4Id: data.id }, - }), - totalPositionVacant: - data.orgRevision.orgRevisionIsDraft == true - ? await this.posMasterRepository.count({ - where: { - orgChild4Id: data.id, - next_holderId: IsNull() || "", - }, - }) - : await this.posMasterRepository.count({ - where: { - orgChild4Id: data.id, - current_holderId: IsNull() || "", - }, - }), - // totalPositionCurrentVacant: await this.posMasterRepository.count({ - // where: { - // orgChild4Id: data.id, - // current_holderId: IsNull() || "", - // }, - // }), - // totalPositionNextVacant: await this.posMasterRepository.count({ - // where: { - // orgChild4Id: data.id, - // next_holderId: IsNull() || "", - // }, - // }), - }; - return new HttpSuccess([formattedData]); - } - default: - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: "); + }; + }), + ), + }; + return new HttpSuccess([formattedData]); } - + case 1: { + const data = await this.orgRootRepository.findOne({ + where: { id: idNode }, + relations: [ + "orgRevision", + "orgChild1s", + "orgChild1s.orgChild2s", + "orgChild1s.orgChild2s.orgChild3s", + "orgChild1s.orgChild2s.orgChild3s.orgChild4s", + ], + }); + if (!data) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId"); + } + + const formattedData = { + departmentName: data.orgRootName, + deptID: data.id, + type: 1, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgRootId: data.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // next_holderId: IsNull() || "", + // }, + // }), + + children: await Promise.all( + data.orgChild1s + .sort((a, b) => a.orgChild1Order - b.orgChild1Order) + .map(async (orgChild1) => ({ + departmentName: orgChild1.orgChild1Name, + deptID: orgChild1.id, + type: 2, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgRootId: data.id, orgChild1Id: orgChild1.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // next_holderId: IsNull() || "", + // }, + // }), + children: await Promise.all( + orgChild1.orgChild2s + .sort((a, b) => a.orgChild2Order - b.orgChild2Order) + .map(async (orgChild2) => ({ + departmentName: orgChild2.orgChild2Name, + deptID: orgChild2.id, + type: 3, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + }, + }), + totalPositionCurrentVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // next_holderId: IsNull() || "", + // }, + // }), + children: await Promise.all( + orgChild2.orgChild3s + .sort((a, b) => a.orgChild3Order - b.orgChild3Order) + .map(async (orgChild3) => ({ + departmentName: orgChild3.orgChild3Name, + deptID: orgChild3.id, + type: 4, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // next_holderId: IsNull() || "", + // }, + // }), + children: await Promise.all( + orgChild3.orgChild4s + .sort((a, b) => a.orgChild4Order - b.orgChild4Order) + .map(async (orgChild4) => ({ + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + type: 5, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRootId: data.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: + // await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRootId: data.id, + // orgChild1Id: orgChild1.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // next_holderId: IsNull() || "", + // }, + // }), + })), + ), + })), + ), + })), + ), + })), + ), + }; + return new HttpSuccess([formattedData]); + } + case 2: { + const data = await this.child1Repository.findOne({ + where: { id: idNode }, + relations: [ + "orgRevision", + "orgChild2s", + "orgChild2s.orgChild3s", + "orgChild2s.orgChild3s.orgChild4s", + ], + }); + if (!data) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id"); + } + + const formattedData = { + departmentName: data.orgChild1Name, + deptID: data.id, + type: 2, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgRevisionId: data.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild1Id: data.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild1Id: data.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild1Id: data.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild1Id: data.id, + // next_holderId: IsNull() || "", + // }, + // }), + + children: await Promise.all( + data.orgChild2s + .sort((a, b) => a.orgChild2Order - b.orgChild2Order) + .map(async (orgChild2) => ({ + departmentName: orgChild2.orgChild2Name, + deptID: orgChild2.id, + type: 3, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgChild1Id: data.id, orgChild2Id: orgChild2.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild1Id: data.id, + orgChild2Id: orgChild2.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild1Id: data.id, + orgChild2Id: orgChild2.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild1Id: data.id, + // orgChild2Id: orgChild2.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild1Id: data.id, + // orgChild2Id: orgChild2.id, + // next_holderId: IsNull() || "", + // }, + // }), + children: await Promise.all( + orgChild2.orgChild3s + .sort((a, b) => a.orgChild3Order - b.orgChild3Order) + .map(async (orgChild3) => ({ + departmentName: orgChild3.orgChild3Name, + deptID: orgChild3.id, + type: 4, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // next_holderId: IsNull() || "", + // }, + // }), + children: await Promise.all( + orgChild3.orgChild4s + .sort((a, b) => a.orgChild4Order - b.orgChild4Order) + .map(async (orgChild4) => ({ + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + type: 5, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgRevisionId: data.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgRevisionId: data.id, + // orgChild2Id: orgChild2.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // next_holderId: IsNull() || "", + // }, + // }), + })), + ), + })), + ), + })), + ), + }; + return new HttpSuccess([formattedData]); + } + case 3: { + const data = await this.child2Repository.findOne({ + where: { id: idNode }, + relations: ["orgRevision", "orgChild3s", "orgChild3s.orgChild4s"], + }); + if (!data) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id"); + } + + const formattedData = { + departmentName: data.orgChild2Name, + deptID: data.id, + type: 3, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgChild2Id: data.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild2Id: data.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild2Id: data.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild2Id: data.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild2Id: data.id, + // next_holderId: IsNull() || "", + // }, + // }), + + children: await Promise.all( + data.orgChild3s + .sort((a, b) => a.orgChild3Order - b.orgChild3Order) + .map(async (orgChild3) => ({ + departmentName: orgChild3.orgChild3Name, + deptID: orgChild3.id, + type: 4, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgChild2Id: data.id, orgChild3Id: orgChild3.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild2Id: data.id, + orgChild3Id: orgChild3.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild2Id: data.id, + orgChild3Id: orgChild3.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild2Id: data.id, + // orgChild3Id: orgChild3.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild2Id: data.id, + // orgChild3Id: orgChild3.id, + // next_holderId: IsNull() || "", + // }, + // }), + children: await Promise.all( + orgChild3.orgChild4s + .sort((a, b) => a.orgChild4Order - b.orgChild4Order) + .map(async (orgChild4) => ({ + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + type: 5, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { + orgChild2Id: data.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild2Id: data.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild2Id: data.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild2Id: data.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild2Id: data.id, + // orgChild3Id: orgChild3.id, + // orgChild4Id: orgChild4.id, + // next_holderId: IsNull() || "", + // }, + // }), + })), + ), + })), + ), + }; + return new HttpSuccess([formattedData]); + } + case 4: { + const data = await this.child3Repository.findOne({ + where: { id: idNode }, + relations: ["orgRevision", "orgChild4s"], + }); + if (!data) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id"); + } + + const formattedData = { + departmentName: data.orgChild3Name, + deptID: data.id, + type: 4, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgChild3Id: data.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild3Id: data.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild3Id: data.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild3Id: data.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild3Id: data.id, + // next_holderId: IsNull() || "", + // }, + // }), + + children: await Promise.all( + data.orgChild4s + .sort((a, b) => a.orgChild4Order - b.orgChild4Order) + .map(async (orgChild4) => ({ + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + type: 5, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgChild3Id: data.id, orgChild4Id: orgChild4.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild3Id: data.id, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild3Id: data.id, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild3Id: data.id, + // orgChild4Id: orgChild4.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild3Id: data.id, + // orgChild4Id: orgChild4.id, + // next_holderId: IsNull() || "", + // }, + // }), + })), + ), + }; + return new HttpSuccess([formattedData]); + } + case 5: { + const data = await this.child4Repository.findOne({ + where: { id: idNode }, + relations: ["orgRevision"], + }); + if (!data) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id"); + } + + const formattedData = { + departmentName: data.orgChild4Name, + deptID: data.id, + type: 5, + // heads: + totalPositionCount: await this.posMasterRepository.count({ + where: { orgChild4Id: data.id }, + }), + totalPositionVacant: + data.orgRevision.orgRevisionIsDraft == true + ? await this.posMasterRepository.count({ + where: { + orgChild4Id: data.id, + next_holderId: IsNull() || "", + }, + }) + : await this.posMasterRepository.count({ + where: { + orgChild4Id: data.id, + current_holderId: IsNull() || "", + }, + }), + // totalPositionCurrentVacant: await this.posMasterRepository.count({ + // where: { + // orgChild4Id: data.id, + // current_holderId: IsNull() || "", + // }, + // }), + // totalPositionNextVacant: await this.posMasterRepository.count({ + // where: { + // orgChild4Id: data.id, + // next_holderId: IsNull() || "", + // }, + // }), + }; + return new HttpSuccess([formattedData]); + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: "); + } } /** @@ -2750,62 +2738,61 @@ export class OrganizationController extends Controller { */ @Post("find/node") async findNodeAll(@Body() requestBody: { node: number; nodeId: string }) { - switch (requestBody.node) { - case 0: { - const data = await this.orgRootRepository.findOne({ - where: { id: requestBody.nodeId }, - }); - if (data == null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId."); - } - return new HttpSuccess([data.id]); + switch (requestBody.node) { + case 0: { + const data = await this.orgRootRepository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId."); } - case 1: { - const data = await this.child1Repository.findOne({ - where: { id: requestBody.nodeId }, - }); - if (data == null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1."); - } - return new HttpSuccess([data.orgRootId, data.id]); - } - case 2: { - const data = await this.child2Repository.findOne({ - where: { id: requestBody.nodeId }, - }); - if (data == null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2."); - } - return new HttpSuccess([data.orgRootId, data.orgChild1Id, data.id]); - } - case 3: { - const data = await this.child3Repository.findOne({ - where: { id: requestBody.nodeId }, - }); - if (data == null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3."); - } - return new HttpSuccess([data.orgRootId, data.orgChild1Id, data.orgChild2Id, data.id]); - } - case 4: { - const data = await this.child4Repository.findOne({ - where: { id: requestBody.nodeId }, - }); - if (data == null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4."); - } - return new HttpSuccess([ - data.orgRootId, - data.orgChild1Id, - data.orgChild2Id, - data.orgChild3Id, - data.id, - ]); - } - default: - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.node); + return new HttpSuccess([data.id]); } - + case 1: { + const data = await this.child1Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1."); + } + return new HttpSuccess([data.orgRootId, data.id]); + } + case 2: { + const data = await this.child2Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2."); + } + return new HttpSuccess([data.orgRootId, data.orgChild1Id, data.id]); + } + case 3: { + const data = await this.child3Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3."); + } + return new HttpSuccess([data.orgRootId, data.orgChild1Id, data.orgChild2Id, data.id]); + } + case 4: { + const data = await this.child4Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4."); + } + return new HttpSuccess([ + data.orgRootId, + data.orgChild1Id, + data.orgChild2Id, + data.orgChild3Id, + data.id, + ]); + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.node); + } } /** @@ -2816,25 +2803,24 @@ export class OrganizationController extends Controller { */ @Get("active/root") async GetActiveRoot() { - const orgRevisionActive = await this.orgRevisionRepository.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, - }); - if (!orgRevisionActive) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพร๋อยู่ตอนนี้"); - } + const orgRevisionActive = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + if (!orgRevisionActive) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพร๋อยู่ตอนนี้"); + } - const data = await this.orgRootRepository.find({ - where: { orgRevisionId: orgRevisionActive.id }, - relations: [ - "orgRevision", - "orgChild1s", - "orgChild1s.orgChild2s", - "orgChild1s.orgChild2s.orgChild3s", - "orgChild1s.orgChild2s.orgChild3s.orgChild4s", - ], - }); - return new HttpSuccess(data); - + const data = await this.orgRootRepository.find({ + where: { orgRevisionId: orgRevisionActive.id }, + relations: [ + "orgRevision", + "orgChild1s", + "orgChild1s.orgChild2s", + "orgChild1s.orgChild2s.orgChild3s", + "orgChild1s.orgChild2s.orgChild3s.orgChild4s", + ], + }); + return new HttpSuccess(data); } /** @@ -2845,18 +2831,17 @@ export class OrganizationController extends Controller { */ @Get("active/root/id") async GetActiveRootId() { - const orgRevisionActive = await this.orgRevisionRepository.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, - }); - if (!orgRevisionActive) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพร๋อยู่ตอนนี้"); - } + const orgRevisionActive = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + if (!orgRevisionActive) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพร๋อยู่ตอนนี้"); + } - const data = await this.orgRootRepository.find({ - where: { orgRevisionId: orgRevisionActive.id }, - }); - return new HttpSuccess(data.map((x) => x.id)); - + const data = await this.orgRootRepository.find({ + where: { orgRevisionId: orgRevisionActive.id }, + }); + return new HttpSuccess(data.map((x) => x.id)); } /** @@ -2867,18 +2852,17 @@ export class OrganizationController extends Controller { */ @Get("active/root/{revisionId}") async GetActiveRootByRevision(@Path() revisionId: string) { - const data = await this.orgRootRepository.find({ - where: { orgRevisionId: revisionId }, - relations: [ - "orgRevision", - "orgChild1s", - "orgChild1s.orgChild2s", - "orgChild1s.orgChild2s.orgChild3s", - "orgChild1s.orgChild2s.orgChild3s.orgChild4s", - ], - }); - return new HttpSuccess(data); - + const data = await this.orgRootRepository.find({ + where: { orgRevisionId: revisionId }, + relations: [ + "orgRevision", + "orgChild1s", + "orgChild1s.orgChild2s", + "orgChild1s.orgChild2s.orgChild3s", + "orgChild1s.orgChild2s.orgChild3s.orgChild4s", + ], + }); + return new HttpSuccess(data); } /** * API หา revision ล่าสุด