diff --git a/src/controllers/OrganizationUnauthorizeController.ts b/src/controllers/OrganizationUnauthorizeController.ts index ba39c597..1c43c7d5 100644 --- a/src/controllers/OrganizationUnauthorizeController.ts +++ b/src/controllers/OrganizationUnauthorizeController.ts @@ -1,21 +1,4 @@ -import { - Controller, - Get, - Post, - Put, - Delete, - Patch, - Route, - Security, - Tags, - Body, - Path, - Request, - Example, - SuccessResponse, - Response, - Query, -} from "tsoa"; +import { Controller, Get, Post, Route, Tags, Body, Path, SuccessResponse, Response } from "tsoa"; import { OrgRevision } from "../entities/OrgRevision"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -528,8 +511,9 @@ export class OrganizationUnauthorizeController extends Controller { } const findProfile = await AppDataSource.getRepository(Profile) - .createQueryBuilder("current_holder") - .leftJoinAndSelect("current_holder.positions", "positions") + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.positions", "positions") .leftJoinAndSelect("positions.posExecutive", "posExecutive") .where({ id: id }) .getOne(); @@ -561,7 +545,18 @@ export class OrganizationUnauthorizeController extends Controller { .find((x) => x.orgRevisionId == findRevision.id) ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive .posExecutiveName; + const root = + findProfile.current_holders == null || + findProfile.current_holders.length == 0 || + findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; return new HttpSuccess({ + rootId: root == null ? null : root.id, + root: root == null ? null : root.orgRootName, + orgRootShortName: root == null ? null : root.orgRootShortName, + orgRevisionId: findRevision.id, + profileId: findProfile.id, type: "OFFICER", rank: findProfile.rank, prefix: findProfile.prefix, @@ -583,9 +578,17 @@ export class OrganizationUnauthorizeController extends Controller { */ @Get("employee/{id}") async GetProfileEmployeeById(@Path() id: string) { + const findRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); + } + const findProfile = await AppDataSource.getRepository(ProfileEmployee) - .createQueryBuilder("current_holder") - .leftJoinAndSelect("current_holder.positions", "positions") + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.positions", "positions") .leftJoinAndSelect("positions.posExecutive", "posExecutive") .where({ id: id }) .getOne(); @@ -593,7 +596,19 @@ export class OrganizationUnauthorizeController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found Profile"); } + const root = + findProfile.current_holders == null || + findProfile.current_holders.length == 0 || + findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + return new HttpSuccess({ + rootId: root == null ? null : root.id, + root: root == null ? null : root.orgRootName, + orgRootShortName: root == null ? null : root.orgRootShortName, + orgRevisionId: findRevision.id, + profileId: findProfile.id, type: "OFFICER", rank: findProfile.rank, prefix: findProfile.prefix, @@ -605,4 +620,132 @@ export class OrganizationUnauthorizeController extends Controller { posTypeId: findProfile.posTypeId, }); } + + /** + * API หา user profile officer + * + * @summary หา user profile officer + * + */ + @Get("officer/citizen/{id}") + async GetProfileByCitizenId(@Path() id: string) { + const findRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); + } + + const findProfile = await AppDataSource.getRepository(Profile) + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.positions", "positions") + .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .where({ citizenId: id }) + .getOne(); + if (!findProfile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found Profile"); + } + + const posExecutive = + findProfile.current_holders == null || + findProfile.current_holders.length == 0 || + findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null || + findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions == + null || + findProfile.current_holders?.find((x) => x.orgRevisionId == findRevision.id)?.positions + .length == 0 || + findProfile.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true) == null || + findProfile.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive == + null || + findProfile.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + ?.posExecutiveName == null + ? null + : findProfile.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + .posExecutiveName; + const root = + findProfile.current_holders == null || + findProfile.current_holders.length == 0 || + findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + return new HttpSuccess({ + rootId: root == null ? null : root.id, + root: root == null ? null : root.orgRootName, + orgRootShortName: root == null ? null : root.orgRootShortName, + orgRevisionId: findRevision.id, + profileId: findProfile.id, + type: "OFFICER", + rank: findProfile.rank, + prefix: findProfile.prefix, + firstName: findProfile.firstName, + lastName: findProfile.lastName, + citizenId: findProfile.citizenId, + position: findProfile.position, + posExecutive: posExecutive, + posLevelId: findProfile.posLevelId, + posTypeId: findProfile.posTypeId, + }); + } + + /** + * API หา user profile employee + * + * @summary หา user profile employee + * + */ + @Get("employee/citizen/{id}") + async GetProfileEmployeeByCitizenId(@Path() id: string) { + const findRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); + } + + const findProfile = await AppDataSource.getRepository(ProfileEmployee) + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.positions", "positions") + .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .where({ citizenId: id }) + .getOne(); + if (!findProfile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found Profile"); + } + + const root = + findProfile.current_holders == null || + findProfile.current_holders.length == 0 || + findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : findProfile.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + + return new HttpSuccess({ + rootId: root == null ? null : root.id, + root: root == null ? null : root.orgRootName, + orgRootShortName: root == null ? null : root.orgRootShortName, + orgRevisionId: findRevision.id, + profileId: findProfile.id, + type: "EMPLOYEE", + rank: findProfile.rank, + prefix: findProfile.prefix, + firstName: findProfile.firstName, + lastName: findProfile.lastName, + citizenId: findProfile.citizenId, + position: findProfile.position, + posLevelId: findProfile.posLevelId, + posTypeId: findProfile.posTypeId, + }); + } } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 55d079de..791a6139 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -43,6 +43,7 @@ export class ProfileController extends Controller { private profileHistoryRepo = AppDataSource.getRepository(ProfileHistory); private posLevelRepo = AppDataSource.getRepository(PosLevel); private posTypeRepo = AppDataSource.getRepository(PosType); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * @@ -379,33 +380,62 @@ export class ProfileController extends Controller { .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); + const findRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } const data = await Promise.all( record.map((_data) => { const posExecutive = _data.current_holders.length == 0 || - _data.current_holders[0].positions.length == 0 || - _data.current_holders[0].positions.find((x: any) => x.positionIsSelected == true) == - null || - _data.current_holders[0].positions.find((x: any) => x.positionIsSelected == true) - ?.posExecutive == null + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null || + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions.length == + 0 || + _data.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions.find((x: any) => x.positionIsSelected == true) == null || + _data.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive == null ? null - : _data.current_holders[0].positions.find((x: any) => x.positionIsSelected == true) - ?.posExecutive?.posExecutiveName; + : _data.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive + ?.posExecutiveName; const shortName = _data.current_holders.length == 0 ? null - : _data.current_holders[0].orgChild4 != null - ? `${_data.current_holders[0].orgChild4.orgChild4ShortName}${_data.current_holders[0].posMasterNo}` - : _data.current_holders[0].orgChild3 != null - ? `${_data.current_holders[0].orgChild3.orgChild3ShortName}${_data.current_holders[0].posMasterNo}` - : _data.current_holders[0].orgChild2 != null - ? `${_data.current_holders[0].orgChild2.orgChild2ShortName}${_data.current_holders[0].posMasterNo}` - : _data.current_holders[0].orgChild1 != null - ? `${_data.current_holders[0].orgChild1.orgChild1ShortName}${_data.current_holders[0].posMasterNo}` - : _data.current_holders[0].orgRoot != null - ? `${_data.current_holders[0].orgRoot.orgRootShortName}${_data.current_holders[0].posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild3 != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild2 != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` : null; + const root = + _data.current_holders.length == 0 || + (_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) + ? null + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; return { id: _data.id, prefix: _data.prefix, @@ -420,6 +450,10 @@ export class ProfileController extends Controller { position: _data.position, posExecutive: posExecutive, posNo: shortName, + rootId: root == null ? null : root.id, + root: root == null ? null : root.orgRootName, + orgRootShortName: root == null ? null : root.orgRootShortName, + orgRevisionId: root == null ? null : root.orgRevisionId, }; }), ); @@ -955,6 +989,7 @@ export class ProfileController extends Controller { const [findProfile, total] = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") .leftJoinAndSelect("profile.current_holders", "current_holders") .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") @@ -962,11 +997,14 @@ export class ProfileController extends Controller { .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .leftJoinAndSelect("current_holders.positions", "positions") + .leftJoinAndSelect("positions.posExecutive", "posExecutive") .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}%` }) + .orWhere("posType.posTypeName LIKE :keyword", { keyword: `%${body.keyword}%` }) .orderBy("profile.citizenId", "ASC") .skip((body.page - 1) * body.pageSize) .take(body.pageSize) @@ -975,9 +1013,75 @@ export class ProfileController extends Controller { const orgRevisionActive = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); + const findRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } const mapDataProfile = await Promise.all( findProfile.map(async (item: Profile) => { + const posMaster = + item.current_holders == null || + item.current_holders.length == 0 || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id); + const position = + posMaster == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions == null || + item.current_holders?.find((x) => x.orgRevisionId == findRevision.id)?.positions.length == + 0 || + item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true) == null + ? null + : item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true); + const posExecutive = + position == null || + item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive == + null || + item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + ?.posExecutiveName == null + ? null + : item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + .posExecutiveName; + + const shortName = + item.current_holders.length == 0 + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild2 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; + return { id: item.id, prefix: item.prefix, @@ -987,6 +1091,12 @@ export class ProfileController extends Controller { position: item.position, idcard: item.citizenId, posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, + posTypeName: item.posType == null ? null : item.posType.posTypeName, + posNo: `${posMaster == null ? null : posMaster.posMasterNo}${shortName}`, + positionField: position == null ? null : position.positionField, + positionArea: position == null ? null : position.positionArea, + posExecutiveName: posExecutive, + positionExecutiveField: position == null ? null : position.positionExecutiveField, isProbation: item.isProbation, orgRootName: item.current_holders == null ||