diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index bb93a4af..66c2a8a2 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -18,7 +18,9 @@ import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Profile, CreateProfile, UpdateProfile } from "../entities/Profile"; -import { Not } from "typeorm"; +import { Brackets, In, IsNull, Like, Not } from "typeorm"; +import { OrgRevision } from "../entities/OrgRevision"; +import { PosMaster } from "../entities/PosMaster"; @Route("api/v1/org/profile") @Tags("Profile") @@ -29,6 +31,8 @@ import { Not } from "typeorm"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class ProfileController extends Controller { + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + private posMasterRepository = AppDataSource.getRepository(PosMaster); private profileRepository = AppDataSource.getRepository(Profile); /** @@ -43,7 +47,6 @@ export class ProfileController extends Controller { requestBody: CreateProfile, @Request() request: { user: Record }, ) { - try { const profile = Object.assign(new Profile(), requestBody); profile.createdUserId = request.user.sub; @@ -121,7 +124,16 @@ export class ProfileController extends Controller { async detailProfile(@Path() id: string) { const profile = await this.profileRepository.findOne({ where: { id }, - select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"], + select: [ + "id", + "prefix", + "firstName", + "lastName", + "citizenId", + "position", + "posLevelId", + "posTypeId", + ], }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); @@ -142,7 +154,16 @@ export class ProfileController extends Controller { @Get() async listProfile() { const profile = await this.profileRepository.find({ - select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"], + select: [ + "id", + "prefix", + "firstName", + "lastName", + "citizenId", + "position", + "posLevelId", + "posTypeId", + ], order: { createdAt: "ASC" }, }); @@ -155,4 +176,118 @@ export class ProfileController extends Controller { return error; } } + + /** + * API ค้นหารายชื่อไปครองตำแหน่ง + * + * @summary ORG_063 - ค้นหารายชื่อไปครองตำแหน่ง (ADMIN) #68 + * + */ + @Post("search") + async searchProfileOrg( + @Body() + requestBody: { + position?: string; + posLevelId?: string; + posTypeId?: string; + page: number; + pageSize: number; + keyword?: string; + }, + ) { + try { + const orgRevision = await this.orgRevisionRepository.findOne({ + where: { + orgRevisionIsDraft: true, + orgRevisionIsCurrent: false, + }, + relations: ["posMasters"], + }); + if (!orgRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + } + const [profiles, total] = await this.profileRepository + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.next_holders", "next_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .where( + requestBody.position != null && requestBody.position != "" + ? "profile.position LIKE :position" + : "1=1", + { + position: `%${requestBody.position}%`, + }, + ) + .andWhere( + new Brackets((qb) => { + qb.where( + requestBody.keyword != null && requestBody.keyword != "" + ? "profile.prefix LIKE :keyword" + : "1=1", + { + keyword: `%${requestBody.keyword}%`, + }, + ) + .orWhere( + requestBody.keyword != null && requestBody.keyword != "" + ? "profile.firstName LIKE :keyword" + : "1=1", + { + keyword: `%${requestBody.keyword}%`, + }, + ) + .orWhere( + requestBody.keyword != null && requestBody.keyword != "" + ? "profile.lastName LIKE :keyword" + : "1=1", + { + keyword: `%${requestBody.keyword}%`, + }, + ); + }), + ) + .andWhere( + requestBody.posTypeId != null && requestBody.posTypeId != "" + ? "profile.posTypeId LIKE :posTypeId" + : "1=1", + { + posTypeId: `%${requestBody.posTypeId}%`, + }, + ) + .andWhere( + requestBody.posLevelId != null && requestBody.posLevelId != "" + ? "profile.posLevelId LIKE :posLevelId" + : "1=1", + { + posLevelId: `%${requestBody.posLevelId}%`, + }, + ) + .andWhere( + new Brackets((qb) => { + qb.where("next_holders.id IS NULL").orWhere("next_holders.id NOT IN (:...ids)", { + ids: orgRevision.posMasters.map((x) => x.id), + }); + }), + ) + .skip((requestBody.page - 1) * requestBody.pageSize) + .take(requestBody.pageSize) + .getManyAndCount(); + + const data = profiles.map((_data) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + position: _data.position, + })); + + return new HttpSuccess({ data: data, total }); + } catch (error) { + return error; + } + } }