From ee1589131094e009114be4a29c2b98b656398f92 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 1 Feb 2024 11:12:44 +0700 Subject: [PATCH] =?UTF-8?q?org=5F053=20=E0=B8=A3=E0=B8=B2=E0=B8=A2?= =?UTF-8?q?=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=AD=E0=B8=B1=E0=B8=95=E0=B8=A3?= =?UTF-8?q?=E0=B8=B2=E0=B8=81=E0=B8=B3=E0=B8=A5=E0=B8=B1=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PositionController.ts | 86 +++++++++++++++++---------- 1 file changed, 56 insertions(+), 30 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index f86d46d9..36a198a4 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -24,7 +24,7 @@ import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { CreatePosDict, PosDict } from "../entities/PosDict"; import HttpError from "../interfaces/http-error"; -import { In, Like } from "typeorm"; +import { In, IsNull, Like } from "typeorm"; import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; @@ -711,23 +711,44 @@ export class PositionController extends Controller { ) { try { let typeCondition: any = {}; + let checkChildConditions: any = {}; if (body.type === 0) { typeCondition = { orgRootId: body.id, }; + if (!body.isAll) { + checkChildConditions = { + orgChild1Id: IsNull(), + }; + } } else if (body.type === 1) { typeCondition = { orgChild1Id: body.id, }; + if (!body.isAll) { + checkChildConditions = { + orgChild2Id: IsNull(), + }; + } } else if (body.type === 2) { typeCondition = { orgChild2Id: body.id, }; + if (!body.isAll) { + checkChildConditions = { + orgChild3Id: IsNull(), + }; + } } else if (body.type === 3) { typeCondition = { orgChild3Id: body.id, }; + if (!body.isAll) { + checkChildConditions = { + orgChild4Id: IsNull(), + }; + } } else if (body.type === 4) { typeCondition = { orgChild4Id: body.id, @@ -735,38 +756,43 @@ export class PositionController extends Controller { } const posMaster = await this.posMasterRepository.find({ - where: typeCondition, + where: { + ...typeCondition, + ...checkChildConditions, + }, }); - if (!posMaster || posMaster.length === 0 ) { + if (!posMaster || posMaster.length === 0) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - const formattedData = await Promise.all(posMaster.map(async (posMaster) => { - const positions = await this.positionRepository.find({ - where: { posMasterId: posMaster.id }, - }); - - return { - id: posMaster.id, - posMasterNoPrefix: posMaster.posMasterNoPrefix, - posMasterNo: posMaster.posMasterNo, - posMasterNoSuffix: posMaster.posMasterNoSuffix, - positions: positions.map((position) => ({ - id: position.id, - positionName: position.positionName, - positionField: position.positionField, - posTypeId: position.posTypeId, - posTypeName: position.posType == null ? null : position.posType.posTypeName, - posLevelId: position.posLevelId, - posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, - posExecutiveId: position.posExecutiveId, - posExecutiveName: - position.posExecutive == null ? null : position.posExecutive.posExecutiveName, - positionExecutiveField: position.positionExecutiveField, - positionArea: position.positionArea, - positionIsSelected: position.positionIsSelected, - })), - }; - })); + const formattedData = await Promise.all( + posMaster.map(async (posMaster) => { + const positions = await this.positionRepository.find({ + where: { posMasterId: posMaster.id }, + }); + + return { + id: posMaster.id, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positions: positions.map((position) => ({ + id: position.id, + positionName: position.positionName, + positionField: position.positionField, + posTypeId: position.posTypeId, + posTypeName: position.posType == null ? null : position.posType.posTypeName, + posLevelId: position.posLevelId, + posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, + posExecutiveId: position.posExecutiveId, + posExecutiveName: + position.posExecutive == null ? null : position.posExecutive.posExecutiveName, + positionExecutiveField: position.positionExecutiveField, + positionArea: position.positionArea, + positionIsSelected: position.positionIsSelected, + })), + }; + }), + ); return new HttpSuccess(formattedData); } catch (error) { return error;