From e790b50f58a08320c58780d3c954d7e56fca44ea Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 17 Jun 2025 18:07:41 +0700 Subject: [PATCH] fix bug report #1368, #1578 --- .../LeaveRequests/LeaveRequestRepository.cs | 63 +++++++--- .../Repositories/UserProfileRepository.cs | 65 +++++++++++ .../Controllers/LeaveReportController.cs | 108 ++++++++++++++++-- 3 files changed, 215 insertions(+), 21 deletions(-) diff --git a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs index 25595c5f..6845fb61 100644 --- a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs @@ -1465,24 +1465,59 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests { // var _nodeId = Guid.Parse(nodeId); var data = new List(); + //if (role == "OWNER" || role == "CHILD") + //{ + // data = await _dbContext.Set().AsQueryable() + // .Include(x => x.Type) + // .Where(x => x.ProfileType == type.Trim().ToUpper()) + // .Where(x => x.LeaveStartDate.Date >= startDate.Date && x.LeaveStartDate.Date <= endDate.Date) + // .Where(x => node == 4 ? x.Child4Id == Guid.Parse(nodeId) : (node == 3 ? x.Child3Id == Guid.Parse(nodeId) : (node == 2 ? x.Child2Id == Guid.Parse(nodeId) : (node == 1 ? x.Child1Id == Guid.Parse(nodeId) : (node == 0 ? x.RootId == Guid.Parse(nodeId) : (node == null ? true : true)))))) + // .Where(x => x.LeaveStatus == "APPROVE" || x.LeaveStatus == "DELETING").ToListAsync(); + //} + //else + //{ + // data = await _dbContext.Set().AsQueryable() + // .Include(x => x.Type) + // .Where(x => x.ProfileType == type.Trim().ToUpper()) + // .Where(x => x.LeaveStartDate.Date >= startDate.Date && x.LeaveStartDate.Date <= endDate.Date) + // .Where(x => node == 4 ? x.Child4Id == Guid.Parse(nodeId) : (node == 3 ? x.Child3Id == Guid.Parse(nodeId) : (node == 2 ? x.Child2Id == Guid.Parse(nodeId) : (node == 1 ? x.Child1Id == Guid.Parse(nodeId) : (node == 0 ? x.RootId == Guid.Parse(nodeId) : (node == null ? true : true)))))) + // .Where(x => node == 0 ? x.Child1Id == null : (node == 1 ? x.Child2Id == null : (node == 2 ? x.Child3Id == null : (node == 3 ? x.Child4Id == null : true)))) + // .Where(x => x.LeaveStatus == "APPROVE" || x.LeaveStatus == "DELETING").ToListAsync(); + //} + data = await _dbContext.Set().AsQueryable() + .Include(x => x.Type) + .Where(x => x.ProfileType == type.Trim().ToUpper()) + .Where(x => x.LeaveStartDate.Date >= startDate.Date && x.LeaveStartDate.Date <= endDate.Date) + .Where(x => x.LeaveStatus == "APPROVE" || x.LeaveStatus == "DELETING").ToListAsync(); + + if (role == "OWNER") + { + node = null; + } if (role == "OWNER" || role == "CHILD") { - data = await _dbContext.Set().AsQueryable() - .Include(x => x.Type) - .Where(x => x.ProfileType == type.Trim().ToUpper()) - .Where(x => x.LeaveStartDate.Date >= startDate.Date && x.LeaveStartDate.Date <= endDate.Date) - .Where(x => node == 4 ? x.Child4Id == Guid.Parse(nodeId) : (node == 3 ? x.Child3Id == Guid.Parse(nodeId) : (node == 2 ? x.Child2Id == Guid.Parse(nodeId) : (node == 1 ? x.Child1Id == Guid.Parse(nodeId) : (node == 0 ? x.RootId == Guid.Parse(nodeId) : (node == null ? true : true)))))) - .Where(x => x.LeaveStatus == "APPROVE" || x.LeaveStatus == "DELETING").ToListAsync(); + data = data.Where(x => + node == 4 ? x.Child4DnaId == Guid.Parse(nodeId) : + node == 3 ? x.Child3DnaId == Guid.Parse(nodeId) : + node == 2 ? x.Child2DnaId == Guid.Parse(nodeId) : + node == 1 ? x.Child1DnaId == Guid.Parse(nodeId) : + node == 0 ? x.RootDnaId == Guid.Parse(nodeId) : + node == null ? true : true + ).ToList(); } - else + else if (role == "ROOT") { - data = await _dbContext.Set().AsQueryable() - .Include(x => x.Type) - .Where(x => x.ProfileType == type.Trim().ToUpper()) - .Where(x => x.LeaveStartDate.Date >= startDate.Date && x.LeaveStartDate.Date <= endDate.Date) - .Where(x => node == 4 ? x.Child4Id == Guid.Parse(nodeId) : (node == 3 ? x.Child3Id == Guid.Parse(nodeId) : (node == 2 ? x.Child2Id == Guid.Parse(nodeId) : (node == 1 ? x.Child1Id == Guid.Parse(nodeId) : (node == 0 ? x.RootId == Guid.Parse(nodeId) : (node == null ? true : true)))))) - .Where(x => node == 0 ? x.Child1Id == null : (node == 1 ? x.Child2Id == null : (node == 2 ? x.Child3Id == null : (node == 3 ? x.Child4Id == null : true)))) - .Where(x => x.LeaveStatus == "APPROVE" || x.LeaveStatus == "DELETING").ToListAsync(); + data = data.Where(x => x.RootDnaId == Guid.Parse(nodeId)).ToList(); + } + else if (role == "NORMAL") + { + data = data.Where(x => + node == 0 ? x.Child1DnaId == null : + node == 1 ? x.Child2DnaId == null : + node == 2 ? x.Child3DnaId == null : + node == 3 ? x.Child4DnaId == null : + true + ).ToList(); } var res = (from d in data diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index 11fea71b..71bd30ad 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -523,6 +523,38 @@ namespace BMA.EHR.Application.Repositories } } + public async Task> GetProfileByAdminRole(string? accessToken, int? node, string? nodeId, string role, string? revisionId) + { + try + { + var apiPath = $"{_configuration["API"]}/org/dotnet/officer-by-admin-role"; + var apiKey = _configuration["API_KEY"]; + var body = new + { + node = node, + nodeId = nodeId, + role = role, + revisionId = revisionId, + }; + + var profiles = new List(); + + var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey); + if (apiResult != null) + { + var raw = JsonConvert.DeserializeObject(apiResult); + if (raw != null) + return raw.Result; + } + + return null; + } + catch + { + throw; + } + } + public async Task> GetProfileWithKeycloakAllOfficerRetireFilterAndRevision(string? accessToken, int? node, string? nodeId, bool isAll, bool? isRetirement, string? revisionId) { try @@ -619,6 +651,39 @@ namespace BMA.EHR.Application.Repositories throw; } } + + public async Task> GetEmployeeByAdminRole(string? accessToken, int? node, string? nodeId, string role, string? revisionId) + { + try + { + var apiPath = $"{_configuration["API"]}/org/dotnet/employee-by-admin-role"; + var apiKey = _configuration["API_KEY"]; + var body = new + { + node = node, + nodeId = nodeId, + role = role, + revisionId = revisionId, + }; + + var profiles = new List(); + + var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey); + if (apiResult != null) + { + var raw = JsonConvert.DeserializeObject(apiResult); + if (raw != null) + return raw.Result; + } + + return null; + } + catch + { + throw; + } + } + public async Task> SearchProfile(string? citizenId, string? firstName, string? lastName, string accessToken) { try diff --git a/BMA.EHR.Leave/Controllers/LeaveReportController.cs b/BMA.EHR.Leave/Controllers/LeaveReportController.cs index b33c2a1e..c2aceda1 100644 --- a/BMA.EHR.Leave/Controllers/LeaveReportController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveReportController.cs @@ -949,22 +949,47 @@ namespace BMA.EHR.Leave.Service.Controllers { try { - var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_SALARY_OFFICER"); + var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_LEAVE_REPORT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var profile = new List(); + string role = jsonData["result"]; + var nodeId = string.Empty; + var profileAdmin = new GetUserOCAllDto(); + profileAdmin = await _userProfileRepository.GetUserOCAll(Guid.Parse(UserId!), AccessToken); + if (role == "NORMAL" || role == "CHILD") + { + nodeId = profileAdmin?.Node == 4 + ? profileAdmin?.Child4DnaId + : profileAdmin?.Node == 3 + ? profileAdmin?.Child3DnaId + : profileAdmin?.Node == 2 + ? profileAdmin?.Child2DnaId + : profileAdmin?.Node == 1 + ? profileAdmin?.Child1DnaId + : profileAdmin?.Node == 0 + ? profileAdmin?.RootDnaId + : ""; + } + else if (role == "ROOT") + { + nodeId = profileAdmin?.RootDnaId; + } + if (type.Trim().ToUpper() == "OFFICER") { //profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficerAndRevision(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD", req.revisionId); - profile = await _userProfileRepository.GetProfileWithNoneValidateKeycloakAllOfficerAndRevision(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD", req.revisionId); + //profile = await _userProfileRepository.GetProfileWithNoneValidateKeycloakAllOfficerAndRevision(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD", req.revisionId); + profile = await _userProfileRepository.GetProfileByAdminRole(AccessToken, profileAdmin?.Node, nodeId, role, req.revisionId); } else { //profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployeeAndRevision(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD", req.revisionId); - profile = await _userProfileRepository.GetProfileWithNoneValidateKeycloakAllEmployeeAndRevision(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD", req.revisionId); + //profile = await _userProfileRepository.GetProfileWithNoneValidateKeycloakAllEmployeeAndRevision(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD", req.revisionId); + profile = await _userProfileRepository.GetEmployeeByAdminRole(AccessToken, profileAdmin?.Node, nodeId, role, req.revisionId); } // get leave day var leaveDays = await _leaveRequestRepository.GetSumApproveLeaveByTypeAndRange(req.StartDate, req.EndDate); @@ -1338,15 +1363,39 @@ namespace BMA.EHR.Leave.Service.Controllers return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var profile = new List(); + string role = jsonData["result"]; + var nodeId = string.Empty; + var profileAdmin = new GetUserOCAllDto(); + profileAdmin = await _userProfileRepository.GetUserOCAll(Guid.Parse(UserId!), AccessToken); + if (role == "NORMAL" || role == "CHILD") + { + nodeId = profileAdmin?.Node == 4 + ? profileAdmin?.Child4DnaId + : profileAdmin?.Node == 3 + ? profileAdmin?.Child3DnaId + : profileAdmin?.Node == 2 + ? profileAdmin?.Child2DnaId + : profileAdmin?.Node == 1 + ? profileAdmin?.Child1DnaId + : profileAdmin?.Node == 0 + ? profileAdmin?.RootDnaId + : ""; + } + else if (role == "ROOT") + { + nodeId = profileAdmin?.RootDnaId; + } if (type.Trim().ToUpper() == "OFFICER") { //profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); - profile = await _userProfileRepository.GetProfileWithNoneValidateKeycloakAllOfficer(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); + //profile = await _userProfileRepository.GetProfileWithNoneValidateKeycloakAllOfficer(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); + profile = await _userProfileRepository.GetProfileByAdminRole(AccessToken, profileAdmin?.Node, nodeId, role, req.revisionId); } else { //profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); - profile = await _userProfileRepository.GetProfileWithNoneValidateKeycloakAllEmployee(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); + //profile = await _userProfileRepository.GketProfileWithNoneValidateKeycloakAllEmployee(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD"); + profile = await _userProfileRepository.GetEmployeeByAdminRole(AccessToken, profileAdmin?.Node, nodeId, role, req.revisionId); } var date = req.StartDate.Date; @@ -1630,8 +1679,30 @@ namespace BMA.EHR.Leave.Service.Controllers var employees = new List(); var count = 1; - + string role = jsonData["result"]; + var nodeId = string.Empty; + var profileAdmin = new GetUserOCAllDto(); + profileAdmin = await _userProfileRepository.GetUserOCAll(Guid.Parse(UserId!), AccessToken); + if (role == "NORMAL" || role == "CHILD") + { + nodeId = profileAdmin?.Node == 4 + ? profileAdmin?.Child4DnaId + : profileAdmin?.Node == 3 + ? profileAdmin?.Child3DnaId + : profileAdmin?.Node == 2 + ? profileAdmin?.Child2DnaId + : profileAdmin?.Node == 1 + ? profileAdmin?.Child1DnaId + : profileAdmin?.Node == 0 + ? profileAdmin?.RootDnaId + : ""; + } + else if (role == "ROOT") + { + nodeId = profileAdmin?.RootDnaId; + } var userTimeStamps = await _processUserTimeStampRepository.GetTimestampByDateLateAsync(type.Trim().ToUpper(), jsonData["result"]?.ToString(), req.nodeId, req.node, req.StartDate, req.EndDate); + //var userTimeStamps = await _processUserTikmeStampRepository.GetTimestampByDateLateAsync(type.Trim().ToUpper(), role, nodeId, profileAdmin?.Node, req.StartDate, req.EndDate); foreach (var p in userTimeStamps) { var fullName = $"{p.Prefix}{p.FirstName} {p.LastName}"; @@ -1713,7 +1784,30 @@ namespace BMA.EHR.Leave.Service.Controllers { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } - var leaveDays = await _leaveRequestRepository.GetSumApproveLeaveByRootAndRange(req.StartDate, req.EndDate, type, jsonData["result"]?.ToString(), req.nodeId, req.node); + + string role = jsonData["result"]; + var nodeId = string.Empty; + var profileAdmin = new GetUserOCAllDto(); + profileAdmin = await _userProfileRepository.GetUserOCAll(Guid.Parse(UserId!), AccessToken); + if (role == "NORMAL" || role == "CHILD") + { + nodeId = profileAdmin?.Node == 4 + ? profileAdmin?.Child4DnaId + : profileAdmin?.Node == 3 + ? profileAdmin?.Child3DnaId + : profileAdmin?.Node == 2 + ? profileAdmin?.Child2DnaId + : profileAdmin?.Node == 1 + ? profileAdmin?.Child1DnaId + : profileAdmin?.Node == 0 + ? profileAdmin?.RootDnaId + : ""; + } + else if (role == "ROOT") + { + nodeId = profileAdmin?.RootDnaId; + } + var leaveDays = await _leaveRequestRepository.GetSumApproveLeaveByRootAndRange(req.StartDate, req.EndDate, type, jsonData["result"]?.ToString(), nodeId, profileAdmin?.Node); var enddate = req.EndDate.Date == req.StartDate.Date ? "" : $" - {req.EndDate.Date.ToThaiShortDate().ToThaiNumber()}"; var result = new {