hrms-api-backend/BMA.EHR.Application/Repositories/UserProfileRepository.cs
2023-12-12 14:17:00 +07:00

160 lines
4.7 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Domain.Models.HR;
using BMA.EHR.Domain.Models.Organizations;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Application.Repositories
{
public class UserProfileRepository : GenericRepository<Guid, Profile>
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Costructor and Destructor "
public UserProfileRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region " Methods "
public async Task<Profile?> GetProfileByKeycloakIdAsync(Guid keycloakId)
{
try
{
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>().FirstOrDefaultAsync(x => x.Id == profileId);
if (profile == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
else
{
profile.DutyTimeId = roundId;
profile.DutyTimeEffectiveDate = effectiveDate;
await UpdateAsync(profile);
return true;
}
}
catch
{
throw;
}
}
public async Task<List<Profile>> SearchProfile(string? citizenId, string? firstName, string? lastName)
{
try
{
var data = _dbContext.Set<Profile>().AsQueryable();
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);
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
}
}