diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index b4edbbcc..ba3431bb 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -3187,7 +3187,7 @@ export class ProfileController extends Controller { * */ @Get("keycloak/position") - async getProfileByKeycloak(@Request() request: { user: Record }) { + async getProfileByKeycloak(@Request() request: { user: Record } , @Query("revisionId") revisionId?: string,) { const profile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub }, relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot", "profileSalary"], @@ -3209,6 +3209,18 @@ export class ProfileController extends Controller { if (!orgRevisionPublish) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); } + + if (revisionId) { + const orgRevisionPublish = await this.orgRevisionRepo + .createQueryBuilder("orgRevision") + .where("orgRevision.id = :revisionId", { revisionId }) + // .andWhere("orgRevision.orgRevisionIsDraft = false") + // .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); + if (!orgRevisionPublish) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + } + } const posMaster = profile.current_holders == null || diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 5d0c29eb..c4f20bc0 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -133,20 +133,20 @@ export class ProfileEmployeeController extends Controller { where: { id: id }, order: { profileSalarys: { - date: "DESC" - } - } + date: "DESC", + }, + }, }); if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", profile.id); - let ImgUrl: any - if(profile?.avatar != null && profile?.avatarName != null) { + let ImgUrl: any; + if (profile?.avatar != null && profile?.avatarName != null) { await new CallAPI() - .GetData2(req, `/salary/file/${profile?.avatar}/${profile?.avatarName}`) - .then(async (x) => { - ImgUrl = x.downloadUrl - }) - .catch(); + .GetData2(req, `/salary/file/${profile?.avatar}/${profile?.avatarName}`) + .then(async (x) => { + ImgUrl = x.downloadUrl; + }) + .catch(); } const province = await this.provinceRepository.findOneBy({ id: profile.registrationProvinceId, @@ -241,21 +241,25 @@ export class ProfileEmployeeController extends Controller { ? Extension.ToThaiNumber(profile.profileSalarys[0].amount.toLocaleString()) : "-", Education: Education, - // profile.profileEducations.length > 0 && - // profile.profileEducations[profile.profileEducations.length - 1].institute != null - // ? profile.profileEducations[profile.profileEducations.length - 1].institute - // : "", + // profile.profileEducations.length > 0 && + // profile.profileEducations[profile.profileEducations.length - 1].institute != null + // ? profile.profileEducations[profile.profileEducations.length - 1].institute + // : "", AppointText: profile.dateAppoint != null ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.dateAppoint)) : "-", SalaryDate: profile.profileSalarys.length > 0 && profile.profileSalarys[0].date != null - ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile.profileSalarys[0].date)) + ? Extension.ToThaiNumber( + Extension.ToThaiShortDate_monthYear(profile.profileSalarys[0].date), + ) : "-", PositionName: profile.position != null ? profile.position : "-", OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`, - ImgUrl: ImgUrl ? ImgUrl : `https://bma-ehr.frappet.synology.me/assets/avatar_user-89f22423.jpg` + ImgUrl: ImgUrl + ? ImgUrl + : `https://bma-ehr.frappet.synology.me/assets/avatar_user-89f22423.jpg`, }; return new HttpSuccess(mapData); @@ -290,14 +294,14 @@ export class ProfileEmployeeController extends Controller { await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", profiles.id); } if (!profiles) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - let ImgUrl: any - if(profiles?.avatar != null && profiles?.avatarName != null) { + let ImgUrl: any; + if (profiles?.avatar != null && profiles?.avatarName != null) { await new CallAPI() - .GetData2(req, `/salary/file/${profiles?.avatar}/${profiles?.avatarName}`) - .then(async (x) => { - ImgUrl = x.downloadUrl - }) - .catch(); + .GetData2(req, `/salary/file/${profiles?.avatar}/${profiles?.avatarName}`) + .then(async (x) => { + ImgUrl = x.downloadUrl; + }) + .catch(); } const profileOc = await this.profileRepo.findOne({ relations: [ @@ -459,107 +463,124 @@ export class ProfileEmployeeController extends Controller { ? Extension.ToThaiNumber(profiles.currentProvince.name) : "", // AvatarId: profiles?.avatar ?? null, - ImgUrl: ImgUrl ? ImgUrl : `https://bma-ehr.frappet.synology.me/assets/avatar_user-89f22423.jpg` + ImgUrl: ImgUrl + ? ImgUrl + : `https://bma-ehr.frappet.synology.me/assets/avatar_user-89f22423.jpg`, }; const certs = await this.certificateRepository.find({ where: { profileEmployeeId: id }, select: ["certificateType", "issuer", "certificateNo", "issueDate"], }); - const Cert = certs.length > 0 - ? certs.map((item) => ({ - CertificateType: item.certificateType ?? null, - Issuer: item.issuer ?? null, - CertificateNo: Extension.ToThaiNumber(item.certificateNo) ?? null, - IssueDate: Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.issueDate)) ?? null, - })) - :[{ - CertificateType: "-", - Issuer: "-", - CertificateNo: "-", - IssueDate: "-" - }]; + const Cert = + certs.length > 0 + ? certs.map((item) => ({ + CertificateType: item.certificateType ?? null, + Issuer: item.issuer ?? null, + CertificateNo: Extension.ToThaiNumber(item.certificateNo) ?? null, + IssueDate: Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.issueDate)) ?? null, + })) + : [ + { + CertificateType: "-", + Issuer: "-", + CertificateNo: "-", + IssueDate: "-", + }, + ]; const trainings = await this.trainingRepository.find({ select: ["startDate", "endDate", "place", "department"], where: { profileEmployeeId: id }, }); - const Training = trainings.length > 0 - ? trainings.map((item) => ({ - Institute: item.department ?? "", - Start: - item.startDate == null - ? "" - : Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.startDate)), - End: - item.endDate == null ? "" : Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.endDate)), - Date: - item.startDate && item.endDate - ? `${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.endDate))}` - : "", - Level: "", - Degree: item.name, - Field: "", - })) - : [{ - Institute: "-", - Start: "-", - End: "-", - Date: "-", - Level: "-", - Degree: "-", - Field: "-", - }]; + const Training = + trainings.length > 0 + ? trainings.map((item) => ({ + Institute: item.department ?? "", + Start: + item.startDate == null + ? "" + : Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.startDate)), + End: + item.endDate == null + ? "" + : Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.endDate)), + Date: + item.startDate && item.endDate + ? `${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.endDate))}` + : "", + Level: "", + Degree: item.name, + Field: "", + })) + : [ + { + Institute: "-", + Start: "-", + End: "-", + Date: "-", + Level: "-", + Degree: "-", + Field: "-", + }, + ]; const disciplines = await this.disciplineRepository.find({ select: ["refCommandDate", "refCommandNo", "detail"], where: { profileEmployeeId: id }, }); - const Discipline = disciplines.length > 0 - ? disciplines.map((item) => ({ - DisciplineYear: - Extension.ToThaiNumber(new Date(item.refCommandDate).getFullYear().toString()) ?? null, - DisciplineDetail: item.detail ?? null, - RefNo: Extension.ToThaiNumber(item.refCommandNo) ?? null, - })) - : [{ - DisciplineYear: "-", - DisciplineDetail: "-", - RefNo: "-", - }]; + const Discipline = + disciplines.length > 0 + ? disciplines.map((item) => ({ + DisciplineYear: + Extension.ToThaiNumber(new Date(item.refCommandDate).getFullYear().toString()) ?? + null, + DisciplineDetail: item.detail ?? null, + RefNo: Extension.ToThaiNumber(item.refCommandNo) ?? null, + })) + : [ + { + DisciplineYear: "-", + DisciplineDetail: "-", + RefNo: "-", + }, + ]; const educations = await this.educationRepository.find({ select: ["startDate", "endDate", "educationLevel", "degree", "field", "institute"], where: { profileEmployeeId: id }, order: { lastUpdatedAt: "DESC" }, }); - const Education = educations.length > 0 - ? educations.map((item) => ({ - Institute: item.institute, - Start: - item.startDate == null - ? "" - : Extension.ToThaiNumber(new Date(item.startDate).getFullYear().toString()), - End: - item.endDate == null - ? "" - : Extension.ToThaiNumber(new Date(item.endDate).getFullYear().toString()), - Date: - item.startDate && item.endDate - ? `${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.endDate))}` - : "", - Level: item.educationLevel ?? "", - Degree: item.degree ? `${item.degree} ${item.field ? item.field : ""}` : "", - Field: item.field ?? "-", - })) - : [{ - Institute: "-", - Start: "-", - End: "-", - Date: "-", - Level: "-", - Degree: "-", - Field: "-", - }]; + const Education = + educations.length > 0 + ? educations.map((item) => ({ + Institute: item.institute, + Start: + item.startDate == null + ? "" + : Extension.ToThaiNumber(new Date(item.startDate).getFullYear().toString()), + End: + item.endDate == null + ? "" + : Extension.ToThaiNumber(new Date(item.endDate).getFullYear().toString()), + Date: + item.startDate && item.endDate + ? `${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.endDate))}` + : "", + Level: item.educationLevel ?? "", + Degree: item.degree ? `${item.degree} ${item.field ? item.field : ""}` : "", + Field: item.field ?? "-", + })) + : [ + { + Institute: "-", + Start: "-", + End: "-", + Date: "-", + Level: "-", + Degree: "-", + Field: "-", + }, + ]; const salarys = await this.salaryRepository.find({ select: [ "date", @@ -573,39 +594,46 @@ export class ProfileEmployeeController extends Controller { "templateDoc", ], where: { profileEmployeeId: id }, - order: { order: "ASC" } + order: { order: "ASC" }, }); - const Salary = salarys.length > 0 - ? salarys.map((item) => ({ - SalaryDate: item.date ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.date)) : null, - Position: item.position != null ? Extension.ToThaiNumber(item.position) : null, - PosNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : null, - Salary: item.amount != null ? Extension.ToThaiNumber(item.amount.toLocaleString()) : null, - Rank: item.positionLevel != null ? Extension.ToThaiNumber(item.positionLevel) : null, - RefAll: item.templateDoc ? Extension.ToThaiNumber(item.templateDoc) : null, - PositionLevel: item.positionLevel != null ? Extension.ToThaiNumber(item.positionLevel) : null, - PositionType: item.positionType ?? null, - PositionAmount: - item.positionSalaryAmount == null - ? null - : Extension.ToThaiNumber(item.positionSalaryAmount.toLocaleString()), - FullName: `${profiles?.prefix}${profiles?.firstName} ${profiles?.lastName}`, - OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`, - })) - : [{ - SalaryDate: "-", - Position: "-", - PosNo: "-", - Salary: "-", - Rank: "-", - RefAll: "-", - PositionLevel: "-", - PositionType: "-", - PositionAmount: "-", - FullName: "-", - OcFullPath: "-", - }]; + const Salary = + salarys.length > 0 + ? salarys.map((item) => ({ + SalaryDate: item.date + ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.date)) + : null, + Position: item.position != null ? Extension.ToThaiNumber(item.position) : null, + PosNo: item.posNo != null ? Extension.ToThaiNumber(item.posNo) : null, + Salary: + item.amount != null ? Extension.ToThaiNumber(item.amount.toLocaleString()) : null, + Rank: item.positionLevel != null ? Extension.ToThaiNumber(item.positionLevel) : null, + RefAll: item.templateDoc ? Extension.ToThaiNumber(item.templateDoc) : null, + PositionLevel: + item.positionLevel != null ? Extension.ToThaiNumber(item.positionLevel) : null, + PositionType: item.positionType ?? null, + PositionAmount: + item.positionSalaryAmount == null + ? null + : Extension.ToThaiNumber(item.positionSalaryAmount.toLocaleString()), + FullName: `${profiles?.prefix}${profiles?.firstName} ${profiles?.lastName}`, + OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`, + })) + : [ + { + SalaryDate: "-", + Position: "-", + PosNo: "-", + Salary: "-", + Rank: "-", + RefAll: "-", + PositionLevel: "-", + PositionType: "-", + PositionAmount: "-", + FullName: "-", + OcFullPath: "-", + }, + ]; const insignias = await this.profileInsigniaRepo.find({ relations: { @@ -616,56 +644,62 @@ export class ProfileEmployeeController extends Controller { where: { profileEmployeeId: id }, order: { receiveDate: "ASC" }, }); - const Insignia = insignias.length > 0 - ? insignias.map((item) => ({ - ReceiveDate: item.receiveDate - ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.receiveDate)) - : "", - InsigniaName: item.insignia.name, - InsigniaShortName: item.insignia.shortName, - InsigniaTypeName: item.insignia.insigniaType.name, - No: item.no ? Extension.ToThaiNumber(item.no) : "", - Issue: item.issue ? item.issue : "", - VolumeNo: item.volumeNo ? Extension.ToThaiNumber(item.volumeNo) : "", - Volume: item.volume ? Extension.ToThaiNumber(item.volume) : "", - Section: item.section ? Extension.ToThaiNumber(item.section) : "", - Page: item.page ? Extension.ToThaiNumber(item.page) : "", - RefCommandDate: item.refCommandDate - ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.refCommandDate)) - : "", - })) - : [{ - ReceiveDate: "-", - InsigniaName: "-", - InsigniaShortName: "-", - InsigniaTypeName: "-", - No: "-", - Issue: "-", - VolumeNo: "-", - Volume: "-", - Section: "-", - Page: "-", - RefCommandDate: "-", - }]; + const Insignia = + insignias.length > 0 + ? insignias.map((item) => ({ + ReceiveDate: item.receiveDate + ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.receiveDate)) + : "", + InsigniaName: item.insignia.name, + InsigniaShortName: item.insignia.shortName, + InsigniaTypeName: item.insignia.insigniaType.name, + No: item.no ? Extension.ToThaiNumber(item.no) : "", + Issue: item.issue ? item.issue : "", + VolumeNo: item.volumeNo ? Extension.ToThaiNumber(item.volumeNo) : "", + Volume: item.volume ? Extension.ToThaiNumber(item.volume) : "", + Section: item.section ? Extension.ToThaiNumber(item.section) : "", + Page: item.page ? Extension.ToThaiNumber(item.page) : "", + RefCommandDate: item.refCommandDate + ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.refCommandDate)) + : "", + })) + : [ + { + ReceiveDate: "-", + InsigniaName: "-", + InsigniaShortName: "-", + InsigniaTypeName: "-", + No: "-", + Issue: "-", + VolumeNo: "-", + Volume: "-", + Section: "-", + Page: "-", + RefCommandDate: "-", + }, + ]; const leaves = await this.profileLeaveRepository.find({ relations: { leaveType: true }, where: { profileEmployeeId: id }, order: { dateLeaveStart: "ASC" }, }); - const Leave = leaves.length > 0 - ? leaves.map((item) => ({ - LeaveTypeName: item.leaveType.name, - DateLeaveStart: item.dateLeaveStart - ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.dateLeaveStart)) - : "", - LeaveDays: item.leaveDays ? Extension.ToThaiNumber(item.leaveDays.toString()) : "", - })) - : [{ - LeaveTypeName: "-", - DateLeaveStart: "-", - LeaveDays: "-", - }]; + const Leave = + leaves.length > 0 + ? leaves.map((item) => ({ + LeaveTypeName: item.leaveType.name, + DateLeaveStart: item.dateLeaveStart + ? Extension.ToThaiNumber(Extension.ToThaiFullDate2(item.dateLeaveStart)) + : "", + LeaveDays: item.leaveDays ? Extension.ToThaiNumber(item.leaveDays.toString()) : "", + })) + : [ + { + LeaveTypeName: "-", + DateLeaveStart: "-", + LeaveDays: "-", + }, + ]; return new HttpSuccess({ Profile: [Profile], @@ -1738,15 +1772,24 @@ export class ProfileEmployeeController extends Controller { * */ @Get("keycloak/position") - async getProfileByKeycloak(@Request() request: { user: Record }) { + async getProfileByKeycloak( + @Request() request: { user: Record }, + @Query("revisionId") revisionId?: string, + ) { const profile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub }, - relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot", "profileSalarys"], + relations: [ + "posLevel", + "posType", + "current_holders", + "current_holders.orgRoot", + "profileSalarys", + ], order: { profileSalarys: { order: "DESC", - } - } + }, + }, }); if (!profile) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); @@ -1761,6 +1804,18 @@ export class ProfileEmployeeController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); } + if (revisionId) { + const orgRevisionPublish = await this.orgRevisionRepo + .createQueryBuilder("orgRevision") + .where("orgRevision.id = :revisionId", { revisionId }) + // .andWhere("orgRevision.orgRevisionIsDraft = false") + // .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); + if (!orgRevisionPublish) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + } + } + const _profile = { profileId: profile.id, rank: profile.rank, @@ -1845,10 +1900,9 @@ export class ProfileEmployeeController extends Controller { ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4 .orgChild4Name, - salary: profile && profile.profileSalarys.length > 0 - ? profile.profileSalarys[0].amount - : null -}; + salary: + profile && profile.profileSalarys.length > 0 ? profile.profileSalarys[0].amount : null, + }; return new HttpSuccess(_profile); } diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index 90ff2976..e4f943ea 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1535,15 +1535,24 @@ export class ProfileEmployeeTempController extends Controller { * */ @Get("keycloak/position") - async getProfileByKeycloak(@Request() request: { user: Record }) { + async getProfileByKeycloak( + @Request() request: { user: Record }, + @Query("revisionId") revisionId?: string, + ) { const profile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub }, - relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot","profileSalarys"], + relations: [ + "posLevel", + "posType", + "current_holders", + "current_holders.orgRoot", + "profileSalarys", + ], order: { profileSalarys: { order: "DESC", - } - } + }, + }, }); if (!profile) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); @@ -1558,6 +1567,18 @@ export class ProfileEmployeeTempController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); } + if (revisionId) { + const orgRevisionPublish = await this.orgRevisionRepo + .createQueryBuilder("orgRevision") + .where("orgRevision.id = :revisionId", { revisionId }) + // .andWhere("orgRevision.orgRevisionIsDraft = false") + // .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); + if (!orgRevisionPublish) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + } + } + const _profile = { profileId: profile.id, rank: profile.rank, @@ -1642,9 +1663,8 @@ export class ProfileEmployeeTempController extends Controller { ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4 .orgChild4Name, - salary: profile && profile.profileSalarys.length > 0 - ? profile.profileSalarys[0].amount - : null + salary: + profile && profile.profileSalarys.length > 0 ? profile.profileSalarys[0].amount : null, }; return new HttpSuccess(_profile); }