diff --git a/BMA.EHR.Application/Repositories/GenericRepository.cs b/BMA.EHR.Application/Repositories/GenericRepository.cs index 44503ad2..a2dcc868 100644 --- a/BMA.EHR.Application/Repositories/GenericRepository.cs +++ b/BMA.EHR.Application/Repositories/GenericRepository.cs @@ -4,7 +4,11 @@ using BMA.EHR.Domain.Models.Base; using BMA.EHR.Domain.Models.HR; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using System.Net.Http.Headers; using System.Security.Claims; +using System.Text; namespace BMA.EHR.Application.Repositories { @@ -43,6 +47,60 @@ namespace BMA.EHR.Application.Repositories #region " Methods " + #region " For Call External API " + + protected async Task GetExternalAPIAsync(string apiPath, string accessToken) + { + try + { + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", "")); + var _res = await client.GetAsync(apiPath); + if (_res.IsSuccessStatusCode) + { + var _result = await _res.Content.ReadAsStringAsync(); + + return _result; + } + return string.Empty; + } + } + catch + { + throw; + } + } + + protected async Task PostExternalAPIAsync(string apiPath, string accessToken, object? body) + { + try + { + var json = JsonConvert.SerializeObject(body); + var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json"); + stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", "")); + var _res = await client.PostAsync(apiPath, stringContent); + if (_res.IsSuccessStatusCode) + { + var _result = await _res.Content.ReadAsStringAsync(); + + return _result; + } + return string.Empty; + } + } + catch + { + throw; + } + } + + #endregion + public async Task GetProfileOrganizationAsync(string citizenId) { try diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index 893af1d7..69b15c8c 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -1,11 +1,14 @@ -using System.Security.Cryptography.X509Certificates; -using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Application.Responses.Profiles; using BMA.EHR.Domain.Models.HR; using BMA.EHR.Domain.Models.MetaData; using BMA.EHR.Domain.Models.Organizations; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; namespace BMA.EHR.Application.Repositories { @@ -15,15 +18,19 @@ namespace BMA.EHR.Application.Repositories private readonly IApplicationDBContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; + private readonly IConfiguration _configuration; #endregion #region " Costructor and Destructor " - public UserProfileRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) + public UserProfileRepository(IApplicationDBContext dbContext, + IHttpContextAccessor httpContextAccessor, + IConfiguration configuration) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; + _configuration = configuration; } #endregion @@ -122,29 +129,51 @@ namespace BMA.EHR.Application.Repositories } } - public async Task> SearchProfile(string? citizenId, string? firstName, string? lastName) + public async Task> SearchProfile(string? citizenId, string? firstName, string? lastName, string accessToken) { try { - var data = _dbContext.Set().AsQueryable() - .Where(x => x.ProfileType == "officer"); + var apiPath = $"{_configuration["API"]}/org/dotnet/search"; + var body = new + { + citizenId = citizenId, + firstName = firstName, + lastName = lastName + }; + + var profiles = new List(); + + var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body); + if (apiResult != null) + { + var raw = JsonConvert.DeserializeObject(apiResult); + if (raw != null && raw.Result != null) + { + profiles.AddRange(raw.Result); + } + } + + return profiles; + + //var data = _dbContext.Set().AsQueryable() + // .Where(x => x.ProfileType == "officer"); - if (citizenId != null) - data = data.Where(x => x.CitizenId!.Contains(citizenId)); + //if (citizenId != null) + // data = data.Where(x => x.CitizenId!.Contains(citizenId)); - if (firstName != null) - data = data.Where(x => x.FirstName!.Contains(firstName)); + //if (firstName != null) + // data = data.Where(x => x.FirstName!.Contains(firstName)); - if (lastName != null) - data = data.Where(x => x.LastName!.Contains(lastName)); + //if (lastName != null) + // data = data.Where(x => x.LastName!.Contains(lastName)); - data = data.Include(x => x.Prefix) - .Include(x => x.Position) - .Include(x => x.PositionLevel) - .Include(x => x.PosNo); + //data = data.Include(x => x.Prefix) + // .Include(x => x.Position) + // .Include(x => x.PositionLevel) + // .Include(x => x.PosNo); - return await data.ToListAsync(); + //return await data.ToListAsync(); } catch { diff --git a/BMA.EHR.Application/Responses/Profiles/SearchProfileDto.cs b/BMA.EHR.Application/Responses/Profiles/SearchProfileDto.cs new file mode 100644 index 00000000..b557f7d9 --- /dev/null +++ b/BMA.EHR.Application/Responses/Profiles/SearchProfileDto.cs @@ -0,0 +1,12 @@ +namespace BMA.EHR.Application.Responses.Profiles +{ + public class SearchProfileDto + { + public Guid Id { get; set; } + + public string? Prefix { get; set; } + public string? FirstName { get; set; } + public string? LastName { get; set; } + public string? CitizenId { get; set; } + } +} diff --git a/BMA.EHR.Application/Responses/Profiles/SearchProfileResultDto.cs b/BMA.EHR.Application/Responses/Profiles/SearchProfileResultDto.cs new file mode 100644 index 00000000..95c26ed7 --- /dev/null +++ b/BMA.EHR.Application/Responses/Profiles/SearchProfileResultDto.cs @@ -0,0 +1,12 @@ +namespace BMA.EHR.Application.Responses.Profiles +{ + public class SearchProfileResultDto + { + public string Messsage { get; set; } = string.Empty; + + public int Status { get; set; } = -1; + + public List Result { get; set; } = new List(); + + } +} diff --git a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs index 25964891..3e71e0f6 100644 --- a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs @@ -2,7 +2,6 @@ using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Application.Repositories.Leaves.LeaveRequests; using BMA.EHR.Application.Repositories.Leaves.TimeAttendants; -using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Models.Leave.TimeAttendants; using BMA.EHR.Domain.Shared; @@ -15,7 +14,6 @@ using BMA.EHR.Leave.Service.DTOs.DutyTime; using BMA.EHR.Leave.Service.DTOs.LeaveRequest; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Org.BouncyCastle.Ocsp; using Swashbuckle.AspNetCore.Annotations; using System.ComponentModel.DataAnnotations; using System.Security.Claims; @@ -97,6 +95,8 @@ namespace BMA.EHR.Leave.Service.Controllers private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1"); + private string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"]; + private Guid OcId { get @@ -951,11 +951,11 @@ namespace BMA.EHR.Leave.Service.Controllers [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> SearchProfileAsync([FromBody] SearchProfileDto req) { - var profile = await _userProfileRepository.SearchProfile(req.CitizenId, req.FirstName, req.LastName); + + var profile = await _userProfileRepository.SearchProfile(req.CitizenId, req.FirstName, req.LastName, AccessToken ?? ""); var pagedProfile = profile.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList(); - var getDefaultRound = await _dutyTimeRepository.GetDefaultAsync(); var resultSet = new List(); @@ -972,8 +972,8 @@ namespace BMA.EHR.Leave.Service.Controllers var res = new SearchProfileResultDto { ProfileId = p.Id, - CitizenId = p.CitizenId, - FullName = $"{p.Prefix.Name}{p.FirstName} {p.LastName}", + CitizenId = p.CitizenId ?? "", + FullName = $"{p.Prefix ?? "" }{p.FirstName ?? ""} {p.LastName ?? ""}", StartTimeMorning = duty.StartTimeMorning, LeaveTimeAfterNoon = duty.EndTimeAfternoon, EffectiveDate = effectiveDate == null ? null : effectiveDate.EffectiveDate.Value.Date