From 5c5c85a120eb32cf2fa60842bb02b2db507984f2 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Thu, 17 Aug 2023 12:43:47 +0700 Subject: [PATCH] Add Org and Approver Api --- .../Commands/CommandRepository.cs | 114 +++++++++++++++++- .../Repositories/GenericRepository.cs | 6 + .../Responses/KeyValueItemResponse.cs | 9 ++ 3 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 BMA.EHR.Application/Responses/KeyValueItemResponse.cs diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index 67679328..cf77fef5 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -24,6 +24,7 @@ namespace BMA.EHR.Application.Repositories.Commands private readonly IApplicationDBContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; private readonly OrganizationCommonRepository _organizationCommonRepository; + private readonly UserProfileRepository _userProfileRepository; #endregion @@ -31,11 +32,28 @@ namespace BMA.EHR.Application.Repositories.Commands public CommandRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor, - OrganizationCommonRepository organizationCommonRepository) : base(dbContext, httpContextAccessor) + OrganizationCommonRepository organizationCommonRepository, + UserProfileRepository userProfileRepository) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; _organizationCommonRepository = organizationCommonRepository; + _userProfileRepository = userProfileRepository; + } + + #endregion + + #region " Properties " + + protected Guid UserOrganizationId + { + get + { + if (UserId != null || UserId != "") + return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!)); + else + return Guid.Empty; + } } #endregion @@ -490,6 +508,7 @@ namespace BMA.EHR.Application.Repositories.Commands .Include(x => x.Receivers) .Include(x => x.CommandStatus) .FirstOrDefaultAsync(x => x.Id == id); + } public override async Task> GetAllAsync() @@ -1188,13 +1207,13 @@ namespace BMA.EHR.Application.Repositories.Commands #endregion + #region " Organization and Approver " + public async Task GetRootOcIdAsync(Guid ocId) { try { var data = await _dbContext.Set().AsQueryable() - //.Include(x => x.OrganizationAgency) - //.Include(x => x.OrganizationGovernmentAgency) .FirstOrDefaultAsync(o => o.Id == ocId); if (data == null) @@ -1208,6 +1227,95 @@ namespace BMA.EHR.Application.Repositories.Commands } } + public async Task> GetCommandOrgAsync() + { + try + { + var ret = new List(); + ret.Add(new KeyValueItemResponse + { + Id = Guid.Empty, + Name = "กรุงเทพมหานคร" + }); + + var rootOcId = await GetRootOcIdAsync(UserOrganizationId); + + var oc = await _dbContext.Set().FirstOrDefaultAsync(x => x.Id == rootOcId); + + return ret; + } + catch + { + throw; + } + } + + public async Task> GetOrgApproverAsync(Guid ocId) + { + try + { + if (ocId == Guid.Empty) + return new List() { new KeyValueItemResponse { Id = Guid.Empty, Name = "ปลัดกรุงเทพมหานคร" } }; + else + { + //var ret = new List(); + var oc = await _dbContext.Set().Include(x => x.Parent).FirstOrDefaultAsync(x => x.Id == ocId); + + var profilePosition = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Prefix) + .Include(x => x.OrganizationPosition) + .ThenInclude(x => x!.Organization) + .Include(x => x.OrganizationPosition) + .ThenInclude(x => x!.PositionMaster) + .Where(x => x.OrganizationPosition!.Organization!.Id == ocId && + x.OrganizationPosition!.PositionMaster!.IsDirector == true) + .Select(x => new KeyValueItemResponse + { + Id = x.Profile!.Id, + Name = $"{x.Profile!.Prefix!.Name}{x.Profile!.FirstName} {x.Profile!.LastName}" + }) + .ToListAsync(); + + if (profilePosition.Count > 0) + { + return profilePosition; + } + else + { + if (oc != null && oc.Parent != null) + { + var parentProfilePosition = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Prefix) + .Include(x => x.OrganizationPosition) + .ThenInclude(x => x!.Organization) + .Include(x => x.OrganizationPosition) + .ThenInclude(x => x!.PositionMaster) + .Where(x => x.OrganizationPosition!.Organization!.Id == oc.Parent.Id && + x.OrganizationPosition!.PositionMaster!.IsDirector == true) + .Select(x => new KeyValueItemResponse + { + Id = x.Profile!.Id, + Name = $"{x.Profile!.Prefix!.Name}{x.Profile!.FirstName} {x.Profile!.LastName}" + }) + .ToListAsync(); + + return parentProfilePosition; + } + else + return new List(); + } + } + } + catch + { + throw; + } + } + + #endregion + #endregion } diff --git a/BMA.EHR.Application/Repositories/GenericRepository.cs b/BMA.EHR.Application/Repositories/GenericRepository.cs index f752c581..2f2a6e58 100644 --- a/BMA.EHR.Application/Repositories/GenericRepository.cs +++ b/BMA.EHR.Application/Repositories/GenericRepository.cs @@ -13,6 +13,7 @@ namespace BMA.EHR.Application.Repositories private readonly IApplicationDBContext _dbContext; private readonly DbSet _dbSet; private readonly IHttpContextAccessor _httpContextAccessor; + #endregion @@ -24,6 +25,7 @@ namespace BMA.EHR.Application.Repositories _dbContext = dbContext; _dbSet = _dbContext.Set(); _httpContextAccessor = httpContextAccessor; + } #endregion @@ -34,6 +36,10 @@ namespace BMA.EHR.Application.Repositories protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; + protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1"); + + + #endregion #region " Methods " diff --git a/BMA.EHR.Application/Responses/KeyValueItemResponse.cs b/BMA.EHR.Application/Responses/KeyValueItemResponse.cs new file mode 100644 index 00000000..63fdd6d6 --- /dev/null +++ b/BMA.EHR.Application/Responses/KeyValueItemResponse.cs @@ -0,0 +1,9 @@ +namespace BMA.EHR.Application.Responses +{ + public class KeyValueItemResponse + { + public Guid Id { get; set; } = Guid.Empty; + + public string Name { get; set; } = string.Empty; + } +}