diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index eb937777..4f2ecdfb 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -33,7 +33,7 @@ import { log } from "console"; @Route("api/v1/org") @Tags("Organization") -// @Security("bearerAuth") +@Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 61df770a..2fe69e48 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 ,Brackets } 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"; @@ -35,7 +35,7 @@ import { OrgChild4 } from "../entities/OrgChild4"; import { Position } from "../entities/Position"; @Route("api/v1/org/pos") @Tags("Position") -// @Security("bearerAuth") +@Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", @@ -112,52 +112,75 @@ 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 + " มีอยู่ในระบบแล้ว"); + 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 + " มีอยู่ในระบบแล้ว"); + 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 + " มีอยู่ในระบบแล้ว"); + 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 + " มีอยู่ในระบบแล้ว"); + 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 + " มีอยู่ในระบบแล้ว"); + 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 + " มีอยู่ในระบบแล้ว"); + 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 + " มีอยู่ในระบบแล้ว"); + const chk_posDictArea = await this.posDictRepository.findOne({ + where: { posDictArea: posDict.posDictArea }, + }); + if (chk_posDictArea) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ด้าน/สาขา: " + chk_posDictArea.posDictArea + " มีอยู่ในระบบแล้ว", + ); } try { @@ -650,6 +673,7 @@ export class PositionController extends Controller { const positions = await this.positionRepository.find({ where: { posMasterId: posMaster.id }, relations: ["posType", "posLevel", "posExecutive"], + order: { lastUpdatedAt: "ASC" }, }); const formattedData = { id: posMaster.id, @@ -808,19 +832,17 @@ export class PositionController extends Controller { } masterId = [...new Set(masterId)]; } - + const conditions = [ { ...checkChildConditions, ...typeCondition, - ...(body.keyword && ( - masterId.length > 0 - ? { id: In(masterId)} - : { posMasterNo: Like(`%${body.keyword}%`) } - - )), + ...(body.keyword && + (masterId.length > 0 + ? { id: In(masterId) } + : { posMasterNo: Like(`%${body.keyword}%`) })), }, - ] + ]; // if (body.keyword) { // conditions.push({ @@ -831,7 +853,7 @@ export class PositionController extends Controller { // const [posMaster, total] = await this.posMasterRepository.findAndCount({ // where: conditions, - // order: { posMasterOrder: "ASC" }, + // order: { posMasterOrder: "ASC" }, // relations: [ // "orgRoot", // "orgChild1", @@ -846,38 +868,34 @@ export class PositionController extends Controller { // }); 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(); + .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) => {