diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts index c0c2ea8b..f5019685 100644 --- a/src/controllers/ProfileGovernmentController.ts +++ b/src/controllers/ProfileGovernmentController.ts @@ -23,6 +23,8 @@ import { ProfileGovernment, UpdateProfileGovernment, } from "../entities/ProfileGovernment"; +import { Position } from "../entities/Position"; +import { PosMaster } from "../entities/PosMaster"; @Route("api/v1/org/profile/government") @Tags("ProfileGovernment") @@ -30,16 +32,93 @@ import { export class ProfileGovernmentHistoryController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private govRepo = AppDataSource.getRepository(ProfileGovernment); + private positionRepo = AppDataSource.getRepository(Position); + private posMasterRepo = AppDataSource.getRepository(PosMaster); @Get("{profileId}") @Example({}) public async getGovHistory(@Path() profileId: string) { - const record = await this.govRepo.find({ - take: 1, + const record = await this.profileRepo.findOne({ + where: { id: profileId }, order: { createdAt: "DESC" }, - where: { profileId }, + relations: { + posType: true, + posLevel: true, + }, }); - return new HttpSuccess({ ...record[0] }); + const posMaster = await this.posMasterRepo.findOne({ + where: { + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + current_holderId: profileId, + }, + order: { createdAt: "DESC" }, + relations: { + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }); + const position = await this.positionRepo.findOne({ + where: { + positionIsSelected: true, + posMaster: { + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + current_holderId: profileId, + }, + }, + order: { createdAt: "DESC" }, + relations: { + posExecutive: true, + }, + }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const fullNameParts = [ + posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name, + posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name, + posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name, + posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name, + posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName, + ]; + const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("/"); + let orgShortName = ""; + if (posMaster != null) { + if (posMaster.orgChild1Id === null) { + orgShortName = posMaster.orgRoot?.orgRootShortName; + } else if (posMaster.orgChild2Id === null) { + orgShortName = posMaster.orgChild1?.orgChild1ShortName; + } else if (posMaster.orgChild3Id === null) { + orgShortName = posMaster.orgChild2?.orgChild2ShortName; + } else if (posMaster.orgChild4Id === null) { + orgShortName = posMaster.orgChild3?.orgChild3ShortName; + } else { + orgShortName = posMaster.orgChild4?.orgChild4ShortName; + } + } + const data = { + org: org, //สังกัด + positionField: position == null ? null : position.positionField, //สายงาน + position: record.position, //ตำแหน่ง + posLevel: record.posLevel.posLevelName, //ระดับ + posMasterNo: posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}`, //เลขที่ตำแหน่ง + posType: record.posType.posTypeName, //ประเภท + posExecutive: + position == null || position.posExecutive == null + ? null + : position.posExecutive.posExecutiveName, //ตำแหน่งทางการบริหาร + positionArea: position == null ? null : position.positionArea, //ด้าน/สาขา + positionExecutiveField: position == null ? null : position.positionExecutiveField, //ด้านทางการบริหาร + }; + + return new HttpSuccess(data); } @Get("history/{profileId}")