Ejemplo n.º 1
0
        public async Task <IActionResult> GetClient(string du, Sex sex, [FromBody] MapOptions mapOptions)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var cuix = String.Empty;

            try
            {
                cuix = await _clientServices.GetCuix(du, sex.ToString());

                _logger.LogInformation("Cuix OK.");
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError, "Error getting CUIX"));
            }

            try
            {
                var dataPadron = await _clientServices.GetClientAfip(cuix);

                _logger.LogInformation("Afip services OK.");

                if (dataPadron == null)
                {
                    return(NotFound());
                }


                await _clientServices.GetClientNV(dataPadron);

                if (!String.IsNullOrEmpty(dataPadron.HostId))
                {
                    var addressNV = await _clientServices.GetAddressNV(dataPadron.HostId);

                    dataPadron.Addresses.Add(addressNV);
                }

                try
                {
                    var normalizedAddresses = new List <Address>();

                    foreach (var item in dataPadron.Addresses)
                    {
                        mapOptions.Address = item;

                        var result = await _clientServices.NormalizeAddress(mapOptions);

                        if (result != null)
                        {
                            normalizedAddresses.Add(result);
                            _logger.LogInformation("Normalize Address OK.");
                        }
                        else
                        {
                            _logger.LogInformation("Address not found.");
                        }
                    }

                    if (normalizedAddresses.Count > 0)
                    {
                        dataPadron.Addresses = normalizedAddresses;
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    _logger.LogTrace("Error normalizing address.");
                }

                return(new ObjectResult(dataPadron));
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError, "Error al consultar los datos padron."));
            }
        }