diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index 5c5c14ff..e9e184cc 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1,5 +1,4 @@ -using Amazon; -using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Requests.Commands; using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Extensions; @@ -13,6 +12,9 @@ using BMA.EHR.Domain.Models.Retirement; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; +using System.Net.Http.Headers; using Command = BMA.EHR.Domain.Models.Commands.Core.Command; using Profile = BMA.EHR.Domain.Models.HR.Profile; @@ -26,6 +28,7 @@ namespace BMA.EHR.Application.Repositories.Commands private readonly IHttpContextAccessor _httpContextAccessor; private readonly OrganizationCommonRepository _organizationCommonRepository; private readonly UserProfileRepository _userProfileRepository; + private readonly IConfiguration _configuration; #endregion @@ -34,12 +37,14 @@ namespace BMA.EHR.Application.Repositories.Commands public CommandRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor, OrganizationCommonRepository organizationCommonRepository, - UserProfileRepository userProfileRepository) : base(dbContext, httpContextAccessor) + UserProfileRepository userProfileRepository, + IConfiguration configuration) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; _organizationCommonRepository = organizationCommonRepository; _userProfileRepository = userProfileRepository; + _configuration = configuration; } #endregion @@ -65,7 +70,7 @@ namespace BMA.EHR.Application.Repositories.Commands #region " List Receiver " - public async Task> GetReceiverForByCommndTypeAsync(Command command) + public async Task> GetReceiverForByCommndTypeAsync(Command command, string token = "") { var result = new List(); @@ -98,6 +103,15 @@ namespace BMA.EHR.Application.Repositories.Commands case "C-PM-09": result = await GetReceiver09Async(command); break; + case "C-PM-10": + result = await GetReceiver10Async(command); + break; + case "C-PM-11": + result = await GetReceiver11Async(command, token); + break; + case "C-PM-12": + result = await GetReceiver12Async(command, token); + break; case "C-PM-13": result = await GetReceiver13Async(command); break; @@ -686,6 +700,226 @@ namespace BMA.EHR.Application.Repositories.Commands } } + + /// + /// C-PM-10 - คำสั่งแต่งตั้งคณะกรรมการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ + /// + /// object ของรายการคำสั่ง + /// + private async Task> GetReceiver10Async(Command command) + { + try + { + var result = new List(); + var seq = 1; + // เอาชื่อของกรรมการ ไปหาจากทะเบียนประวัติ + + var p1 = await _dbContext.Set() + .Include(x => x.Prefix) + .Select(x => new + { + profileId = x.Id, + citizenId = x.CitizenId, + prefix = x.Prefix.Name, + firstName = x.FirstName, + lastName = x.LastName, + fullName = $"{x.Prefix.Name}{x.FirstName} {x.LastName}" + }) + .FirstOrDefaultAsync(x => x.fullName == command.ChairManFullName); + + if (p1 != null) + { + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = p1.citizenId!, + Prefix = p1.prefix!, + FirstName = p1.firstName!, + LastName = p1.lastName!, + RefPlacementProfileId = p1.profileId + }; + seq++; + result.Add(receiver); + } + + var p2 = await _dbContext.Set() + .Include(x => x.Prefix) + .Select(x => new + { + profileId = x.Id, + citizenId = x.CitizenId, + prefix = x.Prefix.Name, + firstName = x.FirstName, + lastName = x.LastName, + fullName = $"{x.Prefix.Name}{x.FirstName} {x.LastName}" + }) + .FirstOrDefaultAsync(x => x.fullName == command.Member1FullName); + + if (p2 != null) + { + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = p2.citizenId!, + Prefix = p2.prefix!, + FirstName = p2.firstName!, + LastName = p2.lastName!, + RefPlacementProfileId = p2.profileId + }; + seq++; + result.Add(receiver); + } + + var p3 = await _dbContext.Set() + .Include(x => x.Prefix) + .Select(x => new + { + profileId = x.Id, + citizenId = x.CitizenId, + prefix = x.Prefix.Name, + firstName = x.FirstName, + lastName = x.LastName, + fullName = $"{x.Prefix.Name}{x.FirstName} {x.LastName}" + }) + .FirstOrDefaultAsync(x => x.fullName == command.Member2FullName); + + if (p3 != null) + { + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = p3.citizenId!, + Prefix = p3.prefix!, + FirstName = p3.firstName!, + LastName = p3.lastName!, + RefPlacementProfileId = p3.profileId + }; + seq++; + result.Add(receiver); + } + return result; + } + catch + { + throw; + } + } + + /// + /// C-PM-11 - คำสั่งให้ข้าราชการที่มีผลการทดลองปฏิบัติหน้าที่ราชการไม่ต่ำกว่ามาตรฐานที่กำหนดรับราชการต่อไป + /// + /// object ของรายการคำสั่ง + /// + private async Task> GetReceiver11Async(Command command, string token) + { + try + { + var resultData = new List(); + + var baseAPI = _configuration["Node:API"]; + var apiUrl = $"{baseAPI}/report/pass"; + + var response = new PassProbationResponse(); + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + var req = new HttpRequestMessage(HttpMethod.Get, apiUrl); + var res = await client.SendAsync(req); + var result = await res.Content.ReadAsStringAsync(); + + response = JsonConvert.DeserializeObject(result); + + var seq = 1; + foreach (var d in response!.data) + { + var pf = await _dbContext.Set() + .Include(x => x.Prefix) + .FirstOrDefaultAsync(x => x.Id == d.person.id); + + if (pf != null) + { + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = pf.CitizenId!, + Prefix = pf.Prefix!.Name, + FirstName = pf.FirstName!, + LastName = pf.LastName!, + RefPlacementProfileId = pf.Id + }; + seq++; + + resultData.Add(receiver); + } + } + } + + return resultData; + } + catch + { + throw; + } + } + + + /// + /// C-PM-12 - คำสั่งให้ข้าราชการออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐานที่กำหนด + /// + /// object ของรายการคำสั่ง + /// + private async Task> GetReceiver12Async(Command command, string token) + { + try + { + var resultData = new List(); + + var baseAPI = _configuration["Node:API"]; + var apiUrl = $"{baseAPI}/report/not-pass"; + + var response = new PassProbationResponse(); + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + var req = new HttpRequestMessage(HttpMethod.Get, apiUrl); + var res = await client.SendAsync(req); + var result = await res.Content.ReadAsStringAsync(); + + response = JsonConvert.DeserializeObject(result); + + var seq = 1; + foreach (var d in response!.data) + { + var pf = await _dbContext.Set() + .Include(x => x.Prefix) + .FirstOrDefaultAsync(x => x.Id == d.person.id); + + if (pf != null) + { + var receiver = new CommandReceiver + { + Sequence = seq, + CitizenId = pf.CitizenId!, + Prefix = pf.Prefix!.Name, + FirstName = pf.FirstName!, + LastName = pf.LastName!, + RefPlacementProfileId = pf.Id + }; + seq++; + + resultData.Add(receiver); + } + } + } + + return resultData; + } + catch + { + throw; + } + } + /// /// C-PM-13 - คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ /// @@ -1645,7 +1879,7 @@ namespace BMA.EHR.Application.Repositories.Commands } } - public async Task> GetReceiverForCommandAsync(Guid id) + public async Task> GetReceiverForCommandAsync(Guid id, string token = "") { try { @@ -1657,7 +1891,7 @@ namespace BMA.EHR.Application.Repositories.Commands if (command == null) throw new Exception(GlobalMessages.CommandNotFound); else - return await GetReceiverForByCommndTypeAsync(command); + return await GetReceiverForByCommndTypeAsync(command, token); } catch { diff --git a/BMA.EHR.Application/Responses/PassProbationResponse.cs b/BMA.EHR.Application/Responses/PassProbationResponse.cs new file mode 100644 index 00000000..b1938786 --- /dev/null +++ b/BMA.EHR.Application/Responses/PassProbationResponse.cs @@ -0,0 +1,21 @@ +namespace BMA.EHR.Application.Responses +{ + public class PassProbationResponse + { + public bool successful { get; set; } + + public List data { get; set; } = new(); + } + + public class DataResponse + { + public ProfileResponse person { get; set; } = new(); + } + + public class ProfileResponse + { + public Guid id { get; set; } = Guid.Empty; + + public string name { get; set; } = string.Empty; + } +} diff --git a/BMA.EHR.Command.Service/Controllers/OrderController.cs b/BMA.EHR.Command.Service/Controllers/OrderController.cs index 99111d89..42b86e83 100644 --- a/BMA.EHR.Command.Service/Controllers/OrderController.cs +++ b/BMA.EHR.Command.Service/Controllers/OrderController.cs @@ -12,8 +12,8 @@ using BMA.EHR.Infrastructure.Persistence; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; +using System.Net.Http.Headers; using System.Security.Claims; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace BMA.EHR.Command.Service.Controllers { @@ -2485,10 +2485,23 @@ namespace BMA.EHR.Command.Service.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetCommandReceiverAsync(Guid orderId) + public async Task> GetCommandReceiverAsync([FromHeader] string authorization, Guid orderId) { try { + var token = string.Empty; + if (AuthenticationHeaderValue.TryParse(authorization, out var headerValue)) + { + // we have a valid AuthenticationHeaderValue that has the following details: + + var scheme = headerValue.Scheme; + token = headerValue.Parameter; + + // scheme will be "Bearer" + // parmameter will be the token itself. + } + + var command = await _repository.GetByIdAsync(orderId); if (command == null) throw new Exception(GlobalMessages.CommandNotFound); @@ -2496,7 +2509,7 @@ namespace BMA.EHR.Command.Service.Controllers // TODO : หาค่า Education มาแสดง var existed = await _repository.GetReceiverByCommmandIdAsync(orderId); - var receivers = (await _repository.GetReceiverForCommandAsync(orderId)) + var receivers = (await _repository.GetReceiverForCommandAsync(orderId, token!)) .OrderBy(x => x.Sequence) .Select(r => new CommandReceiverResponse { diff --git a/BMA.EHR.Command.Service/Program.cs b/BMA.EHR.Command.Service/Program.cs index 35f257f2..9054b535 100644 --- a/BMA.EHR.Command.Service/Program.cs +++ b/BMA.EHR.Command.Service/Program.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Tokens; using Serilog; @@ -47,6 +48,7 @@ var builder = WebApplication.CreateBuilder(args); // Authorization builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(opt => { + opt.SaveToken = true; opt.RequireHttpsMetadata = false; //false for dev opt.Authority = issuer; opt.TokenValidationParameters = new() diff --git a/BMA.EHR.Command.Service/appsettings.json b/BMA.EHR.Command.Service/appsettings.json index ddc81996..0b449f48 100644 --- a/BMA.EHR.Command.Service/appsettings.json +++ b/BMA.EHR.Command.Service/appsettings.json @@ -31,5 +31,8 @@ "SecretKey": "FPTadmin2357", "BucketName": "bma-ehr-fpt" }, - "Protocol": "HTTPS" + "Protocol": "HTTPS", + "Node": { + "API": "https://bma-ehr.frappet.synology.me/api/v1/probation" + } } \ No newline at end of file