hrms-api-backend/BMA.EHR.Application/Repositories/UserProfileRepository.cs
2024-05-30 09:32:34 +07:00

285 lines
9.1 KiB
C#

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
{
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<GetProfileByKeycloakIdDto?> GetProfileByKeycloakIdAsync(Guid keycloakId, string? accessToken)
{
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak/{keycloakId}";
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "");
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultDto>(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<bool> UpdateDutyTimeAsync(Guid profileId, Guid roundId, DateTime effectiveDate)
{
try
{
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<Profile>> GetProfileWithKeycloak()
{
try
{
var data = await _dbContext.Set<Profile>().AsQueryable()
.Where(x => x.ProfileType == "officer")
.Where(x => x.KeycloakId != null)
.ToListAsync();
return data;
}
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 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 (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<Profile>> SearchProfileEmployee(string? citizenId, string? firstName, string? lastName)
{
try
{
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)
{
try
{
var data = _dbContext.Set<Profile>().AsQueryable()
.Include(x => x.Prefix)
.Where(x => x.KeycloakId == keycloakId)
.Select(x => $"{x.Prefix!.Name}{x.FirstName} {x.LastName}")
.FirstOrDefault();
return data ?? "-";
}
catch
{
throw;
}
}
public Guid GetUserOCId(Guid keycloakId)
{
try
{
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 Guid? GetRootOcId(Guid ocId)
{
try
{
var data = _dbContext.Set<OrganizationEntity>()
.FirstOrDefault(o => o.Id == ocId);
return data == null ? Guid.Empty : data.OrganizationAgencyId;
}
catch
{
throw;
}
}
#endregion
}
}