683 lines
23 KiB
C#
683 lines
23 KiB
C#
using BMA.EHR.Application.Common.Interfaces;
|
|
using BMA.EHR.Application.Responses.Insignias;
|
|
using BMA.EHR.Application.Responses.Organizations;
|
|
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 Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using System.Reflection.Emit;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace BMA.EHR.Application.Repositories
|
|
{
|
|
public class UserProfileRepository : GenericRepository<Guid, Profile>
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly IApplicationDBContext _dbContext;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
#endregion
|
|
|
|
#region " Costructor and Destructor "
|
|
|
|
public UserProfileRepository(IApplicationDBContext dbContext,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
IConfiguration configuration) : base(dbContext, httpContextAccessor)
|
|
{
|
|
_dbContext = dbContext;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " Methods "
|
|
|
|
public async Task<OrganizationEntity?> GetOrganizationById(Guid id)
|
|
{
|
|
var data = await _dbContext.Set<OrganizationEntity>().AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<OrganizationAgency?> GetOrgAgencyById(Guid id)
|
|
{
|
|
var data = await _dbContext.Set<OrganizationAgency>().AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<OrganizationGovernmentAgency?> GetOrgGovAgencyById(Guid id)
|
|
{
|
|
var data = await _dbContext.Set<OrganizationGovernmentAgency>().AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<List<GetProfileByRootIdDto>> GetOfficerProfileByRootIdAsync(Guid rootId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/root/officer/{rootId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByRootIdResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
|
|
//var data = await _dbContext.Set<Profile>().AsQueryable()
|
|
// .Include(p => p.Prefix)
|
|
// .Include(p => p.Position)
|
|
// .Include(p => p.PositionLevel)
|
|
// .Include(p => p.Salaries)
|
|
// .FirstOrDefaultAsync(p => p.KeycloakId == keycloakId);
|
|
|
|
//return data;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<GetProfileByRootIdDto>> GetEmployeeProfileByRootIdAsync(Guid rootId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/root/employee/{rootId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByRootIdResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
|
|
//var data = await _dbContext.Set<Profile>().AsQueryable()
|
|
// .Include(p => p.Prefix)
|
|
// .Include(p => p.Position)
|
|
// .Include(p => p.PositionLevel)
|
|
// .Include(p => p.Salaries)
|
|
// .FirstOrDefaultAsync(p => p.KeycloakId == keycloakId);
|
|
|
|
//return data;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<GetProfileByKeycloakIdDto?> GetProfileByKeycloakIdAsync(Guid keycloakId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak/{keycloakId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<GetProfileByKeycloakIdDto?> GetProfileByProfileIdAsync(Guid keycloakId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/profile/{keycloakId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateDutyTimeAsync(Guid profileId, Guid roundId, DateTime effectiveDate, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/update-dutytime";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var body = new
|
|
{
|
|
EffevtiveDate = effectiveDate,
|
|
RoundId = roundId,
|
|
ProfileId = profileId
|
|
};
|
|
|
|
var apiResult = await PostExternalAPIBooleanAsync(apiPath, accessToken ?? "", body, apiKey);
|
|
|
|
return apiResult;
|
|
//var profile = await _dbContext.Set<Profile>()
|
|
// .AsQueryable()
|
|
// .Include(x => x.Prefix)
|
|
// .FirstOrDefaultAsync(x => x.Id == profileId);
|
|
|
|
//if (profile == null)
|
|
//{
|
|
// throw new Exception(GlobalMessages.DataNotFound);
|
|
//}
|
|
//else
|
|
//{
|
|
// var fullName = $"{profile.Prefix.Name}{profile.FirstName} {profile.LastName}";
|
|
// Console.WriteLine(fullName);
|
|
|
|
// profile.DutyTimeId = roundId;
|
|
// profile.DutyTimeEffectiveDate = effectiveDate;
|
|
|
|
// await UpdateAsync(profile);
|
|
|
|
// return true;
|
|
//}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<GetProfileByKeycloakIdDto>> GetProfileWithKeycloak(string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetListProfileByKeycloakIdResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<SearchProfileDto>> SearchProfile(string? citizenId, string? firstName, string? lastName, string accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/search";
|
|
var apiKey = _configuration["API_KEY"];
|
|
var body = new
|
|
{
|
|
citizenId = citizenId,
|
|
firstName = firstName,
|
|
lastName = lastName
|
|
};
|
|
|
|
var profiles = new List<SearchProfileDto>();
|
|
|
|
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey);
|
|
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 (firstName != null)
|
|
// data = data.Where(x => x.FirstName!.Contains(firstName));
|
|
|
|
//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);
|
|
|
|
//return await data.ToListAsync();
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<SearchProfileDto>> SearchProfileEmployee(string? citizenId, string? firstName, string? lastName, string accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/search-employee";
|
|
var apiKey = _configuration["API_KEY"];
|
|
var body = new
|
|
{
|
|
citizenId = citizenId,
|
|
firstName = firstName,
|
|
lastName = lastName
|
|
};
|
|
|
|
var profiles = new List<SearchProfileDto>();
|
|
|
|
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey);
|
|
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 == "employee");
|
|
|
|
|
|
//if (citizenId != null)
|
|
// data = data.Where(x => x.CitizenId!.Contains(citizenId));
|
|
|
|
//if (firstName != null)
|
|
// data = data.Where(x => x.FirstName!.Contains(firstName));
|
|
|
|
//if (lastName != null)
|
|
// data = data.Where(x => x.LastName!.Contains(lastName));
|
|
|
|
//data = data.Include(x => x.Prefix);
|
|
////.Include(x => x.PosNoEmployee);
|
|
|
|
|
|
//return await data.ToListAsync();
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public string GetUserFullName(Guid keycloakId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/user-fullname/{keycloakId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult.Result != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetUserFullNameResultDto>(apiResult.Result);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Guid GetUserOCId(Guid keycloakId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/user-oc/{keycloakId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult.Result != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetUserOCIdDto>(apiResult.Result);
|
|
if (raw == null || raw.RootId == null)
|
|
return Guid.Empty;
|
|
return raw.RootId;
|
|
}
|
|
|
|
return Guid.Empty;
|
|
|
|
//var data = _dbContext.Set<ProfilePosition>()
|
|
// .Include(x => x.Profile)
|
|
// .Include(x => x.OrganizationPosition)
|
|
// .ThenInclude(x => x.Organization)
|
|
// .Where(x => x.Profile!.KeycloakId == keycloakId)
|
|
// .FirstOrDefault();
|
|
|
|
//if (data == null)
|
|
// throw new Exception(GlobalMessages.DataNotFound);
|
|
|
|
//return data.OrganizationPosition!.Organization!.Id;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public GetUserOCIdDto? GetUserOC(Guid keycloakId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/user-oc/{keycloakId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult.Result != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetUserOCIdDto>(apiResult.Result);
|
|
|
|
return raw;
|
|
|
|
//if (raw == null || raw.RootId == null)
|
|
// return Guid.Empty;
|
|
//return raw.RootId;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Guid? GetRootOcId(Guid ocId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/root-oc/{ocId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult.Result != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetRootOCIdResultDto>(apiResult.Result);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
|
|
//var data = _dbContext.Set<OrganizationEntity>()
|
|
// .FirstOrDefault(o => o.Id == ocId);
|
|
|
|
//return data == null ? Guid.Empty : data.OrganizationAgencyId;
|
|
|
|
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public GetOrganizationResponseDTO? GetOc(Guid ocId, int level, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/find/all";
|
|
var apiKey = _configuration["API_KEY"];
|
|
var body = new
|
|
{
|
|
nodeId = ocId,
|
|
node = level
|
|
|
|
};
|
|
|
|
var apiResult = PostExternalAPIAsync(apiPath, accessToken ?? "", body, apiKey).Result;
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetOrganizationResponseResultDTO>(apiResult);
|
|
if (raw != null && raw.Result != null)
|
|
{
|
|
return raw.Result;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public GetProfileByIdDto GetOfficerProfileById(Guid id, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/profile/{id}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult.Result != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByIdResultDto>(apiResult.Result);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public GetProfileByIdDto GetEmployeeProfileById(Guid id, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/profile-employee/{id}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult.Result != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByIdResultDto>(apiResult.Result);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<GetProfileByKeycloakIdDto?> GetOfficerProfileByCitizenId(string citizenId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/citizenId/{citizenId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult.Result != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultDto>(apiResult.Result);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<GetActiveRootDto>> GetActiveRootAsync(string? accessToken,Guid? revisionId)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/active/root/{revisionId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetActiveRootResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<Guid?> GetLastRevision(string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/active/root/latest";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetLastRevisionDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<GetActiveRootDto>> GetActiveRootLatestAsync(string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/active/root/all";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetActiveRootResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task PostProfileInsigniaAsync(PostProfileInsigniaDto body, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/profile/insignia";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var profiles = new List<SearchProfileDto>();
|
|
|
|
var apiResult = await PostExternalAPIBooleanAsync(apiPath, accessToken ?? "", body, apiKey);
|
|
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<GetIsOfficerDto> GetIsOfficerRootAsync(string? accessToken, string sys)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/workflow/keycloak/isofficer-root/{sys}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetIsOfficerResultDto>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
return new GetIsOfficerDto { isOfficer = false, isStaff = false, isDirector = false };
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|