diff --git a/CompanyManagment.Application/TemporaryClientRegistrationApplication.cs b/CompanyManagment.Application/TemporaryClientRegistrationApplication.cs index bd56802f..90159c5d 100644 --- a/CompanyManagment.Application/TemporaryClientRegistrationApplication.cs +++ b/CompanyManagment.Application/TemporaryClientRegistrationApplication.cs @@ -119,14 +119,6 @@ public class TemporaryClientRegistrationApplication : ITemporaryClientRegistrati //دریافت اطلاعات احراز هویت var apiResponsParty = await _uidService.GetPersonalInfo(nationalCode, dateOfBirth); - - //چک کردن مطابقت شماره همراه و کد ملی - var isMachMobilAndNationalCode = await _uidService.IsMachPhoneWithNationalCode(nationalCode, mobile); - if (isMachMobilAndNationalCode == null) - return op.Failed("خطا در سرویس احراز هویت"); - if (!isMachMobilAndNationalCode.IsMatched) - return op.Failed("شماره همراه وارد شده با کد ملی مطابقت ندارد"); - if (apiResponsParty == null) throw new InternalServerException("خطا در سرویس احراز هویت"); @@ -136,10 +128,20 @@ public class TemporaryClientRegistrationApplication : ITemporaryClientRegistrati if (apiResponsParty.ResponseContext.Status.Code != 0) return op.Failed($"{apiResponsParty.ResponseContext.Status.Message}"); - idNumberParty = apiResponsParty.IdentificationInformation.ShenasnamehNumber == "0" + idNumberParty = apiResponsParty.IdentificationInformation.ShenasnamehNumber == "0" ? apiResponsParty.IdentificationInformation.NationalId : apiResponsParty.IdentificationInformation.ShenasnamehNumber; + + + //چک کردن مطابقت شماره همراه و کد ملی + var isMachMobilAndNationalCode = await _uidService.IsMachPhoneWithNationalCode(nationalCode, mobile); + + if (isMachMobilAndNationalCode == null) + throw new InternalServerException("خطا در سرویس تطابق کد ملی و شماره همراه"); + if (!isMachMobilAndNationalCode.IsMatched) + return op.Failed("شماره همراه وارد شده با کد ملی مطابقت ندارد"); + contractingParty.Authentication(apiResponsParty.BasicInformation.FirstName, apiResponsParty.BasicInformation.LastName, apiResponsParty.BasicInformation.FatherName,idNumberParty,apiResponsParty.IdentificationInformation.ShenasnameSeri, apiResponsParty.IdentificationInformation.ShenasnameSerial,dateOfBirth,apiResponsParty.BasicInformation.GenderEnum, @@ -147,12 +149,11 @@ public class TemporaryClientRegistrationApplication : ITemporaryClientRegistrati await _contractingPartyTempRepository.SaveChangesAsync(); - - } if (contractingParty.Phone != mobile) return op.Failed("شما قبلا با شماره همراه دیگری احراز هویت شده اید"); + result.Id = contractingParty.id; result.FName = contractingParty.FName; result.LName = contractingParty.LName; diff --git a/CompanyManagment.EFCore/Services/UidService.cs b/CompanyManagment.EFCore/Services/UidService.cs index c48d754d..b0b0be93 100644 --- a/CompanyManagment.EFCore/Services/UidService.cs +++ b/CompanyManagment.EFCore/Services/UidService.cs @@ -15,268 +15,272 @@ namespace CompanyManagment.EFCore.Services; public class UidService : IUidService { - private readonly HttpClient _httpClient; - private readonly IAuthorizedPersonApplication _authorizedPersonApplication; - private readonly IAuthorizedBankDetailsApplication _authorizedBankDetailsApplication; - private const string BaseUrl = "https://json-api.uid.ir/api/"; - - public const string BusinessToken = "5e03dd4e-999d-466f-92d8-7c0b1f66a8e9"; + private readonly HttpClient _httpClient; + private readonly IAuthorizedPersonApplication _authorizedPersonApplication; + private readonly IAuthorizedBankDetailsApplication _authorizedBankDetailsApplication; + private const string BaseUrl = "https://json-api.uid.ir/api/"; + + public const string BusinessToken = "5e03dd4e-999d-466f-92d8-7c0b1f66a8e9"; public const string BusinessId = "98ed67ca-d441-4978-a748-e8bebce010eb"; - - public UidService(IAuthorizedPersonApplication authorizedPersonApplication, IAuthorizedBankDetailsApplication authorizedBankDetailsApplication) - { - _httpClient = new HttpClient() - { - BaseAddress = new Uri(BaseUrl), - Timeout = new TimeSpan(0, 0, 12) - }; - _authorizedPersonApplication = authorizedPersonApplication; - _authorizedBankDetailsApplication = authorizedBankDetailsApplication; - } - public async Task GetPersonalInfo(string nationalCode, string birthDate) - { - // First check if person exists in database - var existingPerson = _authorizedPersonApplication.GetByNationalCode(nationalCode); - if (existingPerson != null) - { - if (birthDate !=existingPerson.BirthDate ) - { - return new PersonalInfoResponse(new UidBasicInformation(), - new IdentificationInformation(default, default, default, default, default), new RegistrationStatus(), - new ResponseContext(new UidStatus(13, "تاریخ تولد وارد با کد ملی تطابق ندارد"))); - } - // Return data from database instead of calling API - return CreatePersonalInfoResponseFromDatabase(existingPerson); - } + public UidService(IAuthorizedPersonApplication authorizedPersonApplication, + IAuthorizedBankDetailsApplication authorizedBankDetailsApplication) + { + _httpClient = new HttpClient() + { + BaseAddress = new Uri(BaseUrl), + Timeout = new TimeSpan(0, 0, 20) + }; + _authorizedPersonApplication = authorizedPersonApplication; + _authorizedBankDetailsApplication = authorizedBankDetailsApplication; + } - // If not found in database, call UID API - var request = new PersonalInfoRequest - { - BirthDate = birthDate, - NationalId = nationalCode, - RequestContext = new UidRequestContext() - }; - var json = JsonConvert.SerializeObject(request); - var contentType = new StringContent(json, Encoding.UTF8, "application/json"); + public async Task GetPersonalInfo(string nationalCode, string birthDate) + { + // First check if person exists in database + var existingPerson = _authorizedPersonApplication.GetByNationalCode(nationalCode); + if (existingPerson != null) + { + if (birthDate != existingPerson.BirthDate) + { + return new PersonalInfoResponse(new UidBasicInformation(), + new IdentificationInformation(default, default, default, default, default), + new RegistrationStatus(), + new ResponseContext(new UidStatus(13, "تاریخ تولد وارد با کد ملی تطابق ندارد"))); + } - try - { - var requestResult = await _httpClient.PostAsync("inquiry/person/v2", contentType); - - if (!requestResult.IsSuccessStatusCode) - return null; - var responseResult = await requestResult.Content.ReadFromJsonAsync(); - if (responseResult.BasicInformation != null) - { - responseResult.BasicInformation.FirstName = responseResult.BasicInformation.FirstName?.ToPersian(); - responseResult.BasicInformation.LastName = responseResult.BasicInformation.LastName?.ToPersian(); - responseResult.BasicInformation.FatherName = responseResult.BasicInformation.FatherName?.ToPersian(); - } + // Return data from database instead of calling API + return CreatePersonalInfoResponseFromDatabase(existingPerson); + } - // ذخیره اطلاعات در جدول AuthorizedPerson - await SaveAuthorizedPersonData(responseResult, nationalCode, birthDate); + // If not found in database, call UID API + var request = new PersonalInfoRequest + { + BirthDate = birthDate, + NationalId = nationalCode, + RequestContext = new UidRequestContext() + }; + var json = JsonConvert.SerializeObject(request); + var contentType = new StringContent(json, Encoding.UTF8, "application/json"); - return responseResult; - } - catch - { - return new PersonalInfoResponse(new UidBasicInformation(), - new IdentificationInformation(default, default, default, default, default), new RegistrationStatus(), - new ResponseContext(new UidStatus(14, ""))); - } - } + try + { + var requestResult = await _httpClient.PostAsync("inquiry/person/v2", contentType); - private PersonalInfoResponse CreatePersonalInfoResponseFromDatabase(AuthorizedPersonViewModel person) - { - var basicInfo = new UidBasicInformation - { - FirstName = person.FirstName, - LastName = person.LastName, - FatherName = person.FatherName, - Gender = person.Gender - }; + if (!requestResult.IsSuccessStatusCode) + return null; + var responseResult = await requestResult.Content.ReadFromJsonAsync(); + if (responseResult.BasicInformation != null) + { + responseResult.BasicInformation.FirstName = responseResult.BasicInformation.FirstName?.ToPersian(); + responseResult.BasicInformation.LastName = responseResult.BasicInformation.LastName?.ToPersian(); + responseResult.BasicInformation.FatherName = responseResult.BasicInformation.FatherName?.ToPersian(); + } - var identificationInfo = new IdentificationInformation( - person.NationalCode, - person.BirthDate, - person.ShenasnameSeri, - person.ShenasnameSerial, - person.ShenasnamehNumber - ); + // ذخیره اطلاعات در جدول AuthorizedPerson + await SaveAuthorizedPersonData(responseResult, nationalCode, birthDate); - var registrationStatus = new RegistrationStatus - { - DeathStatus = person.DeathStatus - }; + return responseResult; + } + catch + { + return new PersonalInfoResponse(new UidBasicInformation(), + new IdentificationInformation(default, default, default, default, default), new RegistrationStatus(), + new ResponseContext(new UidStatus(14, ""))); + } + } - var responseContext = new ResponseContext(new UidStatus(0, "Success - از دیتابیس")); + private PersonalInfoResponse CreatePersonalInfoResponseFromDatabase(AuthorizedPersonViewModel person) + { + var basicInfo = new UidBasicInformation + { + FirstName = person.FirstName, + LastName = person.LastName, + FatherName = person.FatherName, + Gender = person.Gender + }; - return new PersonalInfoResponse(basicInfo, identificationInfo, registrationStatus, responseContext); - } + var identificationInfo = new IdentificationInformation( + person.NationalCode, + person.BirthDate, + person.ShenasnameSeri, + person.ShenasnameSerial, + person.ShenasnamehNumber + ); - private async Task SaveAuthorizedPersonData(PersonalInfoResponse response, string nationalCode, string birthDate) - { - if (response?.BasicInformation == null || response.ResponseContext?.Status?.Code != 0) - return; + var registrationStatus = new RegistrationStatus + { + DeathStatus = person.DeathStatus + }; - var command = new CreateAuthorizedPerson - { - NationalCode = nationalCode, - FirstName = response.BasicInformation.FirstName ?? "", - LastName = response.BasicInformation.LastName ?? "", - FatherName = response.BasicInformation.FatherName ?? "", - BirthDate = birthDate, - Gender = response.BasicInformation.Gender ?? "", - DeathStatus = response.RegistrationStatus?.DeathStatus ?? "", - ShenasnameSeri = response.IdentificationInformation?.ShenasnameSeri ?? "", - ShenasnameSerial = response.IdentificationInformation?.ShenasnameSerial ?? "", - ShenasnamehNumber = response.IdentificationInformation?.ShenasnamehNumber ?? "" - }; + var responseContext = new ResponseContext(new UidStatus(0, "Success - از دیتابیس")); - _authorizedPersonApplication.CreateFromUidResponse(command); - } + return new PersonalInfoResponse(basicInfo, identificationInfo, registrationStatus, responseContext); + } - public async Task IsMachPhoneWithNationalCode(string nationalCode, string phoneNumber) - { - var request = new PersonalInfoRequest - { - MobileNumber = phoneNumber, - NationalId = nationalCode, - RequestContext = new UidRequestContext() - }; - var json = JsonConvert.SerializeObject(request); - var contentType = new StringContent(json, Encoding.UTF8, "application/json"); + private async Task SaveAuthorizedPersonData(PersonalInfoResponse response, string nationalCode, string birthDate) + { + if (response?.BasicInformation == null || response.ResponseContext?.Status?.Code != 0) + return; - var requestResult = await _httpClient.PostAsync("inquiry/mobile/owner/v2", contentType); - if (!requestResult.IsSuccessStatusCode) - return null; + var command = new CreateAuthorizedPerson + { + NationalCode = nationalCode, + FirstName = response.BasicInformation.FirstName ?? "", + LastName = response.BasicInformation.LastName ?? "", + FatherName = response.BasicInformation.FatherName ?? "", + BirthDate = birthDate, + Gender = response.BasicInformation.Gender ?? "", + DeathStatus = response.RegistrationStatus?.DeathStatus ?? "", + ShenasnameSeri = response.IdentificationInformation?.ShenasnameSeri ?? "", + ShenasnameSerial = response.IdentificationInformation?.ShenasnameSerial ?? "", + ShenasnamehNumber = response.IdentificationInformation?.ShenasnamehNumber ?? "" + }; - var responseResult = await requestResult.Content.ReadFromJsonAsync(); - return responseResult; - } + _authorizedPersonApplication.CreateFromUidResponse(command); + } - public async Task IbanInquiry(string iban) - { - // First check if bank details exist in database - var existingBankDetails = _authorizedBankDetailsApplication.GetByIban(iban); - if (existingBankDetails != null) - { - // Return data from database instead of calling API - return CreateIbanInquiryResponseFromDatabase(existingBankDetails); - } + public async Task IsMachPhoneWithNationalCode(string nationalCode, + string phoneNumber) + { + var request = new PersonalInfoRequest + { + MobileNumber = phoneNumber, + NationalId = nationalCode, + RequestContext = new UidRequestContext() + }; + var json = JsonConvert.SerializeObject(request); + var contentType = new StringContent(json, Encoding.UTF8, "application/json"); - // If not found in database, call UID API - var request = new - { - iban, - requestContext = new UidRequestContext() - }; - var json = JsonConvert.SerializeObject(request); - var contentType = new StringContent(json, Encoding.UTF8, "application/json"); - - try - { - var requestResult = await _httpClient.PostAsync("inquiry/iban/v2", contentType); - requestResult.EnsureSuccessStatusCode(); - var responseResult = await requestResult.Content.ReadFromJsonAsync(); - - if (responseResult.ResponseContext.Status.Code == 0) - { - var entity = new CreateAuthorizedBankDetails - { - IBan = iban, - AccountNumber = responseResult?.AccountBasicInformation?.AccountNumber ?? "", - BankName = responseResult?.AccountBasicInformation?.BankInformation?.BankName ?? "", - OwnersList = responseResult?.Owners.Select(x=> new CreateAuthorizedBankDetailsOwner() - { - FName = x.FirstName, - LName = x.LastName, - NationalIdentifier = x.NationalIdentifier, - CustomerType = x.CustomerType - }).ToList() ?? [] - }; - _authorizedBankDetailsApplication.Create(entity); - } - - return responseResult; - } - catch - { - return new IbanInquiryResponse - { - ResponseContext = new ResponseContext(new UidStatus(14, "خطا در دریافت اطلاعات از سرویس")) - }; - } - } + var requestResult = await _httpClient.PostAsync("inquiry/mobile/owner/v2", contentType); + if (!requestResult.IsSuccessStatusCode) + return null; - private IbanInquiryResponse CreateIbanInquiryResponseFromDatabase(AuthorizedBankDetailsViewModel bankDetails) - { - var accountBasicInfo = new IbanInquiryAccountBasicInformation - { - Iban = bankDetails.IBan, - AccountNumber = bankDetails.AccountNumber, - BankInformation = new IbanInquiryBankInformation - { - BankName = bankDetails.BankName - }, - AccountStatus = "Active" - }; + var responseResult = await requestResult.Content.ReadFromJsonAsync(); + return responseResult; + } - var owners = new List(); - - // Add owner information if available - if (bankDetails.Owners.Any()) - { - var owner = bankDetails.Owners.First(); - owners.Add(new IbanInquiryOwner - { - NationalIdentifier = owner.NationalIdentifier, - FirstName = owner.FName, - LastName = owner.LName, - CustomerType = owner.CustomerType - }); - } + public async Task IbanInquiry(string iban) + { + // First check if bank details exist in database + var existingBankDetails = _authorizedBankDetailsApplication.GetByIban(iban); + if (existingBankDetails != null) + { + // Return data from database instead of calling API + return CreateIbanInquiryResponseFromDatabase(existingBankDetails); + } - var responseContext = new ResponseContext(new UidStatus(0, "Success - از دیتابیس")); + // If not found in database, call UID API + var request = new + { + iban, + requestContext = new UidRequestContext() + }; + var json = JsonConvert.SerializeObject(request); + var contentType = new StringContent(json, Encoding.UTF8, "application/json"); - return new IbanInquiryResponse - { - AccountBasicInformation = accountBasicInfo, - Owners = owners, - ResponseContext = responseContext - }; - } + try + { + var requestResult = await _httpClient.PostAsync("inquiry/iban/v2", contentType); + requestResult.EnsureSuccessStatusCode(); + var responseResult = await requestResult.Content.ReadFromJsonAsync(); - public async Task AccountToIban(string accountNumber, UidBanks bank) - { - var request = new - { - accountNumber, - bank, - requestContext = new UidRequestContext() - }; - var json = JsonConvert.SerializeObject(request); - var contentType = new StringContent(json, Encoding.UTF8, "application/json"); - var requestResult = await _httpClient.PostAsync("account-to-iban", contentType); - requestResult.EnsureSuccessStatusCode(); - var responseResult = await requestResult.Content.ReadFromJsonAsync(); - return responseResult; - } + if (responseResult.ResponseContext.Status.Code == 0) + { + var entity = new CreateAuthorizedBankDetails + { + IBan = iban, + AccountNumber = responseResult?.AccountBasicInformation?.AccountNumber ?? "", + BankName = responseResult?.AccountBasicInformation?.BankInformation?.BankName ?? "", + OwnersList = responseResult?.Owners.Select(x => new CreateAuthorizedBankDetailsOwner() + { + FName = x.FirstName, + LName = x.LastName, + NationalIdentifier = x.NationalIdentifier, + CustomerType = x.CustomerType + }).ToList() ?? [] + }; + _authorizedBankDetailsApplication.Create(entity); + } - public async Task CardToIban(string cardNumber) - { - var request = new - { - cardNumber, - requestContext = new UidRequestContext() - }; - var json = JsonConvert.SerializeObject(request); - var contentType = new StringContent(json, Encoding.UTF8, "application/json"); - var requestResult = await _httpClient.PostAsync("inquiry/card", contentType); - requestResult.EnsureSuccessStatusCode(); - var responseResult = await requestResult.Content.ReadFromJsonAsync(); - return responseResult; - } -} + return responseResult; + } + catch + { + return new IbanInquiryResponse + { + ResponseContext = new ResponseContext(new UidStatus(14, "خطا در دریافت اطلاعات از سرویس")) + }; + } + } + + private IbanInquiryResponse CreateIbanInquiryResponseFromDatabase(AuthorizedBankDetailsViewModel bankDetails) + { + var accountBasicInfo = new IbanInquiryAccountBasicInformation + { + Iban = bankDetails.IBan, + AccountNumber = bankDetails.AccountNumber, + BankInformation = new IbanInquiryBankInformation + { + BankName = bankDetails.BankName + }, + AccountStatus = "Active" + }; + + var owners = new List(); + + // Add owner information if available + if (bankDetails.Owners.Any()) + { + var owner = bankDetails.Owners.First(); + owners.Add(new IbanInquiryOwner + { + NationalIdentifier = owner.NationalIdentifier, + FirstName = owner.FName, + LastName = owner.LName, + CustomerType = owner.CustomerType + }); + } + + var responseContext = new ResponseContext(new UidStatus(0, "Success - از دیتابیس")); + + return new IbanInquiryResponse + { + AccountBasicInformation = accountBasicInfo, + Owners = owners, + ResponseContext = responseContext + }; + } + + public async Task AccountToIban(string accountNumber, UidBanks bank) + { + var request = new + { + accountNumber, + bank, + requestContext = new UidRequestContext() + }; + var json = JsonConvert.SerializeObject(request); + var contentType = new StringContent(json, Encoding.UTF8, "application/json"); + var requestResult = await _httpClient.PostAsync("account-to-iban", contentType); + requestResult.EnsureSuccessStatusCode(); + var responseResult = await requestResult.Content.ReadFromJsonAsync(); + return responseResult; + } + + public async Task CardToIban(string cardNumber) + { + var request = new + { + cardNumber, + requestContext = new UidRequestContext() + }; + var json = JsonConvert.SerializeObject(request); + var contentType = new StringContent(json, Encoding.UTF8, "application/json"); + var requestResult = await _httpClient.PostAsync("inquiry/card", contentType); + requestResult.EnsureSuccessStatusCode(); + var responseResult = await requestResult.Content.ReadFromJsonAsync(); + return responseResult; + } +} \ No newline at end of file