Merge branch 'develop' of github.com:Frappet/BMA-EHR-BackEnd into develop

This commit is contained in:
Kittapath 2024-05-29 16:29:33 +07:00
commit 94980d9157
5 changed files with 134 additions and 23 deletions

View file

@ -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<string> 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<string> 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<Guid> GetProfileOrganizationAsync(string citizenId)
{
try

View file

@ -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<List<Profile>> SearchProfile(string? citizenId, string? firstName, string? lastName)
public async Task<List<SearchProfileDto>> SearchProfile(string? citizenId, string? firstName, string? lastName, string accessToken)
{
try
{
var data = _dbContext.Set<Profile>().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<SearchProfileDto>();
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<SearchProfileResultDto>(apiResult);
if (raw != null && raw.Result != null)
{
profiles.AddRange(raw.Result);
}
}
return profiles;
//var data = _dbContext.Set<Profile>().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
{

View file

@ -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; }
}
}

View file

@ -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<SearchProfileDto> Result { get; set; } = new List<SearchProfileDto>();
}
}

View file

@ -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<ActionResult<ResponseObject>> 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<SearchProfileResultDto>();
@ -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