diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 93d2677a..f714e217 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -1228,10 +1228,10 @@ export class ProfileController extends Controller { * * */ - @Post("commander-director")//page searchxxxxxxxxxxxxxxxxxxxxxxxx + @Post("commander-director") async getProfileCommanderDirector( @Request() request: RequestWithUser, - @Body() body: { isDirector: boolean }, + @Body() body: { isDirector: boolean; page: number; pageSize: number; keyword: string }, ) { const posMaster = await this.posMasterRepo.findOne({ where: { @@ -1241,15 +1241,78 @@ export class ProfileController extends Controller { }); if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); if (body.isDirector == true) { - const _posMaster = await this.posMasterRepo.find({ - where: { + const [_posMaster, total] = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .leftJoinAndSelect("current_holder.posLevel", "posLevel") + .leftJoinAndSelect("current_holder.posType", "posType") + .where({ orgRootId: posMaster.orgRootId || "", orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, isDirector: true, current_holderId: Not(IsNull()), - }, - relations: ["current_holder", "current_holder.posLevel", "current_holder.posType"], - }); + }) + .andWhere( + new Brackets((qb) => { + qb.where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "CONCAT(current_holder.prefix, current_holder.firstName, ' ', current_holder.lastName) LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "current_holder.citizenId LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "current_holder.position LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "posLevel.posLevelName LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "posType.posTypeName LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ); + }), + ) + .select([ + "posMaster.current_holderId", + "current_holder.prefix", + "current_holder.firstName", + "current_holder.lastName", + "current_holder.citizenId", + "current_holder.position", + "current_holder.posLevel", + "posLevel.posLevelName", + "current_holder.posType", + "posType.posTypeName", + "posMaster.isDirector", + ]) + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + const posMasterActs = await this.posMasterActRepository.find({ where: { posMasterId: In(_posMaster.map((x) => x.id)), @@ -1299,16 +1362,79 @@ export class ProfileController extends Controller { }; data.push(item); }); - return new HttpSuccess(data); + return new HttpSuccess({ data, total }); } else { - const _posMaster = await this.posMasterRepo.find({ - where: { + const [_posMaster, total] = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .leftJoinAndSelect("current_holder.posLevel", "posLevel") + .leftJoinAndSelect("current_holder.posType", "posType") + .where({ orgRootId: posMaster.orgRootId || "", orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, current_holderId: Not(IsNull()), - }, - relations: ["current_holder", "current_holder.posLevel", "current_holder.posType"], - }); + }) + .andWhere( + new Brackets((qb) => { + qb.where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "CONCAT(current_holder.prefix, current_holder.firstName, ' ', current_holder.lastName) LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "current_holder.citizenId LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "current_holder.position LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "posLevel.posLevelName LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .where( + body.keyword != undefined && body.keyword != null && body.keyword != "" + ? "posType.posTypeName LIKE :keyword" + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ); + }), + ) + .select([ + "posMaster.current_holderId", + "current_holder.prefix", + "current_holder.firstName", + "current_holder.lastName", + "current_holder.citizenId", + "current_holder.position", + "current_holder.posLevel", + "posLevel.posLevelName", + "current_holder.posType", + "posType.posTypeName", + "posMaster.isDirector", + ]) + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + let data = _posMaster.map((_data) => ({ id: _data.current_holderId || null, prefix: _data.current_holder?.prefix || "", @@ -1327,7 +1453,7 @@ export class ProfileController extends Controller { isDirector: _data.isDirector || false, actFullName: null, })); - return new HttpSuccess(data); + return new HttpSuccess({ data, total }); } }