public async Task <NFSEDto> GetNfse(string lote, string nfse) { NFSEDto dtoResponse; using (var httpclient = new HttpClient()) { var request = new HttpRequestMessage(HttpMethod.Get, $"{TecnospeedData.host}/{Constants.ConsultarNotaEndpoint}?grupo={TecnospeedData.grupo}&CNPJ={TecnospeedData.cnpj}&nlote={lote}&nnfse={nfse}"); request.Headers.Add("Authorization", this.authentication.ValidateUser()); var response = await httpclient.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); if (response.StatusCode == System.Net.HttpStatusCode.OK) { dtoResponse = new NFSEDto { //TODO - CONVERT DATA }; } else { throw new Exception($"Erro ao consultar nota: {body}"); } } return(dtoResponse); }
public async Task SaveInformationAsync(NFSEDto data) { await this.UsingDbContext(async dbContext => { try { var result = new Invoice { //TODO - ADICIONAR CAMPOS }; dbContext.Invoices.Add(result); await dbContext.SaveChangesAsync(); } catch (Exception) { //TODO - VALIDAR ERRO throw new Exception("Erro ao salvar nota."); } }); }
public async Task <IActionResult> Post([FromBody] RequestData data) { if (data == null) { return(this.BadRequest("Dados inválidos para geração de nota fiscal.")); } //send data to tecnospeed api var response = await this.nfseHandler.sendData(data); //read response body to get the data the server sent back var responseBody = await response.Content.ReadAsStringAsync(); //get nota on tecnospeed endpoint and save information //handle responsebody if (response.StatusCode != System.Net.HttpStatusCode.OK || responseBody.ToString().Contains("EXCEPTION")) { return(this.StatusCode(400, responseBody)); } var responseBodyArray = responseBody.Split(','); //TODO - SALVAR OS CAMPOS QUANDO HANDLE ESTIVER INFORMADO //TODO - GERAR TASK PARA 2 MINUTOS PARA CONSULTAR NOTA CASO STATUS NAO ESTEJA INFORMADO if (responseBodyArray.Length >= 4) { var handle = responseBodyArray[0]; var lote = responseBodyArray[1]; var numero = responseBodyArray[2]; var status = responseBodyArray[3]; if (status == "") { try { Task.Run(async() => { await Task.Delay(TimeSpan.FromMinutes(2)); var nfse = await this.nfseHandler.GetNfse(lote, numero); await this.nfseRepository.SaveInformationAsync(nfse); }); } catch (Exception e) { } } else { var nfse = new NFSEDto { Handle = handle, Status = status, Lote = lote, Numero = numero }; try { await this.nfseRepository.SaveInformationAsync(nfse); } catch (System.Exception ex) { return(this.StatusCode(400, ex.Message)); } } } //var nfse = await this.nfseHandler.GetNfse(lote, numero); ////save information on database //await this.nfseRepository.SaveInformationAsync(nfse); return(this.Created(this.Url.ToString(), responseBody)); }