From 43a88cce2787175238e99c4bf7f7cd177b571cae Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 23 Jan 2025 21:45:29 -0300 Subject: [PATCH] finalizando --- .../Controllers/CnpjConsultaController.cs | 28 +++ OpenAdm.Api/Program.cs | 5 +- .../Dtos/Usuarios/CreateUsuarioDto.cs | 29 +-- .../Interfaces/ICnpjConsultaService.cs | 8 + .../Services/CnpjConsultaService.cs | 26 +++ OpenAdm.Infra/Enums/HttpServiceEnum.cs | 6 + .../Interfaces/ICnpjHttpService.cs | 8 + .../HttpService/Services/CnpjHttpService.cs | 45 ++++ .../Model/ConsultaCnpjErroResponse.cs | 6 + OpenAdm.Infra/Model/ConsultaCnpjResponse.cs | 204 ++++++++++++++++++ OpenAdm.IoC/DependencyInjectIHttpClient.cs | 13 +- OpenAdm.IoC/DependencyInjectyApplication.cs | 1 + 12 files changed, 364 insertions(+), 15 deletions(-) create mode 100644 OpenAdm.Api/Controllers/CnpjConsultaController.cs create mode 100644 OpenAdm.Application/Interfaces/ICnpjConsultaService.cs create mode 100644 OpenAdm.Application/Services/CnpjConsultaService.cs create mode 100644 OpenAdm.Infra/Enums/HttpServiceEnum.cs create mode 100644 OpenAdm.Infra/HttpService/Interfaces/ICnpjHttpService.cs create mode 100644 OpenAdm.Infra/HttpService/Services/CnpjHttpService.cs create mode 100644 OpenAdm.Infra/Model/ConsultaCnpjErroResponse.cs create mode 100644 OpenAdm.Infra/Model/ConsultaCnpjResponse.cs diff --git a/OpenAdm.Api/Controllers/CnpjConsultaController.cs b/OpenAdm.Api/Controllers/CnpjConsultaController.cs new file mode 100644 index 0000000..df59f28 --- /dev/null +++ b/OpenAdm.Api/Controllers/CnpjConsultaController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using OpenAdm.Application.Dtos.Response; +using OpenAdm.Application.Interfaces; +using OpenAdm.Application.Models.Fretes; +using OpenAdm.Infra.Model; + +namespace OpenAdm.Api.Controllers; + +[ApiController] +[Route("cnpj")] +public class CnpjConsultaController : ControllerBase +{ + private readonly ICnpjConsultaService _cnpjConsultaService; + + public CnpjConsultaController(ICnpjConsultaService cnpjConsultaService) + { + _cnpjConsultaService = cnpjConsultaService; + } + + [HttpGet("consulta")] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + public async Task Consulta([FromQuery] string cnpj) + { + var response = await _cnpjConsultaService.ConsultaCnpjAsync(cnpj); + return Ok(response); + } +} diff --git a/OpenAdm.Api/Program.cs b/OpenAdm.Api/Program.cs index dd920d2..645ea58 100644 --- a/OpenAdm.Api/Program.cs +++ b/OpenAdm.Api/Program.cs @@ -29,6 +29,7 @@ var email = VariaveisDeAmbiente.GetVariavel("EMAIL"); var servidor = VariaveisDeAmbiente.GetVariavel("SERVER"); var senha = VariaveisDeAmbiente.GetVariavel("SENHA"); +var urlConsultaCnpj = VariaveisDeAmbiente.GetVariavel("ULR_CONSULTA_CNPJ"); var porta = int.Parse(VariaveisDeAmbiente.GetVariavel("PORT")); ConfiguracaoDeToken.Configure(keyJwt, issue, audience, expirate); @@ -45,13 +46,13 @@ builder.Services.InjectJwt(keyJwt, issue, audience); builder.Services.InjectContext(pgString); builder.Services.InjectRepositories(redisString); -builder.Services.InjectHttpClient(urlDiscord, urlApiCep, urlApiMercadoPago); +builder.Services.InjectHttpClient(urlDiscord, urlApiCep, urlApiMercadoPago, urlConsultaCnpj); QuestPDF.Settings.License = LicenseType.Community; var app = builder.Build(); -var basePath = "/api/v1"; +var basePath = "/api"; app.UsePathBase(new PathString(basePath)); app.UseRouting(); diff --git a/OpenAdm.Application/Dtos/Usuarios/CreateUsuarioDto.cs b/OpenAdm.Application/Dtos/Usuarios/CreateUsuarioDto.cs index 02be4be..9ddb403 100644 --- a/OpenAdm.Application/Dtos/Usuarios/CreateUsuarioDto.cs +++ b/OpenAdm.Application/Dtos/Usuarios/CreateUsuarioDto.cs @@ -16,13 +16,18 @@ public class CreateUsuarioDto : BaseModel public string Telefone { get; set; } = string.Empty; public string? Cnpj { get; set; } = string.Empty; public string? Cpf { get; set; } = string.Empty; + public TipoPessoa TipoPessoa { get; set; } public void Validar() { - if ((string.IsNullOrWhiteSpace(Cpf) && string.IsNullOrWhiteSpace(Cnpj)) || - !string.IsNullOrWhiteSpace(Cpf) && !string.IsNullOrWhiteSpace(Cnpj)) + if (TipoPessoa == TipoPessoa.Juridica && string.IsNullOrWhiteSpace(Cnpj)) { - throw new ExceptionApi("Informe o CPF ou o CNPJ"); + throw new ExceptionApi("Informe o CNPJ"); + } + + if (TipoPessoa == TipoPessoa.Fisica && string.IsNullOrWhiteSpace(Cpf)) + { + throw new ExceptionApi("Informe o CPF"); } if (string.IsNullOrWhiteSpace(Nome)) @@ -45,7 +50,7 @@ public void Validar() throw new ExceptionApi("As senha não conferem!"); } - if (!string.IsNullOrWhiteSpace(Cpf) && !ValidarCnpjECpf.IsCpf(Cpf)) + if (!string.IsNullOrWhiteSpace(Cpf) && !ValidarCnpjECpf.IsCpf(Cpf) && TipoPessoa == TipoPessoa.Fisica) { throw new ExceptionApi("CPF inválido!"); } @@ -56,12 +61,6 @@ public void Validar() public Usuario ToEntity() { - if ((string.IsNullOrWhiteSpace(Cnpj) && string.IsNullOrWhiteSpace(Cpf)) - || (!string.IsNullOrWhiteSpace(Cnpj) && !string.IsNullOrWhiteSpace(Cpf))) - { - throw new ExceptionApi("Informe o CNPJ ou o CPF"); - } - var senha = PasswordAdapter.GenerateHash(Senha); var date = DateTime.Now; @@ -74,8 +73,14 @@ public Usuario ToEntity() senha, Nome, Telefone, - string.IsNullOrWhiteSpace(Cnpj) ? null : Cnpj, - string.IsNullOrWhiteSpace(Cpf) ? null : Cpf, + TipoPessoa == TipoPessoa.Juridica ? Cnpj : null, + TipoPessoa == TipoPessoa.Fisica ? Cpf : null, true); } } + +public enum TipoPessoa +{ + Juridica = 1, + Fisica = 2 +} diff --git a/OpenAdm.Application/Interfaces/ICnpjConsultaService.cs b/OpenAdm.Application/Interfaces/ICnpjConsultaService.cs new file mode 100644 index 0000000..0378aaf --- /dev/null +++ b/OpenAdm.Application/Interfaces/ICnpjConsultaService.cs @@ -0,0 +1,8 @@ +using OpenAdm.Infra.Model; + +namespace OpenAdm.Application.Interfaces; + +public interface ICnpjConsultaService +{ + Task ConsultaCnpjAsync(string cnpj); +} diff --git a/OpenAdm.Application/Services/CnpjConsultaService.cs b/OpenAdm.Application/Services/CnpjConsultaService.cs new file mode 100644 index 0000000..f2bd56c --- /dev/null +++ b/OpenAdm.Application/Services/CnpjConsultaService.cs @@ -0,0 +1,26 @@ +using OpenAdm.Application.Interfaces; +using OpenAdm.Domain.Exceptions; +using OpenAdm.Infra.HttpService.Interfaces; +using OpenAdm.Infra.Model; + +namespace OpenAdm.Application.Services; + +public class CnpjConsultaService : ICnpjConsultaService +{ + private readonly ICnpjHttpService _cnpjHttpService; + + public CnpjConsultaService(ICnpjHttpService cnpjHttpService) + { + _cnpjHttpService = cnpjHttpService; + } + + public async Task ConsultaCnpjAsync(string cnpj) + { + if (string.IsNullOrWhiteSpace(cnpj)) + { + throw new ExceptionApi("CNPJ inválido"); + } + + return await _cnpjHttpService.ConsultarCnpjAsync(cnpj); + } +} diff --git a/OpenAdm.Infra/Enums/HttpServiceEnum.cs b/OpenAdm.Infra/Enums/HttpServiceEnum.cs new file mode 100644 index 0000000..93babd2 --- /dev/null +++ b/OpenAdm.Infra/Enums/HttpServiceEnum.cs @@ -0,0 +1,6 @@ +namespace OpenAdm.Infra.Enums; + +public enum HttpServiceEnum +{ + ConsultaCnpj +} diff --git a/OpenAdm.Infra/HttpService/Interfaces/ICnpjHttpService.cs b/OpenAdm.Infra/HttpService/Interfaces/ICnpjHttpService.cs new file mode 100644 index 0000000..afe846e --- /dev/null +++ b/OpenAdm.Infra/HttpService/Interfaces/ICnpjHttpService.cs @@ -0,0 +1,8 @@ +using OpenAdm.Infra.Model; + +namespace OpenAdm.Infra.HttpService.Interfaces; + +public interface ICnpjHttpService +{ + Task ConsultarCnpjAsync(string cnpj); +} diff --git a/OpenAdm.Infra/HttpService/Services/CnpjHttpService.cs b/OpenAdm.Infra/HttpService/Services/CnpjHttpService.cs new file mode 100644 index 0000000..2067fed --- /dev/null +++ b/OpenAdm.Infra/HttpService/Services/CnpjHttpService.cs @@ -0,0 +1,45 @@ +using System.Text.Json; +using OpenAdm.Domain.Exceptions; +using OpenAdm.Infra.Enums; +using OpenAdm.Infra.HttpService.Interfaces; +using OpenAdm.Infra.Model; + +namespace OpenAdm.Infra.HttpService.Services; + +public class CnpjHttpService : ICnpjHttpService +{ + private readonly IHttpClientFactory _httpClientFactory; + + public CnpjHttpService(IHttpClientFactory httpClientFactory) + { + _httpClientFactory = httpClientFactory; + } + + public async Task ConsultarCnpjAsync(string cnpj) + { + var client = _httpClientFactory.CreateClient(HttpServiceEnum.ConsultaCnpj.ToString()); + + var response = await client.GetAsync(cnpj); + var body = await response.Content.ReadAsStreamAsync(); + + if (!response.IsSuccessStatusCode) + { + if (body == null || body.Length == 0) + { + throw new ExceptionApi("Não foi possível obter a resposta da consulta do seu CNPJ"); + } + + var erro = JsonSerializer.Deserialize(body, JsonSerializerOptionsApi.Options()); + + if (!string.IsNullOrWhiteSpace(erro?.Message)) + { + throw new ExceptionApi(erro.Message); + } + + throw new ExceptionApi("Não foi possível efetuar a consulta do seu CNPJ"); + } + + return JsonSerializer.Deserialize(body, JsonSerializerOptionsApi.Options()) + ?? throw new ExceptionApi("Não foi possível efetuar a consulta do seu CNPJ!"); ; + } +} diff --git a/OpenAdm.Infra/Model/ConsultaCnpjErroResponse.cs b/OpenAdm.Infra/Model/ConsultaCnpjErroResponse.cs new file mode 100644 index 0000000..3f1bdb5 --- /dev/null +++ b/OpenAdm.Infra/Model/ConsultaCnpjErroResponse.cs @@ -0,0 +1,6 @@ +namespace OpenAdm.Infra.Model; + +public class ConsultaCnpjErroResponse +{ + public string Message { get; set; } = string.Empty; +} diff --git a/OpenAdm.Infra/Model/ConsultaCnpjResponse.cs b/OpenAdm.Infra/Model/ConsultaCnpjResponse.cs new file mode 100644 index 0000000..f38a652 --- /dev/null +++ b/OpenAdm.Infra/Model/ConsultaCnpjResponse.cs @@ -0,0 +1,204 @@ +namespace OpenAdm.Infra.Model; + +using System; +using System.Collections.Generic; +using System.Text.Json.Serialization; + +public class ConsultaCnpjResponse +{ + [JsonPropertyName("cnpj")] + public string Cnpj { get; set; } = string.Empty; + + [JsonPropertyName("identificador_matriz_filial")] + public int IdentificadorMatrizFilial { get; set; } + + [JsonPropertyName("descricao_identificador_matriz_filial")] + public string DescricaoIdentificadorMatrizFilial { get; set; } = string.Empty; + + [JsonPropertyName("nome_fantasia")] + public string NomeFantasia { get; set; } = string.Empty; + + [JsonPropertyName("situacao_cadastral")] + public int SituacaoCadastral { get; set; } + + [JsonPropertyName("descricao_situacao_cadastral")] + public string DescricaoSituacaoCadastral { get; set; } = string.Empty; + + [JsonPropertyName("data_situacao_cadastral")] + public DateTime? DataSituacaoCadastral { get; set; } + + [JsonPropertyName("motivo_situacao_cadastral")] + public int MotivoSituacaoCadastral { get; set; } + + [JsonPropertyName("descricao_motivo_situacao_cadastral")] + public string DescricaoMotivoSituacaoCadastral { get; set; } = string.Empty; + + [JsonPropertyName("nome_cidade_no_exterior")] + public string NomeCidadeNoExterior { get; set; } = string.Empty; + + [JsonPropertyName("codigo_pais")] + public int? CodigoPais { get; set; } + + [JsonPropertyName("pais")] + public string Pais { get; set; } = string.Empty; + + [JsonPropertyName("data_inicio_atividade")] + public DateTime? DataInicioAtividade { get; set; } + + [JsonPropertyName("cnae_fiscal")] + public long CnaeFiscal { get; set; } + + [JsonPropertyName("cnae_fiscal_descricao")] + public string CnaeFiscalDescricao { get; set; } = string.Empty; + + [JsonPropertyName("descricao_tipo_de_logradouro")] + public string DescricaoTipoDeLogradouro { get; set; } = string.Empty; + + [JsonPropertyName("logradouro")] + public string Logradouro { get; set; } = string.Empty; + + [JsonPropertyName("numero")] + public string Numero { get; set; } = string.Empty; + + [JsonPropertyName("complemento")] + public string Complemento { get; set; } = string.Empty; + + [JsonPropertyName("bairro")] + public string Bairro { get; set; } = string.Empty; + + [JsonPropertyName("cep")] + public string Cep { get; set; } = string.Empty; + + [JsonPropertyName("uf")] + public string Uf { get; set; } = string.Empty; + + [JsonPropertyName("codigo_municipio")] + public int CodigoMunicipio { get; set; } + + [JsonPropertyName("codigo_municipio_ibge")] + public long CodigoMunicipioIbge { get; set; } + + [JsonPropertyName("municipio")] + public string Municipio { get; set; } = string.Empty; + + [JsonPropertyName("ddd_telefone_1")] + public string DddTelefone1 { get; set; } = string.Empty; + + [JsonPropertyName("ddd_telefone_2")] + public string DddTelefone2 { get; set; } = string.Empty; + + [JsonPropertyName("ddd_fax")] + public string DddFax { get; set; } = string.Empty; + + [JsonPropertyName("situacao_especial")] + public string SituacaoEspecial { get; set; } = string.Empty; + + [JsonPropertyName("data_situacao_especial")] + public DateTime? DataSituacaoEspecial { get; set; } + + [JsonPropertyName("opcao_pelo_simples")] + public bool? OpcaoPeloSimples { get; set; } + + [JsonPropertyName("data_opcao_pelo_simples")] + public DateTime? DataOpcaoPeloSimples { get; set; } + + [JsonPropertyName("data_exclusao_do_simples")] + public DateTime? DataExclusaoDoSimples { get; set; } + + [JsonPropertyName("opcao_pelo_mei")] + public bool? OpcaoPeloMei { get; set; } + + [JsonPropertyName("data_opcao_pelo_mei")] + public DateTime? DataOpcaoPeloMei { get; set; } + + [JsonPropertyName("data_exclusao_do_mei")] + public DateTime? DataExclusaoDoMei { get; set; } + + [JsonPropertyName("razao_social")] + public string RazaoSocial { get; set; } = string.Empty; + + [JsonPropertyName("codigo_natureza_juridica")] + public int CodigoNaturezaJuridica { get; set; } + + [JsonPropertyName("natureza_juridica")] + public string NaturezaJuridica { get; set; } = string.Empty; + + [JsonPropertyName("qualificacao_do_responsavel")] + public int QualificacaoDoResponsavel { get; set; } + + [JsonPropertyName("capital_social")] + public decimal CapitalSocial { get; set; } + + [JsonPropertyName("codigo_porte")] + public int CodigoPorte { get; set; } + + [JsonPropertyName("porte")] + public string Porte { get; set; } = string.Empty; + + [JsonPropertyName("ente_federativo_responsavel")] + public string EnteFederativoResponsavel { get; set; } = string.Empty; + + [JsonPropertyName("descricao_porte")] + public string DescricaoPorte { get; set; } = string.Empty; + + [JsonPropertyName("qsa")] + public IList Qsa { get; set; } = []; + + [JsonPropertyName("cnaes_secundarios")] + public IList CnaesSecundarios { get; set; } = []; +} + +public class Qsa +{ + [JsonPropertyName("identificador_de_socio")] + public int IdentificadorDeSocio { get; set; } + + [JsonPropertyName("nome_socio")] + public string NomeSocio { get; set; } = string.Empty; + + [JsonPropertyName("cnpj_cpf_do_socio")] + public string CnpjCpfDoSocio { get; set; } = string.Empty; + + [JsonPropertyName("codigo_qualificacao_socio")] + public int CodigoQualificacaoSocio { get; set; } + + [JsonPropertyName("qualificacao_socio")] + public string QualificacaoSocio { get; set; } = string.Empty; + + [JsonPropertyName("data_entrada_sociedade")] + public DateTime? DataEntradaSociedade { get; set; } + + [JsonPropertyName("codigo_pais")] + public int? CodigoPais { get; set; } + + [JsonPropertyName("pais")] + public string Pais { get; set; } = string.Empty; + + [JsonPropertyName("cpf_representante_legal")] + public string CpfRepresentanteLegal { get; set; } = string.Empty; + + [JsonPropertyName("nome_representante_legal")] + public string NomeRepresentanteLegal { get; set; } = string.Empty; + + [JsonPropertyName("codigo_qualificacao_representante_legal")] + public int CodigoQualificacaoRepresentanteLegal { get; set; } + + [JsonPropertyName("qualificacao_representante_legal")] + public string QualificacaoRepresentanteLegal { get; set; } = string.Empty; + + [JsonPropertyName("codigo_faixa_etaria")] + public int CodigoFaixaEtaria { get; set; } + + [JsonPropertyName("faixa_etaria")] + public string FaixaEtaria { get; set; } = string.Empty; +} + +public class CnaeSecundario +{ + [JsonPropertyName("codigo")] + public long Codigo { get; set; } + + [JsonPropertyName("descricao")] + public string Descricao { get; set; } = string.Empty; +} + diff --git a/OpenAdm.IoC/DependencyInjectIHttpClient.cs b/OpenAdm.IoC/DependencyInjectIHttpClient.cs index 5b86756..bc4aa1e 100644 --- a/OpenAdm.IoC/DependencyInjectIHttpClient.cs +++ b/OpenAdm.IoC/DependencyInjectIHttpClient.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using OpenAdm.Infra.Enums; using OpenAdm.Infra.HttpService.Interfaces; using OpenAdm.Infra.HttpService.Services; @@ -6,11 +7,16 @@ namespace OpenAdm.IoC; public static class DependencyInjectIHttpClient { - public static void InjectHttpClient(this IServiceCollection services, string url, string urlApiCep, string urlMercadoPago) + public static void InjectHttpClient(this IServiceCollection services, + string url, + string urlApiCep, + string urlMercadoPago, + string urlConsultaCnpj) { services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddHttpClient("Discord", x => { x.BaseAddress = new Uri(url); @@ -25,5 +31,10 @@ public static void InjectHttpClient(this IServiceCollection services, string url { x.BaseAddress = new Uri(urlApiCep); }); + + services.AddHttpClient(HttpServiceEnum.ConsultaCnpj.ToString(), x => + { + x.BaseAddress = new Uri(urlConsultaCnpj); + }); } } diff --git a/OpenAdm.IoC/DependencyInjectyApplication.cs b/OpenAdm.IoC/DependencyInjectyApplication.cs index d3c3a92..7c34721 100644 --- a/OpenAdm.IoC/DependencyInjectyApplication.cs +++ b/OpenAdm.IoC/DependencyInjectyApplication.cs @@ -13,6 +13,7 @@ public static class DependencyInjectyApplication public static void InjectServices(this IServiceCollection services) { services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped();