From 5c5c85a120eb32cf2fa60842bb02b2db507984f2 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Thu, 17 Aug 2023 12:43:47 +0700 Subject: [PATCH 1/2] 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; + } +} From c649861bd618439463757eaf2a305582f3c5daf9 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Thu, 17 Aug 2023 13:05:05 +0700 Subject: [PATCH 2/2] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88?= =?UTF-8?q?=E0=B8=A1=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CommandRepository.cs | 11 +++- .../Controllers/OrderController.cs | 51 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index cf77fef5..213a2937 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1240,7 +1240,16 @@ namespace BMA.EHR.Application.Repositories.Commands var rootOcId = await GetRootOcIdAsync(UserOrganizationId); - var oc = await _dbContext.Set().FirstOrDefaultAsync(x => x.Id == rootOcId); + var oc = await _dbContext.Set() + .Include(x => x.OrganizationOrganization) + .Select(x => new KeyValueItemResponse + { + Id = x.Id, + Name = x.OrganizationOrganization!.Name + }) + .FirstOrDefaultAsync(x => x.Id == rootOcId); + if (oc != null) + ret.Add(oc); return ret; } diff --git a/BMA.EHR.Command.Service/Controllers/OrderController.cs b/BMA.EHR.Command.Service/Controllers/OrderController.cs index b6b4f81e..b3b1be0d 100644 --- a/BMA.EHR.Command.Service/Controllers/OrderController.cs +++ b/BMA.EHR.Command.Service/Controllers/OrderController.cs @@ -1145,6 +1145,57 @@ namespace BMA.EHR.Command.Service.Controllers } } + /// + /// แสดงรายชื่อหน่วยงานสำหรับเลือกเพื่อออกคำสั่ง + /// + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("organizations")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetCommandOrganizationAsync() + { + try + { + var data = await _repository.GetCommandOrgAsync(); + + return Success(data); + } + catch + { + throw; + } + } + + /// + /// แสดงชื่อผู้อนุมัติในรายการคำสั่ง + /// + /// Id ของหน่วยงานที่เลือกเพื่อออกคำสั่ง + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("approver/{ocId}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetCommandOrganizationApproverAsync(Guid ocId) + { + try + { + var data = await _repository.GetOrgApproverAsync(ocId); + + return Success(data); + } + catch + { + throw; + } + } + #endregion } }