hrms-api-backend/BMA.EHR.Application/Repositories/PermissionRepository.cs
Suphonchai Phoonsawat d945deae4f
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m15s
Add error handling for permission API calls and enhance logging in middleware
2026-01-22 11:58:26 +07:00

135 lines
4.5 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Responses;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Organizations;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using Microsoft.Extensions.Configuration;
using System.Security.Claims;
using System.Net.Http.Json;
namespace BMA.EHR.Application.Repositories
{
public class PermissionRepository
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfiguration _configuration;
#endregion
#region " Constructor and Destuctor "
public PermissionRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
_configuration = configuration;
}
#endregion
#region " Properties "
//protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
//protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
//protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
#endregion
#region " Methods "
public async Task<dynamic> GetPermissionAPIAsync(string action, string system)
{
try
{
var apiPath = $"{_configuration["API"]}/org/permission/dotnet/{action}/{system}";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
var req = await client.GetAsync(apiPath);
if (!req.IsSuccessStatusCode)
{
throw new Exception("Error calling permission API");
}
var res = await req.Content.ReadAsStringAsync();
return res;
}
}
catch
{
throw;
}
}
public async Task<dynamic> GetPermissionOrgAPIAsync(string action, string system, string profileId)
{
try
{
var apiPath = $"{_configuration["API"]}/org/permission/dotnet-org/{action}/{system}/{profileId}";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
var req = await client.GetAsync(apiPath);
var res = await req.Content.ReadAsStringAsync();
return res;
}
}
catch
{
throw;
}
}
public async Task<dynamic> GetPermissionAPIWorkflowAsync(string refId, string sysName)
{
try
{
var apiPath = $"{_configuration["API"]}/org/workflow/keycloak/isofficer";
using (var client = new HttpClient())
{
var res = await client.PostAsJsonAsync(apiPath, new
{
data = new
{
refId = refId,
sysName = sysName,
},
});
var result = await res.Content.ReadAsStringAsync();
if (res.IsSuccessStatusCode)
{
return true;
}
else
{
return false;
}
}
}
catch
{
throw;
}
}
#endregion
}
}