diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 346184b2..61df770a 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, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", @@ -112,6 +111,55 @@ export class PositionController extends Controller { } } + const chk_posDictName = await this.posDictRepository.findOne({ + where: { posDictName: posDict.posDictName } + }) + if(chk_posDictName){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ตำแหน่งในสายงาน: " + chk_posDictName.posDictName + " มีอยู่ในระบบแล้ว"); + } + + const chk_posDictField = await this.posDictRepository.findOne({ + where: { posDictField: posDict.posDictField } + }) + if(chk_posDictField){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "สายงาน: " + chk_posDictField.posDictField + " มีอยู่ในระบบแล้ว"); + } + + const chk_posTypeId = await this.posDictRepository.findOne({ + where: { posTypeId: posDict.posTypeId } + }) + if(chk_posTypeId){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ตำแหน่งประเภท: " + chk_posTypeId.posTypeId + " มีอยู่ในระบบแล้ว"); + } + + const chk_posLevelId = await this.posDictRepository.findOne({ + where: { posLevelId: posDict.posLevelId } + }) + if(chk_posLevelId){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับตำแหน่ง: " + chk_posLevelId.posLevelId + " มีอยู่ในระบบแล้ว"); + } + + const chk_posExecutiveId = await this.posDictRepository.findOne({ + where: { posExecutiveId: String(posDict.posExecutiveId) } + }) + if(chk_posExecutiveId){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ตำแหน่งทางการบริหาร: " + chk_posExecutiveId.posExecutiveId + " มีอยู่ในระบบแล้ว"); + } + + const chk_posDictExecutiveField = await this.posDictRepository.findOne({ + where: { posDictExecutiveField: posDict.posDictExecutiveField } + }) + if(chk_posDictExecutiveField){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ด้านทางการบริหาร: " + chk_posDictExecutiveField.posDictExecutiveField + " มีอยู่ในระบบแล้ว"); + } + + const chk_posDictArea= await this.posDictRepository.findOne({ + where: { posDictArea: posDict.posDictArea } + }) + if(chk_posDictArea){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ด้าน/สาขา: " + chk_posDictArea.posDictArea + " มีอยู่ในระบบแล้ว"); + } + try { posDict.createdUserId = request.user.sub; posDict.createdFullName = request.user.name; @@ -760,39 +808,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) => {