From 0043097b2d744314de4ed11a400c42bdf18e1ee9 Mon Sep 17 00:00:00 2001 From: harid Date: Wed, 17 Dec 2025 14:34:53 +0700 Subject: [PATCH] call api check citizen --- Services/CandidateService.cs | 41 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/Services/CandidateService.cs b/Services/CandidateService.cs index 168755d..8fe7f31 100644 --- a/Services/CandidateService.cs +++ b/Services/CandidateService.cs @@ -8,6 +8,9 @@ using BMA.EHR.Recurit.Exam.Service.Response; using BMA.EHR.Recurit.Exam.Service.Responses.Document; using Microsoft.EntityFrameworkCore; using System.Linq; +using Newtonsoft.Json; +using System.Net.Http.Headers; +using Newtonsoft.Json.Linq; namespace BMA.EHR.Recurit.Exam.Service.Services { @@ -20,6 +23,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services private readonly IHttpContextAccessor _httpContextAccessor; private readonly MinIOService _minioService; private readonly MailService _mailService; + private readonly IConfiguration _configuration; #endregion @@ -29,13 +33,15 @@ namespace BMA.EHR.Recurit.Exam.Service.Services OrgDbContext contextOrg, IHttpContextAccessor httpContextAccessor, MinIOService minioService, - MailService mailService) + MailService mailService, + IConfiguration configuration) { _context = context; _contextOrg = contextOrg; _httpContextAccessor = httpContextAccessor; _minioService = minioService; _mailService = mailService; + _configuration = configuration; } #endregion @@ -43,8 +49,8 @@ namespace BMA.EHR.Recurit.Exam.Service.Services #region " Properties " private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; - private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; + private string? token => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"]; #endregion @@ -2601,6 +2607,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); + #region check digit (old) // if (citizenId.Length != 13) // throw new Exception(GlobalMessages.CitizenIncomplete); @@ -2629,6 +2636,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services // if (citizenIdDigits[12] != chkDigit) // throw new Exception(GlobalMessages.CitizenIncorrect); + #endregion if (positionId != "00000000-0000-0000-0000-000000000000") { @@ -2638,12 +2646,27 @@ namespace BMA.EHR.Recurit.Exam.Service.Services if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); - var candidate1 = await _context.Candidates.AsQueryable() - .Where(x => x.PeriodExam == exam && x.UserId != UserId /*&& x.PositionExam == position*/ && x.CitizenId == citizenId) - .FirstOrDefaultAsync(); - if (candidate1 != null) - throw new Exception(GlobalMessages.CitizanDupicate); - return; + //var candidate1 = await _context.Candidates.AsQueryable() + // .Where(x => x.PeriodExam == exam && x.UserId != UserId /*&& x.PositionExam == position*/ && x.CitizenId == citizenId) + // .FirstOrDefaultAsync(); + //if (candidate1 != null) + // throw new Exception(GlobalMessages.CitizanDupicate); + //return; + } + + var apiUrl = $"{_configuration["API"]}/org/dotnet/check-citizen"; + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); + client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); + var _res = await client.PostAsJsonAsync(apiUrl, new + { + citizenId + }); + var _result = await _res.Content.ReadAsStringAsync(); + var jsonData = JsonConvert.DeserializeObject(_result); + if (!_res.IsSuccessStatusCode) + throw new Exception(jsonData?["message"]?.ToString()); } var candidate2 = await _context.Candidates.AsQueryable() @@ -2651,6 +2674,8 @@ namespace BMA.EHR.Recurit.Exam.Service.Services .FirstOrDefaultAsync(); if (candidate2 != null) throw new Exception(GlobalMessages.CitizanDupicate); + + return; } public async Task GetExamCandidateAsync(Guid id)