From 0e71ba98ea4f8d952c5aefaa4ee1e0b45b14acba Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 9 Feb 2024 16:35:01 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=20api=20?= =?UTF-8?q?=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88=E0=B8=87?= =?UTF-8?q?=E0=B8=97=E0=B8=B1=E0=B9=89=E0=B8=87=E0=B8=AB=E0=B8=A1=E0=B8=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PositionController.ts | 699 +++++++++++++++++++------- 1 file changed, 506 insertions(+), 193 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 1a013369..1981b38f 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1137,7 +1137,7 @@ export class PositionController extends Controller { * */ @Post("summary") - async PositionSummary(@Body() requestBody: { id: string; type: number }) { + async PositionSummary(@Body() requestBody: { id: string; type: number; isNode: boolean }) { try { let summary: any; let totalPosition: any; @@ -1146,204 +1146,517 @@ export class PositionController extends Controller { let totalPositionNextUse: any; let totalPositionNextVacant: any; - switch (requestBody.type) { - case 0: { - const NodeId = await this.posMasterRepository.findOne({ - where: { orgRootId: requestBody.id }, - }); - if (!NodeId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, - ); + if(requestBody.isNode === true){ + switch (requestBody.type) { + case 0: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgRootId: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { orgRootId: requestBody.id }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + next_holderId: IsNull() && "", + }, + }); + break; } - totalPosition = await this.posMasterRepository.count({ - where: { orgRootId: requestBody.id }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - current_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - current_holderId: IsNull() && "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - next_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgRootId: requestBody.id, - next_holderId: IsNull() && "", - }, - }); - break; - } - case 1: { - const NodeId = await this.posMasterRepository.findOne({ - where: { orgChild1Id: requestBody.id }, - }); - if (!NodeId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, - ); + case 1: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild1Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { orgChild1Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild1Id: requestBody.id, + next_holderId: IsNull() && "", + }, + }); + break; } - totalPosition = await this.posMasterRepository.count({ - where: { orgChild1Id: requestBody.id }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgChild1Id: requestBody.id, - current_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild1Id: requestBody.id, - current_holderId: IsNull() && "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild1Id: requestBody.id, - next_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild1Id: requestBody.id, - next_holderId: IsNull() && "", - }, - }); - break; - } - case 2: { - const NodeId = await this.posMasterRepository.findOne({ - where: { orgChild2Id: requestBody.id }, - }); - if (!NodeId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, - ); + case 2: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild2Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { orgChild2Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild2Id: requestBody.id, + next_holderId: IsNull() && "", + }, + }); + break; } - totalPosition = await this.posMasterRepository.count({ - where: { orgChild2Id: requestBody.id }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgChild2Id: requestBody.id, - current_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild2Id: requestBody.id, - current_holderId: IsNull() && "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild2Id: requestBody.id, - next_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild2Id: requestBody.id, - next_holderId: IsNull() && "", - }, - }); - break; - } - case 3: { - const NodeId = await this.posMasterRepository.findOne({ - where: { orgChild3Id: requestBody.id }, - }); - if (!NodeId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, - ); + case 3: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild3Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { orgChild3Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild3Id: requestBody.id, + next_holderId: IsNull() && "", + }, + }); + break; } - totalPosition = await this.posMasterRepository.count({ - where: { orgChild3Id: requestBody.id }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgChild3Id: requestBody.id, - current_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild3Id: requestBody.id, - current_holderId: IsNull() && "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild3Id: requestBody.id, - next_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild3Id: requestBody.id, - next_holderId: IsNull() && "", - }, - }); - break; - } - case 4: { - const NodeId = await this.posMasterRepository.findOne({ - where: { orgChild4Id: requestBody.id }, - }); - if (!NodeId) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, - ); + case 4: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild4Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { orgChild4Id: requestBody.id }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgChild4Id: requestBody.id, + next_holderId: IsNull() && "", + }, + }); + break; } - totalPosition = await this.posMasterRepository.count({ - where: { orgChild4Id: requestBody.id }, - }); - totalPositionCurrentUse = await this.posMasterRepository.count({ - where: { - orgChild4Id: requestBody.id, - current_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionCurrentVacant = await this.posMasterRepository.count({ - where: { - orgChild4Id: requestBody.id, - current_holderId: IsNull() && "", - }, - }); - totalPositionNextUse = await this.posMasterRepository.count({ - where: { - orgChild4Id: requestBody.id, - next_holderId: Not(IsNull()) && Not(""), - }, - }); - totalPositionNextVacant = await this.posMasterRepository.count({ - where: { - orgChild4Id: requestBody.id, - next_holderId: IsNull() && "", - }, - }); - break; + default: + break; + } + } + else { + switch (requestBody.type) { + case 0: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgRootId: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id : IsNull() || "", + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id : IsNull() || "", + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id : IsNull() || "", + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + current_holderId: IsNull() || "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id : IsNull() || "", + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: requestBody.id, + orgChild1Id : IsNull() || "", + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + next_holderId: IsNull() || "", + }, + }); + break; + } + case 1: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild1Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : requestBody.id, + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : requestBody.id, + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : requestBody.id, + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : requestBody.id, + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : requestBody.id, + orgChild2Id : IsNull() || "", + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + next_holderId: IsNull() && "", + }, + }); + break; + } + case 2: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild2Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : requestBody.id, + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : requestBody.id, + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : requestBody.id, + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : requestBody.id, + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : requestBody.id, + orgChild3Id : IsNull() || "", + orgChild4Id : IsNull() || "", + next_holderId: IsNull() && "", + }, + }); + break; + } + case 3: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild3Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : requestBody.id, + orgChild4Id : IsNull() || "", + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : requestBody.id, + orgChild4Id : IsNull() || "", + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : requestBody.id, + orgChild4Id : IsNull() || "", + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : requestBody.id, + orgChild4Id : IsNull() || "", + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : requestBody.id, + orgChild4Id : IsNull() || "", + next_holderId: IsNull() && "", + }, + }); + break; + } + case 4: { + const NodeId = await this.posMasterRepository.findOne({ + where: { orgChild4Id: requestBody.id }, + }); + if (!NodeId) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, + ); + } + totalPosition = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : Not(IsNull()) || Not(""), + orgChild4Id : requestBody.id, + }, + }); + totalPositionCurrentUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : Not(IsNull()) || Not(""), + orgChild4Id : requestBody.id, + current_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionCurrentVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : Not(IsNull()) || Not(""), + orgChild4Id : requestBody.id, + current_holderId: IsNull() && "", + }, + }); + totalPositionNextUse = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : Not(IsNull()) || Not(""), + orgChild4Id : requestBody.id, + next_holderId: Not(IsNull()) && Not(""), + }, + }); + totalPositionNextVacant = await this.posMasterRepository.count({ + where: { + orgRootId: Not(IsNull()) || Not(""), + orgChild1Id : Not(IsNull()) || Not(""), + orgChild2Id : Not(IsNull()) || Not(""), + orgChild3Id : Not(IsNull()) || Not(""), + orgChild4Id : requestBody.id, + next_holderId: IsNull() && "", + }, + }); + break; + } + default: + break; } - default: - break; } summary = {