diff --git a/src/controllers/ChangePositionController.ts b/src/controllers/ChangePositionController.ts index 9cffa825..3e224b28 100644 --- a/src/controllers/ChangePositionController.ts +++ b/src/controllers/ChangePositionController.ts @@ -165,8 +165,8 @@ export class ChangePositionController extends Controller { .createQueryBuilder("changePosition") .leftJoinAndSelect("changePosition.profileChangePosition", "profileChangePosition") .where( - searchKeyword ? - "changePosition.name LIKE :keyword OR changePosition.date LIKE :keyword OR changePosition.status LIKE :keyword" + searchKeyword + ? "changePosition.name LIKE :keyword OR changePosition.date LIKE :keyword OR changePosition.status LIKE :keyword" : "1=1", { keyword: `%${searchKeyword}%` } ) @@ -262,33 +262,68 @@ export class ChangePositionController extends Controller { @Query() searchKeyword: string = "", ) { - const findData = await this.profileChangePositionRepository.find({ - where: { changePositionId: changePositionId } - }); - if (!findData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง"); - const [profileChangePosition, total] = await AppDataSource.getRepository(ProfileChangePosition) .createQueryBuilder("profileChangePosition") - .where("profileChangePosition.changePositionId LIKE :id", { id: changePositionId }) + .where({ changePositionId: changePositionId }) .andWhere( - searchKeyword ? - "CONCAT(profileChangePosition.prefix, profileChangePosition.firstName, ' ', profileChangePosition.lastName) LIKE :keyword" - : "1=1", - { - keyword: `%${searchKeyword}%`, - }, - ) - .andWhere( - searchKeyword ? - "profileChangePosition.citizenId LIKE :keyword OR profileChangePosition.status LIKE :keyword" - : "1=1", - { keyword: `%${searchKeyword}%` } - ) - .andWhere( - searchKeyword ? - "profileChangePosition.birthDate LIKE :keyword OR profileChangePosition.lastUpdatedAt LIKE :keyword" - : "1=1", - { keyword: `%${searchKeyword}%` } + new Brackets((qb) => { + qb.where( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileChangePosition.prefix LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileChangePosition.firstName LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileChangePosition.lastName LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileChangePosition.citizenId LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileChangePosition.birthDate LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileChangePosition.lastUpdatedAt LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileChangePosition.status LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + }), ) .orderBy("profileChangePosition.createdAt", "ASC") .skip((page - 1) * pageSize)