import { Controller, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, SuccessResponse, Response, Get, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; 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 { Brackets, In, IsNull, Like, Not } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; import { PosMaster } from "../entities/PosMaster"; import { PosLevel } from "../entities/PosLevel"; import { PosType } from "../entities/PosType"; import { request } from "http"; @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class ProfileController extends Controller { private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private posMasterRepository = AppDataSource.getRepository(PosMaster); private profileRepository = AppDataSource.getRepository(Profile); private posLevelRepository = AppDataSource.getRepository(PosLevel); private posTypeRepository = AppDataSource.getRepository(PosType); /** * API สร้างทะเบียนประวัติ * * @summary ORG_065 - สร้างทะเบียนประวัติ (ADMIN) #70 * */ @Post() async createProfile( @Body() requestBody: CreateProfile, @Request() request: { user: Record }, ) { const _profile = await this.profileRepository.findOne({ where: { citizenId: requestBody.citizenId }, }); if (_profile) { throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "เลขบัตรนี้มีอยู่ในระบบแล้ว"); } if (requestBody.posLevelId == "") { requestBody.posLevelId = null; } if (requestBody.posLevelId) { const checkPosLevel = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelId }, }); if (!checkPosLevel) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId, ); } } if (requestBody.posTypeId == "") { requestBody.posTypeId = null; } if (requestBody.posTypeId) { const checkPosType = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeId }, }); if (!checkPosType) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId, ); } } const checkCitizenId = await this.profileRepository.findOne({ where: { citizenId: requestBody.citizenId }, }); if (checkCitizenId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "มีรหัสประจำตัวประชาชนนี้แล้วในระบบ (citizenId)", ); } try { const profile = Object.assign(new Profile(), requestBody); profile.createdUserId = request.user.sub; profile.createdFullName = request.user.name; profile.lastUpdateUserId = request.user.sub; profile.lastUpdateFullName = request.user.name; await this.profileRepository.save(profile); return new HttpSuccess(); } catch (error) { return error; } } /** * API แก้ไขทะเบียนประวัติ * * @summary ORG_065 - แก้ไขทะเบียนประวัติ (ADMIN) #70 * * @param {string} id Id ทะเบียนประวัติ */ @Put("{id}") async updateProfile( @Path() id: string, @Body() requestBody: CreateProfile, @Request() request: { user: Record }, ) { const profile = await this.profileRepository.findOne({ where: { id: id } }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } const _profile = await this.profileRepository.findOne({ where: { id: Not(id), citizenId: requestBody.citizenId }, }); if (_profile) { throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "เลขบัตรนี้มีอยู่ในระบบแล้ว"); } if (requestBody.posLevelId == "") { requestBody.posLevelId = null; } if (requestBody.posLevelId) { const checkPosLevel = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelId }, }); if (!checkPosLevel) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId, ); } } if (requestBody.posTypeId == "") { requestBody.posTypeId = null; } if (requestBody.posTypeId) { const checkPosType = await this.posTypeRepository.findOne({ where: { id: requestBody.posTypeId }, }); if (!checkPosType) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId, ); } } const checkCitizenId = await this.profileRepository.findOne({ where: { id: Not(id), citizenId: requestBody.citizenId, }, }); if (checkCitizenId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "มีรหัสประจำตัวประชาชนนี้แล้วในระบบ (citizenId)", ); } try { profile.lastUpdateUserId = request.user.sub; profile.lastUpdateFullName = request.user.name; this.profileRepository.merge(profile, requestBody); await this.profileRepository.save(profile); return new HttpSuccess(); } catch (error) { return error; } } /** * API ลบทะเบียนประวัติ * * @summary ORG_065 - ลบทะเบียนประวัติ (ADMIN) #70 * * @param {string} id Id ทะเบียนประวัติ */ @Delete("{id}") async deleteProfile(@Path() id: string) { const delProfile = await this.profileRepository.findOne({ where: { id }, }); if (!delProfile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } try { await this.profileRepository.delete({ id: id }); return new HttpSuccess(); } catch (error) { return error; } } /** * API รายละเอียดรายการทะเบียนประวัติ * * @summary ORG_065 - รายละเอียดรายการทะเบียนประวัติ (ADMIN) #70 * * @param {string} id Id ทะเบียนประวัติ */ @Get("{id}") async detailProfile(@Path() id: string) { const profile = await this.profileRepository.findOne({ where: { id }, select: [ "id", "prefix", "firstName", "lastName", "citizenId", "position", "posLevelId", "posTypeId", ], }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } try { return new HttpSuccess(profile); } catch (error) { return error; } } /** * API รายการทะเบียนประวัติ * * @summary ORG_065 - รายการทะเบียนประวัติ (ADMIN) #70 * */ @Get() async listProfile( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { const [profile, total] = await this.profileRepository.findAndCount({ select: [ "id", "prefix", "firstName", "lastName", "citizenId", "position", "posLevelId", "posTypeId", ], order: { createdAt: "ASC" }, skip: (page - 1) * pageSize, take: pageSize, }); if (keyword != undefined && keyword !== "") { const formattedKeyword = keyword.toLowerCase().replace(/\s+/g, ""); const filteredProfile = profile.filter( (x) => (x.prefix + x.firstName + x.lastName).replace(/\s+/g, "").includes(formattedKeyword) || x.citizenId?.toString().includes(keyword) || x.position?.toString().includes(keyword), ); const formattedData = filteredProfile.map((item) => ({ id: item.id, prefix: item.prefix, firstName: item.firstName, lastName: item.lastName, citizenId: item.citizenId, position: item.position, posLevelId: item.posLevelId, posTypeId: item.posTypeId, })); return new HttpSuccess({ data: formattedData, total: formattedData.length }); } if (!profile) { return new HttpSuccess([]); } const formattedData = profile.map((item) => ({ id: item.id, prefix: item.prefix, firstName: item.firstName, lastName: item.lastName, citizenId: item.citizenId, position: item.position, posLevelId: item.posLevelId, posTypeId: item.posTypeId, })); return new HttpSuccess({ data: formattedData, total }); } /** * 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}%`, }, ) .orWhere( requestBody.keyword != null && requestBody.keyword != "" ? "profile.citizenId 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; } } /** * API ข้อมูลทะเบียนประวัติตาม keycloak * * @summary ORG_065 - ข้อมูลทะเบียนประวัติตาม keycloak (ADMIN) #70 * */ @Get("keycloak/position") async getProfileByKeycloak(@Request() request: { user: Record }) { const profile = await this.profileRepository.findOne({ where: { keycloak: request.user.sub }, relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"], }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); } const orgRevisionPublish = await this.orgRevisionRepository .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); if (!orgRevisionPublish) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); } const _profile = { profileId: profile.id, prefix: profile.prefix, firstName: profile.firstName, lastName: profile.lastName, citizenId: profile.citizenId, position: profile.position, posLevelName: profile.posLevel == null ? null : profile.posLevel.posLevelName, posLevelRank: profile.posLevel == null ? null : profile.posLevel.posLevelRank, posLevelId: profile.posLevel == null ? null : profile.posLevel.id, posTypeName: profile.posType == null ? null : profile.posType.posTypeName, posTypeRank: profile.posType == null ? null : profile.posType.posTypeRank, posTypeId: profile.posType == null ? null : profile.posType.id, rootId: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) ?.orgRootId, root: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot .orgRootName, child1Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild1 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) ?.orgChild1Id, child1: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild1 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild1 .orgChild1Name, child2Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild2 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) ?.orgChild2Id, child2: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild2 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild2 .orgChild2Name, child3Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild3 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) ?.orgChild3Id, child3: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild3 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild3 .orgChild3Name, child4Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) ?.orgChild4Id, child4: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4 .orgChild4Name, }; try { return new HttpSuccess(_profile); } catch (error: any) { throw new Error(error); } } /** * API ค้นหาข้อมูลทะเบียนประวัติ * * @summary ORG_065 - ค้นหาข้อมูลทะเบียนประวัติ (ADMIN) #70 * */ @Post("search-personal") async getProfileBySearchKeyword( @Body() body: { fieldName: string; keyword?: string; }, ) { try { let findProfile: any; switch (body.fieldName) { case "idcard": findProfile = await this.profileRepository.find({ where: { citizenId: Like(`%${body.keyword}%`) }, relations: ["posType", "posLevel"], }); break; case "firstname": findProfile = await this.profileRepository.find({ where: { firstName: Like(`%${body.keyword}%`) }, relations: ["posType", "posLevel"], }); break; case "lastname": findProfile = await this.profileRepository.find({ where: { lastName: Like(`%${body.keyword}%`) }, relations: ["posType", "posLevel"], }); break; default: findProfile = await this.profileRepository.find({ relations: ["posType", "posLevel"], }); break; } const mapDataProfile = await Promise.all( findProfile.map(async (item: Profile) => { return { id: item.id, prefix: item.prefix, firstName: item.firstName, lastName: item.lastName, position: item.position, idcard: item.citizenId, email: item.email, phone: item.phone, }; }), ); return new HttpSuccess(mapDataProfile); } catch (error: any) { throw new Error(error); } } /** * API ค้นหาผู้บังคับบัญชา * * @summary ORG_069 - ค้นหาผู้บังคับบัญชา (ADMIN) #75 * */ @Get("search/commander") async searchCommander(@Request() request: { user: Record }) { let fullName_: any = {}; let position_: any = {}; let commanderAboveFullname_: any = {}; let commanderAbovePosition_: any = {}; let commanderFullname_: any = {}; let commanderPosition_: any = {}; const findProfile = await this.profileRepository.findOne({ where: { keycloak: request.user.sub }, }); const findRevision = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, }, }); const findPosMaster = await this.posMasterRepository.findOne({ where: { current_holderId: findProfile?.id, orgRevisionId: findRevision?.id, }, }); let node = 4; let childId = findPosMaster?.orgChild4Id; let condition: any = { orgChild4Id: childId }; if (findPosMaster?.orgChild4Id == null && findPosMaster?.orgChild3Id != null) { node = 3; childId = findPosMaster?.orgChild3Id; condition = { orgChild3Id: childId, orgChild4Id: IsNull() }; } else if (findPosMaster?.orgChild3Id == null && findPosMaster?.orgChild2Id != null) { node = 2; childId = findPosMaster?.orgChild2Id; condition = { orgChild2Id: childId, orgChild3Id: IsNull() }; } else if (findPosMaster?.orgChild2Id == null && findPosMaster?.orgChild1Id != null) { node = 1; childId = findPosMaster?.orgChild1Id; condition = { orgChild1Id: childId, orgChild2Id: IsNull() }; } else if (findPosMaster?.orgChild1Id == null) { node = 0; childId = findPosMaster?.orgRootId; condition = { orgRootId: childId, orgChild1Id: IsNull() }; } const findCmd = await this.posMasterRepository.findOne({ where: { current_holderId: Not(IsNull()) || Not(""), orgRevisionId: findRevision?.id, ...condition, }, relations: ["current_holder"], order: { posMasterOrder: "ASC" }, }); let findOSAB: PosMaster | null = null; let findTSAB: PosMaster | null = null; //หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 1 ขั้น if (node !== 0) { findOSAB = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.current_holder", "current_holder") .where("posMaster.current_holderId IS NOT NULL") .andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: findRevision?.id }) .andWhere( new Brackets((qb) => { if (node === 4) { qb.andWhere("posMaster.orgChild4Id IS NULL"); qb.andWhere("posMaster.orgChild3Id = :childId", { childId: findPosMaster?.orgChild3Id, }); } else if (node === 3) { qb.andWhere("posMaster.orgChild3Id IS NULL"); qb.andWhere("posMaster.orgChild2Id = :childId", { childId: findPosMaster?.orgChild2Id, }); } else if (node === 2) { qb.andWhere("posMaster.orgChild2Id IS NULL"); qb.andWhere("posMaster.orgChild1Id = :childId", { childId: findPosMaster?.orgChild1Id, }); } else if (node === 1) { qb.andWhere("posMaster.orgChild1Id IS NULL"); qb.andWhere("posMaster.orgRootId = :childId", { childId: findPosMaster?.orgRootId }); } }), ) .orderBy("posMaster.posMasterOrder", "ASC") .getOne(); } //หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 2 ขั้น if (node !== 0 && node !== 1) { findTSAB = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.current_holder", "current_holder") .where("posMaster.current_holderId IS NOT NULL") .andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: findRevision?.id }) .andWhere( new Brackets((qb) => { if (node === 4) { qb.andWhere("posMaster.orgChild3Id IS NULL"); qb.andWhere("posMaster.orgChild2Id = :childId", { childId: findPosMaster?.orgChild2Id, }); } else if (node === 3) { qb.andWhere("posMaster.orgChild2Id IS NULL"); qb.andWhere("posMaster.orgChild1Id = :childId", { childId: findPosMaster?.orgChild1Id, }); } else if (node === 2) { qb.andWhere("posMaster.orgChild1Id IS NULL"); qb.andWhere("posMaster.orgRootId = :childId", { childId: findPosMaster?.orgRootId, }); } }), ) .orderBy("posMaster.posMasterOrder", "ASC") .getOne(); } fullName_ = (findProfile?.prefix ?? "") + (findProfile?.firstName ?? "") + " " + (findProfile?.lastName ?? ""); position_ = findProfile?.position ?? ""; commanderFullname_ = (findCmd?.current_holder?.prefix ?? "") + (findCmd?.current_holder?.firstName ?? "") + " " + (findCmd?.current_holder?.lastName ?? ""); commanderPosition_ = findCmd?.current_holder?.position ?? ""; commanderAboveFullname_ = (findOSAB?.current_holder?.prefix ?? "") + (findOSAB?.current_holder?.firstName ?? "") + " " + (findOSAB?.current_holder?.lastName ?? ""); commanderAbovePosition_ = findOSAB?.current_holder?.position ?? ""; if (findCmd?.current_holderId == findProfile?.id) { commanderFullname_ = (findOSAB?.current_holder?.prefix ?? "") + (findOSAB?.current_holder?.firstName ?? "") + " " + (findOSAB?.current_holder?.lastName ?? ""); commanderPosition_ = findOSAB?.current_holder?.position ?? ""; commanderAboveFullname_ = (findTSAB?.current_holder?.prefix ?? "") + (findTSAB?.current_holder?.firstName ?? "") + " " + (findTSAB?.current_holder?.lastName ?? ""); commanderAbovePosition_ = findTSAB?.current_holder?.position ?? ""; const formattedDataTSAB = { fullname: fullName_, position: position_, commanderAboveFullname: commanderAboveFullname_, commanderAbovePosition: commanderAbovePosition_, commanderFullname: commanderFullname_, commanderPosition: commanderPosition_, }; return new HttpSuccess(formattedDataTSAB); } const formattedData = { fullname: fullName_, position: position_, commanderAboveFullname: commanderAboveFullname_, commanderAbovePosition: commanderAbovePosition_, commanderFullname: commanderFullname_, commanderPosition: commanderPosition_, }; return new HttpSuccess(formattedData); } /** * API เช็คเลขบัตร * * @summary เช็คเลขบัตร (ADMIN) * * @param {string} id Id ทะเบียนประวัติ */ @Put("citizenId/{id}") async checkCitizenIdProfile( @Path() id: string, @Body() requestBody: { citizenId: string }, ) { const profile = await this.profileRepository.findOne({ where: { id: Not(id), citizenId: requestBody.citizenId }, }); if (profile) { throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "เลขบัตรนี้มีอยู่ในระบบแล้ว"); } try { return new HttpSuccess(); } catch (error) { return error; } } /** * API ค้นหาข้อมูลทะเบียนประวัติ ทดลองปฏิบัติหน้าที่ราชการ * * @summary ค้นหาข้อมูลทะเบียนประวัติ ทดลองปฏิบัติหน้าที่ราชการ (ADMIN) * */ @Post("probation") async getProfileBySearchKeywordProbation( @Body() body: { page: number; pageSize: number; keyword?: string; }, ) { try { const [findProfile, total] = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .leftJoinAndSelect("profile.posLevel", "posLevel") .leftJoinAndSelect("profile.current_holders", "current_holders") .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") .where("profile.prefix LIKE :keyword", { keyword: `%${body.keyword}%` }) .orWhere("profile.firstName LIKE :keyword", { keyword: `%${body.keyword}%` }) .orWhere("profile.lastName LIKE :keyword", { keyword: `%${body.keyword}%` }) .orWhere("profile.position LIKE :keyword", { keyword: `%${body.keyword}%` }) .orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${body.keyword}%` }) .orderBy("profile.citizenId", "ASC") .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); const orgRevisionActive = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); const mapDataProfile = await Promise.all( findProfile.map(async (item: Profile) => { return { id: item.id, prefix: item.prefix, firstName: item.firstName, lastName: item.lastName, position: item.position, idcard: item.citizenId, posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, isProbation: item.isProbation, orgRootName: item.current_holders == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot ?.orgRootName == null ? null : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgRoot?.orgRootName, orgChild1Name: item.current_holders == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild1 == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild1 ?.orgChild1Name == null ? null : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild1?.orgChild1Name, orgChild2Name: item.current_holders == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild2 == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild2 ?.orgChild2Name == null ? null : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild2?.orgChild2Name, orgChild3Name: item.current_holders == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild3 == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild3 ?.orgChild3Name == null ? null : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild3?.orgChild3Name, orgChild4Name: item.current_holders == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild4 == null || item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild4 ?.orgChild4Name == null ? null : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild4?.orgChild4Name, }; }), ); return new HttpSuccess({ data: mapDataProfile, total }); } catch (error: any) { throw new Error(error); } } /** * API รายชื่อราชการที่เลื่อนเงินเดือน * * @summary ORG_072 - รายชื่อราชการที่เลื่อนเงินเดือน #76 * */ @Post("salary/gen") async salaryGen( @Body() body: { page: number; pageSize: number; keyword?: string; }, ) { const findRevision = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true }, }); if (!findRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); } const findPosMaster = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.current_holder", "current_holder") .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") .leftJoinAndSelect("posMaster.positions", "positions") .leftJoinAndSelect("positions.posExecutive", "posExecutive") .leftJoinAndSelect("current_holder.profileSalary", "profileSalary") .leftJoinAndSelect("current_holder.posLevel", "posLevel") .leftJoinAndSelect("current_holder.posType", "posType") .where({ orgRevisionId: findRevision?.id, current_holderId: Not(IsNull()), }) .andWhere( new Brackets((qb) => { qb.where( body.keyword != null && body.keyword != "" ? "current_holder.prefix LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "current_holder.firstName LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "current_holder.lastName LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "current_holder.position LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "orgRoot.orgRootName LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "orgChild1.orgChild1Name LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "orgChild2.orgChild2Name LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "orgChild3.orgChild3Name LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "orgChild4.orgChild4Name LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) }), ) .take(body.pageSize) .getMany(); if (!findPosMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. PosMaster"); } const total = findPosMaster?findPosMaster.length:0; const formattedData = findPosMaster.map((item) => { let orgShortName = ""; if (item.orgChild1Id === null) { orgShortName = item.orgRoot?.orgRootShortName; } else if (item.orgChild2Id === null) { orgShortName = item.orgChild1?.orgChild1ShortName; } else if (item.orgChild3Id === null) { orgShortName = item.orgChild2?.orgChild2ShortName; } else if (item.orgChild4Id === null) { orgShortName = item.orgChild3?.orgChild3ShortName; } else { orgShortName = item.orgChild4?.orgChild4ShortName; } const posExecutive = item.positions == null || item.positions?.find((position) => position.positionIsSelected == true) == null || item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive == null || item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive ?.posExecutiveName == null ? null : item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive .posExecutiveName; const amount = item.current_holder == null || item.current_holder.profileSalary.length == 0 ? null : item.current_holder.profileSalary.sort((a: any, b: any) => b.date - a.date)[0].amount; return { prefix: item.current_holder.prefix, firstName: item.current_holder.firstName, lastName: item.current_holder.lastName, citizenId: item.current_holder.citizenId, posMasterNoPrefix: item.posMasterNoPrefix, posMasterNo: item.posMasterNo, posMasterNoSuffix: item.posMasterNoSuffix, orgShortName: orgShortName, position: item.current_holder.position, posType: item.current_holder.posType.posTypeName, posLevel: item.current_holder.posLevel.posLevelName, posExecutive: posExecutive, amount: amount?amount:0, // revisionId: item.orgRevisionId, rootId: item.orgRootId, root: item.orgRoot?.orgRootName?item.orgRoot.orgRootName:null, child1Id: item.orgChild1Id, child1: item.orgChild1?.orgChild1Name?item.orgChild1.orgChild1Name:null, child2Id: item.orgChild2Id, child2: item.orgChild2?.orgChild2Name?item.orgChild2.orgChild2Name:null, child3Id: item.orgChild3Id, child3: item.orgChild3?.orgChild3Name?item.orgChild3.orgChild3Name:null, child4Id: item.orgChild4Id, child4: item.orgChild4?.orgChild4Name?item.orgChild4.orgChild4Name:null, isResult: true, isDuration: false, isPunish: true, isRetired: false, isRetired2: true, }; }); return new HttpSuccess({data:formattedData,total:total}); } /** * API ข้อมูลทะเบียนประวัติตาม keycloak by revisionId * * @summary ข้อมูลทะเบียนประวัติตาม keycloak by revisionId (ADMIN) * */ @Get("keycloak/position/{revisionId}") async getProfileByKeycloakByRevision( @Path() revisionId: string, @Request() request: { user: Record }, ) { const profile = await this.profileRepository.findOne({ where: { keycloak: request.user.sub }, relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"], }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); } const _profile = { profileId: profile.id, prefix: profile.prefix, firstName: profile.firstName, lastName: profile.lastName, citizenId: profile.citizenId, position: profile.position, posLevelName: profile.posLevel == null ? null : profile.posLevel.posLevelName, posLevelRank: profile.posLevel == null ? null : profile.posLevel.posLevelRank, posLevelId: profile.posLevel == null ? null : profile.posLevel.id, posTypeName: profile.posType == null ? null : profile.posType.posTypeName, posTypeRank: profile.posType == null ? null : profile.posType.posTypeRank, posTypeId: profile.posType == null ? null : profile.posType.id, rootId: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRoot == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRootId, root: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRoot == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRoot.orgRootName, child1Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1Id, child1: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1 .orgChild1Name, child2Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2Id, child2: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2 .orgChild2Name, child3Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3Id, child3: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3 .orgChild3Name, child4Id: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4Id, child4: profile.current_holders == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null || profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4 == null ? null : profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4 .orgChild4Name, }; try { return new HttpSuccess(_profile); } catch (error: any) { throw new Error(error); } } }