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.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; namespace BMA.EHR.Application.Repositories { public class UserProfileRepository : GenericRepository { #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 GetOrganizationById(Guid id) { var data = await _dbContext.Set().AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); return data; } public async Task GetOrgAgencyById(Guid id) { var data = await _dbContext.Set().AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); return data; } public async Task GetOrgGovAgencyById(Guid id) { var data = await _dbContext.Set().AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); return data; } public async Task 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(apiResult); if (raw != null) return raw.Result; } return null; //var data = await _dbContext.Set().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 UpdateDutyTimeAsync(Guid profileId, Guid roundId, DateTime effectiveDate) { try { var profile = await _dbContext.Set() .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> GetProfileWithKeycloak() { try { var data = await _dbContext.Set().AsQueryable() .Where(x => x.ProfileType == "officer") .Where(x => x.KeycloakId != null) .ToListAsync(); return data; } catch { throw; } } public async Task> 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(); var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body); if (apiResult != null) { var raw = JsonConvert.DeserializeObject(apiResult); if (raw != null && raw.Result != null) { profiles.AddRange(raw.Result); } } return profiles; //var data = _dbContext.Set().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> SearchProfileEmployee(string? citizenId, string? firstName, string? lastName) { try { var data = _dbContext.Set().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().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() .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() .FirstOrDefault(o => o.Id == ocId); return data == null ? Guid.Empty : data.OrganizationAgencyId; } catch { throw; } } #endregion } }