diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 3046cfa7..e6e01f5f 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -33,7 +33,7 @@ import { log } from "console"; @Route("api/v1/org") @Tags("Organization") -@Security("bearerAuth") +// @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", @@ -1670,181 +1670,1082 @@ export class OrganizationController extends Controller { * @summary Organizational StructChart * */ - @Get("struct-chart/{idNode}/{type}")//bright + @Get("struct-chart/{idNode}/{type}") //bright async structchart(@Path() idNode: string, type: number) { try { - - let revision: any; - let root: any; - let child1: any; - let child2: any; - let child3: any; - let child4: any; - let totalPositionCount_1: any; - let totalPositionVacant_1: any; - let totalPositionCount_2: any; - let totalPositionVacant_2: any; - let totalPositionCount_3: any; - let totalPositionVacant_3: any; - let totalPositionCount_4: any; - let totalPositionVacant_4: any; - let totalPositionCount_5: any; - let totalPositionVacant_5: any; - switch (type) { case 0: { - - revision = await this.orgRevisionRepository.findOne({ + 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 (!revision) { + if (!data) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found revision"); } - totalPositionCount_1 = await this.posMasterRepository.count({ - where: { - orgRevisionId: idNode - } - }); - totalPositionVacant_1 = await this.posMasterRepository.count({ - where: { - orgRevisionId: idNode, - current_holderId: IsNull() && "", - } - }); - root = await this.orgRootRepository.find({ - where: { - orgRevisionId: idNode - }, - }); - if(root.length > 0){ - child1 = await this.child1Repository.find({ - where: { - // orgRevisionId: idNode, - orgRootId: root.id, - }, - }); - if(child1.length > 0){ - child2 = await this.child2Repository.find({ - where: { - // orgRevisionId: idNode, - // orgRootId: root.id, - orgChild1Id: child1.id - }, - }); - if(child2.length > 0){ - child3 = await this.child3Repository.find({ - where: { - // orgRevisionId: idNode, - // orgRootId: root.id, - // orgChild1Id: child1.id, - orgChild2Id: child2.id - }, - }); - if(child3.length > 0){ - child4 = await this.child4Repository.find({ - where: { - // orgRevisionId: idNode, - // orgRootId: root.id, - // orgChild1Id: child1.id, - // orgChild2Id: child2.id, - orgChild3Id: child3.id, + const formattedData = { + departmentName: "กรุงเทพมหานคร", + deptID: data.id, + // 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() || "", }, - }); - } - } - } - } - - //Tree - const treeObject = { - deptID: revision.id, - departmentName: revision.orgRevisionName, - totalPositionCount: totalPositionCount_1 == 0 || null ? 0 : totalPositionCount_1, - totalPositionVacant: totalPositionVacant_1 == 0 || null ? 0 : totalPositionVacant_1, - children: root.map((roots:any) => ({ - deptID: roots.id, - departmentName: roots.orgRootName, - - - })), - heads: [ - { - positionID: null, - positionName: null, - positionNum: null, - totalPositionCount: 0, - totalPositionVacant: 0 - } - ], - officer: [ - { - positionID: null, - positionName: null, - positionNum: null, - totalPositionCount: 0, - totalPositionVacant: 0 - } - ] - } - - return new HttpSuccess(treeObject) - + }) + : 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, + // 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, + // 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, + // 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, + // 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) => ({ + orgTreeId: orgChild4.id, + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + // 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); } case 1: { - root = await this.orgRootRepository.findOne({ + const data = await this.orgRootRepository.findOne({ where: { id: idNode }, + relations: [ + "orgRevision", + "orgChild1s", + "orgChild1s.orgChild2s", + "orgChild1s.orgChild2s.orgChild3s", + "orgChild1s.orgChild2s.orgChild3s.orgChild4s", + ], }); - if (!root) { + if (!data) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId"); } - break; + + const formattedData = { + departmentName: data.orgRootName, + deptID: data.id, + // 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, + // 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, + // 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, + // 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) => ({ + orgTreeId: orgChild4.id, + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + // 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: { - child1 = await this.child1Repository.findOne({ + const data = await this.child1Repository.findOne({ where: { id: idNode }, + relations: [ + "orgRevision", + "orgChild2s", + "orgChild2s.orgChild3s", + "orgChild2s.orgChild3s.orgChild4s", + ], }); - if (!child1) { + if (!data) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id"); } - break; + + const formattedData = { + departmentName: data.orgChild1Name, + deptID: data.id, + // 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, + // 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, + // 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) => ({ + orgTreeId: orgChild4.id, + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + // 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: { - child2 = await this.child2Repository.findOne({ + const data = await this.child2Repository.findOne({ where: { id: idNode }, + relations: ["orgRevision", "orgChild3s", "orgChild3s.orgChild4s"], }); - if (!child2) { + if (!data) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id"); } - break; + + const formattedData = { + departmentName: data.orgChild2Name, + deptID: data.id, + // 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, + // 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) => ({ + orgTreeId: orgChild4.id, + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + // 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: { - child3 = await this.child3Repository.findOne({ + const data = await this.child3Repository.findOne({ where: { id: idNode }, + relations: ["orgRevision", "orgChild4s"], }); - if (!child3) { + if (!data) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id"); } - break; + + const formattedData = { + orgTreeId: data.id, + departmentName: data.orgChild3Name, + deptID: data.id, + // 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) => ({ + orgTreeId: orgChild4.id, + departmentName: orgChild4.orgChild4Name, + deptID: orgChild4.id, + // 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: { - child4 = await this.child4Repository.findOne({ + const data = await this.child4Repository.findOne({ where: { id: idNode }, + relations: ["orgRevision"], }); - if (!child4) { + if (!data) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id"); } - break; + + const formattedData = { + orgTreeId: data.id, + departmentName: data.orgChild4Name, + deptID: data.id, + // 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(); - } catch (error) { return error; } } - }