From 4cc57322c0a43e15e3d50007167dd742ab4615f9 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Thu, 8 May 2025 22:19:12 +0700 Subject: [PATCH] fix ccode --- .../Repositories/GenericRepository.cs | 43 ++++++++++++++++--- .../Repositories/InsigniaPeriodsRepository.cs | 2 +- .../Repositories/UserProfileRepository.cs | 8 +++- .../Controllers/InsigniaRequestController.cs | 2 +- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/BMA.EHR.Application/Repositories/GenericRepository.cs b/BMA.EHR.Application/Repositories/GenericRepository.cs index 1d562581..1f10ca37 100644 --- a/BMA.EHR.Application/Repositories/GenericRepository.cs +++ b/BMA.EHR.Application/Repositories/GenericRepository.cs @@ -1,15 +1,16 @@ -using Amazon.S3.Model.Internal.MarshallTransformations; +using Amazon.Runtime.Internal.Endpoints.StandardLibrary; +using Amazon.S3.Model.Internal.MarshallTransformations; using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Models.Base; using BMA.EHR.Domain.Models.HR; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; -using Newtonsoft.Json.Linq; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Net.Http.Headers; using System.Security.Claims; using System.Text; -using Microsoft.Extensions.Configuration; namespace BMA.EHR.Application.Repositories { @@ -76,13 +77,45 @@ namespace BMA.EHR.Application.Repositories } } + protected async Task SendExternalAPIAsync(HttpMethod method, string apiPath, string accessToken, object? body, string apiKey) + { + try + { + // สร้าง request message + var request = new HttpRequestMessage(method, apiPath); + + + var json = JsonConvert.SerializeObject(body); + request.Content = new StringContent(json, Encoding.UTF8, "application/json"); + + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", "")); + client.DefaultRequestHeaders.Add("api_key", apiKey); + var _res = await client.SendAsync(request); + if (_res.IsSuccessStatusCode) + { + var _result = await _res.Content.ReadAsStringAsync(); + + return _result; + } + return string.Empty; + } + } + catch + { + throw; + } + } + + protected async Task PostExternalAPIAsync(string apiPath, string accessToken, object? body, string apiKey) { try { var json = JsonConvert.SerializeObject(body); - var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json"); - stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); + //stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); using (var client = new HttpClient()) { diff --git a/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs b/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs index 9eed3f11..68a3b076 100644 --- a/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs +++ b/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs @@ -7728,7 +7728,7 @@ namespace BMA.EHR.Application.Repositories if (period != null && period.InsigniaEmployees != null) { allEmployeeProfileByRoot = - (await _userProfileRepository.GetEmployeeProfileByPositionAsync(ocId, period.InsigniaEmployees.Select(x => x.RefId), AccessToken)); + (await _userProfileRepository.GetEmployeeProfileByPositionAsync(ocId, period.InsigniaEmployees.Select(x => x.RefId!.ValueOrBlank()).ToArray(), AccessToken)); } var type_coin = allOfficerProfilesByRoot.Count() > 0 ? await GetCoinCandidate(periodId, ocId, allOfficerProfilesByRoot) : new List(); diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index 5592a6aa..4fbd0128 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System.Reflection.Emit; using System.Security.Cryptography; +using System.Text; namespace BMA.EHR.Application.Repositories { @@ -130,7 +131,7 @@ namespace BMA.EHR.Application.Repositories } } - public async Task> GetEmployeeProfileByPositionAsync(Guid rootId, dynamic empPosId, string? accessToken) + public async Task> GetEmployeeProfileByPositionAsync(Guid rootId, string[] empPosId, string? accessToken) { try { @@ -143,6 +144,11 @@ namespace BMA.EHR.Application.Repositories empPosId }; + //var bodyJson = JsonConvert.SerializeObject(bodyRaw); + + // สร้าง HTTP content + //var body = new StringContent(bodyJson, Encoding.UTF8, "application/json"); + var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey); if (apiResult != null) diff --git a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs index 56b05c71..483228b4 100644 --- a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs +++ b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs @@ -415,7 +415,7 @@ namespace BMA.EHR.Insignia.Service.Controllers if (selectPeriod != null && selectPeriod.InsigniaEmployees != null) { var emp = - await _userProfileRepository.GetEmployeeProfileByPositionAsync(organization.Id, selectPeriod.InsigniaEmployees.Select(x => x.RefId), AccessToken); + await _userProfileRepository.GetEmployeeProfileByPositionAsync(organization.Id, selectPeriod.InsigniaEmployees.Select(x => x.RefId!.ValueOrBlank()).ToArray(), AccessToken); if (emp != null) allEmployeeProfileByRoot.AddRange(emp); }