diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 346184b2..72e04ed1 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 { Equal, ILike, In, IsNull, Like, Not } from "typeorm"; +import { Equal, ILike, In, IsNull, Like, Not ,Brackets } from "typeorm"; import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; @@ -33,10 +33,9 @@ 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") +// @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", @@ -760,39 +759,76 @@ export class PositionController extends Controller { } masterId = [...new Set(masterId)]; } - - const keywordConditions = [ + + const conditions = [ { ...checkChildConditions, ...typeCondition, + ...(body.keyword && ( + masterId.length > 0 + ? { id: In(masterId)} + : { posMasterNo: Like(`%${body.keyword}%`) } + + )), }, - // { - // ...checkChildConditions, - // ...typeCondition, - // id: masterId.length > 0 ? In(masterId) : false, - // }, - // { - // ...checkChildConditions, - // ...typeCondition, - // posMasterNo: Like(`%${keywordAsInt}%`), - // }, - ]; + ] - const [posMaster, total] = await this.posMasterRepository.findAndCount({ - where: keywordConditions, - order: { posMasterOrder: "ASC" }, - relations: [ - "orgRoot", - "orgChild1", - "orgChild2", - "orgChild3", - "orgChild4", - "current_holder", - "next_holder", - ], - skip: (body.page - 1) * body.pageSize, - take: body.pageSize, - }); + // if (body.keyword) { + // conditions.push({ + // id: masterId.length > 0 ? In(masterId) : false, + // posMasterNo: Like(`%${keywordAsInt}%`), + // }); + // } + + // const [posMaster, total] = await this.posMasterRepository.findAndCount({ + // where: conditions, + // order: { posMasterOrder: "ASC" }, + // relations: [ + // "orgRoot", + // "orgChild1", + // "orgChild2", + // "orgChild3", + // "orgChild4", + // "current_holder", + // "next_holder", + // ], + // skip: (body.page - 1) * body.pageSize, + // take: body.pageSize, + // }); + + const [posMaster, total] = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder('posMaster') + .leftJoinAndSelect('posMaster.orgRoot', 'orgRoot') + .leftJoinAndSelect('posMaster.orgChild1', 'orgChild1') + .leftJoinAndSelect('posMaster.orgChild2', 'orgChild2') + .leftJoinAndSelect('posMaster.orgChild3', 'orgChild3') + .leftJoinAndSelect('posMaster.orgChild4', 'orgChild4') + .leftJoinAndSelect('posMaster.current_holder', 'current_holder') + .leftJoinAndSelect('posMaster.next_holder', 'next_holder') + .where(conditions) + // .orWhere('(current_holder.prefix LIKE :keyword OR current_holder.firstName LIKE :keyword OR current_holder.lastName LIKE :keyword)', { keyword: `%${body.keyword}%` }) + .orWhere( + new Brackets((qb) => { + qb.where( + body.keyword != null && body.keyword != "" + ? "current_holder.prefix LIKE :keyword OR current_holder.firstName LIKE :keyword OR current_holder.lastName LIKE :keyword OR :keyword LIKE current_holder.prefix OR :keyword LIKE current_holder.firstName OR :keyword LIKE current_holder.lastName" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .andWhere( + checkChildConditions + ) + .andWhere( + typeCondition + ); + }) + ) + .orderBy('posMaster.posMasterOrder', 'ASC') + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); const formattedData = await Promise.all( posMaster.map(async (posMaster) => {