From 009e315378ea87589b8805ccaf6a69cbcd5e0898 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 1 Feb 2024 17:28:47 +0700 Subject: [PATCH] =?UTF-8?q?org=5F053=20v.queryBuilder=20=E0=B8=A2=E0=B8=B1?= =?UTF-8?q?=E0=B8=87=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B9=80=E0=B8=AA=E0=B8=A3?= =?UTF-8?q?=E0=B9=87=E0=B8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PositionController.ts | 282 +++++++++++++++----------- 1 file changed, 159 insertions(+), 123 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 346cc35f..e25ceaf6 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -33,6 +33,7 @@ import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { Position } from "../entities/Position"; +import { Brackets } from "typeorm/browser"; @Route("api/v1/org/pos") @Tags("Position") @Security("bearerAuth") @@ -733,103 +734,140 @@ export class PositionController extends Controller { 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, - }; - } - const keywordCondition = {} - ? { - posMasterNoPrefix: Like(`%${body.keyword}%`), - positionName: Like(`%${body.keyword}%`), - posTypeName: Like(`%${body.keyword}%`), - posLevelName: Like(`%${body.keyword}%`), - } - : {}; - - const posMaster = await this.posMasterRepository.find({ - where: { - ...typeCondition, - ...checkChildConditions, - ...keywordCondition, - }, - order: { posMasterOrder: "ASC" }, - skip: (body.page - 1) * body.pageSize, - take: body.pageSize, - }); - const total = posMaster.length; + // if (body.type === 0) { + // data = data + // .andWhere("position.orgRootId = :orgRootId", { orgRootId: body.id }); - // if (!posMaster || posMaster.length === 0) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // 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, + // }; // } - const formattedData = await Promise.all( - posMaster.map(async (posMaster) => { - const positions = await this.positionRepository.find({ - where: { posMasterId: posMaster.id }, - relations: ["posLevel", "posType", "posExecutive"], - }); + // const keywordPosMasterCondition = body.keyword + // ? { + // posMasterNoPrefix: Like(`%${body.keyword}%`), + // positionName: Like(`%${body.keyword}%`), + // posTypeName: Like(`%${body.keyword}%`), + // posLevelName: Like(`%${body.keyword}%`), + // } + // : {}; - 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({ data: formattedData, total }); + let data = AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoin("posMaster.positions", "position") + .leftJoin("position.posType", "posType") + .leftJoin("position.posLevel", "posLevel") + .andWhere("posMaster.posMasterNoPrefix LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("position.positionName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("posType.posTypeName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orderBy("posMaster.posMasterOrder", "ASC") + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize); + + if (body.type === 0) { + data = data.andWhere("posMaster.orgRootId = :orgRootId", { orgRootId: body.id }); + if (!body.isAll) { + data = data.andWhere("posMaster.orgChild1Id IS NULL"); + } + } else if (body.type === 1) { + data = data.andWhere("posMaster.orgChild1Id = :orgChild1Id", { orgChild1Id: body.id }); + if (!body.isAll) { + data = data.andWhere("posMaster.orgChild2Id IS NULL"); + } + } else if (body.type === 2) { + data = data.andWhere("posMaster.orgChild2Id = :orgChild2Id", { orgChild2Id: body.id }); + if (!body.isAll) { + data = data.andWhere("posMaster.orgChild3Id IS NULL"); + } + } else if (body.type === 3) { + data = data.andWhere("posMaster.orgChild3Id = :orgChild3Id", { orgChild3Id: body.id }); + if (!body.isAll) { + data = data.andWhere("posMaster.orgChild4Id IS NULL"); + } + } else if (body.type === 4) { + data = data.andWhere("posMaster.orgChild4Id = :orgChild4Id", { orgChild4Id: body.id }); + } + + const [posMaster, total] = await data.getManyAndCount(); + + // const posMaster = await this.posMasterRepository.find({ + // where: { + // ...typeCondition, + // ...checkChildConditions, + // }, + // order: { posMasterOrder: "ASC" }, + // skip: (body.page - 1) * body.pageSize, + // take: body.pageSize, + // }); + // const total = posMaster.length; + + const formattedData = await Promise.all( + posMaster.map(async (posMaster) => { + const positions = await this.positionRepository.find({ + where: { + posMasterId: posMaster.id, + }, + relations: ["posLevel", "posType", "posExecutive"], + }); + + 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({ data: formattedData, total }); } catch (error) { return error; } @@ -1000,27 +1038,27 @@ export class PositionController extends Controller { * @summary ORG_041 - เพิ่มตำแหน่งทางการบริหาร (ADMIN) #44 * */ - @Post("executive") - async createPosExecutive( + @Post("executive") + async createPosExecutive( @Body() - requestBody: CreatePosMaster, - @Request() request: { user: Record } - ) { + requestBody: CreatePosMaster, + @Request() request: { user: Record }, + ) { const posExecutive = Object.assign(new PosExecutive(), requestBody); if (!posExecutive) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - try { - posExecutive.createdUserId = request.user.sub; - posExecutive.createdFullName = request.user.name; - posExecutive.lastUpdateUserId = request.user.sub; - posExecutive.lastUpdateFullName = request.user.name; - await this.posExecutiveRepository.save(posExecutive); - return new HttpSuccess(); - } catch (error) { - return error; - } + try { + posExecutive.createdUserId = request.user.sub; + posExecutive.createdFullName = request.user.name; + posExecutive.lastUpdateUserId = request.user.sub; + posExecutive.lastUpdateFullName = request.user.name; + await this.posExecutiveRepository.save(posExecutive); + return new HttpSuccess(); + } catch (error) { + return error; } + } /** * API แก้ไขตำแหน่งทางการบริหาร @@ -1029,28 +1067,27 @@ export class PositionController extends Controller { * * @param {string} id Id ตำแหน่งทางการบริหาร */ - @Put("executive/{id}") - async updatePosExecutive( + @Put("executive/{id}") + async updatePosExecutive( @Path() id: string, @Body() - requestBody: CreatePosMaster, - @Request() request: { user: Record } - ) { + requestBody: CreatePosMaster, + @Request() request: { user: Record }, + ) { const posExecutive = Object.assign(new PosExecutive(), requestBody); if (!posExecutive) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } - try { - posExecutive.lastUpdateUserId = request.user.sub; - posExecutive.lastUpdateFullName = request.user.name; - await this.posExecutiveRepository.save(posExecutive); - return new HttpSuccess(); - } catch (error) { - return error; - } + try { + posExecutive.lastUpdateUserId = request.user.sub; + posExecutive.lastUpdateFullName = request.user.name; + await this.posExecutiveRepository.save(posExecutive); + return new HttpSuccess(); + } catch (error) { + return error; } + } - /** * API ลบอัตรากำลัง * @@ -1074,5 +1111,4 @@ export class PositionController extends Controller { return error; } } - }