From 53b21460579514938b23e8ffd7f9d10cd4bff4be Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 31 Jan 2024 15:02:54 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20query=20=E0=B8=84?= =?UTF-8?q?=E0=B9=89=E0=B8=99=E0=B8=AB=E0=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OrganizationController.ts | 1 - src/controllers/PositionController.ts | 87 +++++++---------------- 2 files changed, 27 insertions(+), 61 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 0705c0d5..5eeffbd7 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -787,6 +787,5 @@ export class OrganizationController extends Controller { })); return new HttpSuccess(_data); } - return new HttpSuccess(_data); } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 10164b6d..b5dd6343 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 { Like } from "typeorm"; +import { In, Like } from "typeorm"; import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; @@ -267,13 +267,14 @@ export class PositionController extends Controller { * */ @Get("position") - async findPosition(@Query("keyword") keyword: string, @Query("type") type: string) { + async findPosition(@Query("keyword") keyword?: string, @Query("type") type?: string) { try { let findPosDict: any; switch (type) { case "positionName": findPosDict = await this.posDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -283,6 +284,7 @@ export class PositionController extends Controller { case "positionField": findPosDict = await this.posDictRepository.find({ where: { posDictField: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -292,61 +294,40 @@ export class PositionController extends Controller { case "positionType": const findTypes: PosType[] = await this.posTypeRepository.find({ where: { posTypeName: Like(`%${keyword}%`) }, + select: ["id"], + }); + findPosDict = await this.posDictRepository.find({ + where: { posTypeId: In(findTypes.map((x) => x.id)) }, + relations: ["posType", "posLevel", "posExecutive"], }); - if (!findTypes) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword + " ใน posType"); - } - for (const types of findTypes) { - findPosDict = await this.posDictRepository.find({ where: { posTypeId: types.id } }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } - } break; case "positionLevel": const findLevel: PosLevel[] = await this.posLevelRepository.find({ where: { posLevelName: Like(`%${keyword}%`) }, + select: ["id"], + }); + findPosDict = await this.posDictRepository.find({ + where: { posLevelId: In(findLevel.map((x) => x.id)) }, + relations: ["posType", "posLevel", "posExecutive"], }); - if (!findLevel) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูล " + keyword + " ใน posLevel", - ); - } - - for (const types of findLevel) { - findPosDict = await this.posDictRepository.find({ where: { posLevelId: types.id } }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } - } break; case "positionExecutive": const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ where: { posExecutiveName: Like(`%${keyword}%`) }, + select: ["id"], + }); + findPosDict = await this.posDictRepository.find({ + where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, + relations: ["posType", "posLevel", "posExecutive"], }); - if (!findExecutive) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูล " + keyword + " ใน posExecutive", - ); - } - - for (const types of findExecutive) { - findPosDict = await this.posDictRepository.find({ - where: { posExecutiveId: types.id }, - }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } - } break; case "positionExecutiveField": findPosDict = await this.posDictRepository.find({ where: { posDictExecutiveField: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -356,6 +337,7 @@ export class PositionController extends Controller { case "positionArea": findPosDict = await this.posDictRepository.find({ where: { posDictArea: Like(`%${keyword}%`) }, + relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -363,42 +345,27 @@ export class PositionController extends Controller { break; default: - findPosDict = await this.posDictRepository.find(); + findPosDict = await this.posDictRepository.find({ + relations: ["posType", "posLevel", "posExecutive"], + }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } break; } - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } - const mapDataPosDict = await Promise.all( findPosDict.map(async (item: any) => { - const posTypeName = await this.posTypeRepository.findOne({ - where: { id: item.posTypeId }, - select: ["posTypeName"], - }); - const posLevelName = await this.posLevelRepository.findOne({ - where: { id: item.posLevelId }, - select: ["posLevelName"], - }); - const posExecutiveName = await this.posExecutiveRepository.findOne({ - where: { id: item.posExecutiveId }, - select: ["posExecutiveName"], - }); - return { id: item.id, positionName: item.posDictName, positionField: item.posDictField, posTypeId: item.posTypeId, - posTypeName: posTypeName ? posTypeName.posTypeName : null, + posTypeName: item.posType == null ? null : item.posType.posTypeName, posLevelId: item.posLevelId, - posLevelName: posLevelName ? posLevelName.posLevelName : null, + posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, posExecutiveId: item.posExecutiveId, - posExecutiveName: posExecutiveName ? posExecutiveName.posExecutiveName : null, + posExecutiveName: item.posExecutive == null ? null : item.posExecutive.posExecutiveName, positionExecutiveField: item.posDictExecutiveField, positionArea: item.posDictArea, positionIsSelected: false,