diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts index f2acbaba..3ecdabc9 100644 --- a/src/controllers/ProfileGovernmentController.ts +++ b/src/controllers/ProfileGovernmentController.ts @@ -148,6 +148,7 @@ export class ProfileGovernmentHistoryController extends Controller { let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER"); if (_workflow == false) await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId); + const orgRevision = await this.orgRevisionRepository.findOne({ select: ["id"], where: { @@ -155,10 +156,22 @@ export class ProfileGovernmentHistoryController extends Controller { orgRevisionIsCurrent: true, }, }); + + // ค้นหา profile ก่อน const record = await this.profileRepo.findOne({ - where: { + where: { id: profileId }, + relations: ["posType", "posLevel"], + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile"); + } + + // ค้นหา profileSalary แยกต่างหาก + const profileWithSalary = await this.profileRepo.findOne({ + where: { id: profileId, - profileSalary: { + profileSalary: { commandCode: In([ "0", "9", @@ -175,16 +188,19 @@ export class ProfileGovernmentHistoryController extends Controller { "15", "16", ]), - } + }, }, - relations: ["posType", "posLevel", "profileSalary"], + relations: ["profileSalary"], order: { profileSalary: { order: "DESC", - createdAt: "DESC" - } - } + createdAt: "DESC", + }, + }, }); + + // ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ + record.profileSalary = profileWithSalary?.profileSalary || []; const posMaster = await this.posMasterRepo.findOne({ where: { orgRevisionId: orgRevision?.id, @@ -236,8 +252,8 @@ export class ProfileGovernmentHistoryController extends Controller { orgShortName = posMaster.orgChild4?.orgChild4ShortName ?? ""; } } - let _OrgLeave:any = [] - let _profileSalary:any = null; + let _OrgLeave: any = []; + let _profileSalary: any = null; if (record?.isLeave && record?.profileSalary.length > 0) { // _OrgLeave = [ // record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null, @@ -247,15 +263,14 @@ export class ProfileGovernmentHistoryController extends Controller { // record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null, // ]; if (record.leaveType == "RETIRE") { - _profileSalary = record?.profileSalary.length > 1 - ? record?.profileSalary[1] - : record?.profileSalary.length > 0 + _profileSalary = + record?.profileSalary.length > 1 + ? record?.profileSalary[1] + : record?.profileSalary.length > 0 ? record?.profileSalary[0] : null; } else { - _profileSalary = record?.profileSalary.length > 0 - ? record?.profileSalary[0] - : null; + _profileSalary = record?.profileSalary.length > 0 ? record?.profileSalary[0] : null; } if (_profileSalary) { _OrgLeave = [ @@ -269,17 +284,20 @@ export class ProfileGovernmentHistoryController extends Controller { _OrgLeave = []; } } - const orgLeave = _OrgLeave.filter((x:any) => x !== undefined && x !== null).join("\n"); + const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n"); const data = { org: record?.isLeave == false ? org : orgLeave, //สังกัด positionField: position == null ? null : position.positionField, //สายงาน position: record?.position, //ตำแหน่ง posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ - posMasterNo: record?.isLeave == false - ? posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}` - : _profileSalary != null - ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` - : null, //เลขที่ตำแหน่ง + posMasterNo: + record?.isLeave == false + ? posMaster == null + ? null + : `${orgShortName} ${posMaster.posMasterNo}` + : _profileSalary != null + ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` + : null, //เลขที่ตำแหน่ง posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท posExecutive: position == null || position.posExecutive == null @@ -310,8 +328,20 @@ export class ProfileGovernmentHistoryController extends Controller { orgRevisionIsCurrent: true, }, }); + + // ค้นหา profile ก่อน const record = await this.profileRepo.findOne({ - where: { + where: { id: profileId }, + relations: ["posType", "posLevel"], + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile"); + } + + // ค้นหา profileSalary แยกต่างหาก + const profileWithSalary = await this.profileRepo.findOne({ + where: { id: profileId, profileSalary: { commandCode: In([ @@ -330,20 +360,19 @@ export class ProfileGovernmentHistoryController extends Controller { "15", "16", ]), - } - }, - relations: { - posType: true, - posLevel: true, - profileSalary: true + }, }, + relations: ["profileSalary"], order: { profileSalary: { order: "DESC", - createdAt: "DESC" - } - } + createdAt: "DESC", + }, + }, }); + + // ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ + record.profileSalary = profileWithSalary?.profileSalary || []; const posMaster = await this.posMasterRepo.findOne({ where: { orgRevisionId: orgRevision?.id, @@ -395,8 +424,8 @@ export class ProfileGovernmentHistoryController extends Controller { orgShortName = posMaster.orgChild4?.orgChild4ShortName; } } - let _OrgLeave:any = [] - let _profileSalary:any = null; + let _OrgLeave: any = []; + let _profileSalary: any = null; if (record?.isLeave && record?.profileSalary.length > 0) { // _OrgLeave = [ // record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null, @@ -406,15 +435,14 @@ export class ProfileGovernmentHistoryController extends Controller { // record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null, // ]; if (record.leaveType == "RETIRE") { - _profileSalary = record?.profileSalary.length > 1 - ? record?.profileSalary[1] - : record?.profileSalary.length > 0 + _profileSalary = + record?.profileSalary.length > 1 + ? record?.profileSalary[1] + : record?.profileSalary.length > 0 ? record?.profileSalary[0] : null; } else { - _profileSalary = record?.profileSalary.length > 0 - ? record?.profileSalary[0] - : null; + _profileSalary = record?.profileSalary.length > 0 ? record?.profileSalary[0] : null; } if (_profileSalary) { _OrgLeave = [ @@ -428,19 +456,19 @@ export class ProfileGovernmentHistoryController extends Controller { _OrgLeave = []; } } - const orgLeave = _OrgLeave.filter((x:any) => x !== undefined && x !== null).join("\n"); + const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n"); const data = { org: record?.isLeave == false ? org : orgLeave, //สังกัด positionField: position == null ? null : position.positionField, //สายงาน position: record?.position, //ตำแหน่ง posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ - posMasterNo: - record?.isLeave == false - ? posMaster == null - ? null + posMasterNo: + record?.isLeave == false + ? posMaster == null + ? null : `${orgShortName} ${posMaster.posMasterNo}` - : _profileSalary != null - ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` + : _profileSalary != null + ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` : null, //เลขที่ตำแหน่ง posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท posExecutive: @@ -458,7 +486,7 @@ export class ProfileGovernmentHistoryController extends Controller { govAgeAbsent: record?.govAgeAbsent, govAgePlus: record?.govAgePlus, reasonSameDate: record?.reasonSameDate, - isLeave: record?.isLeave + isLeave: record?.isLeave, }; return new HttpSuccess(data);