From b7f8d3a0b07dbe0176f5da51c796e3143c8d8bda Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Fri, 7 Feb 2020 16:26:03 +0500 Subject: [PATCH 1/2] Save request data to the directo object Closes #344 --- app/models/directo.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/directo.rb b/app/models/directo.rb index 789db64b2d..99a0d92651 100644 --- a/app/models/directo.rb +++ b/app/models/directo.rb @@ -48,16 +48,18 @@ def self.send_receipts Rails.logger.info("[Directo] XML request: #{data}") response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false) Rails.logger.info("[Directo] Directo responded with code: #{response.code}, body: #{response.body}") - dump_result_to_db(mappers, response.to_s) + dump_result_to_db(mappers: mappers, xml: response.to_s, data: data) end STDOUT << "#{Time.zone.now.utc} - Directo receipts sending finished. #{counter} of #{total} are sent\n" end - def self.dump_result_to_db mappers, xml + def self.dump_result_to_db(mappers:, xml:, data:) Nokogiri::XML(xml).css("Result").each do |res| obj = mappers[res.attributes["docid"].value.to_i] - obj.directo_records.create!(response: res.as_json.to_h, invoice_number: obj.number) + obj.directo_records.create!(request: data, + response: res.as_json.to_h, + invoice_number: obj.number) obj.update_columns(in_directo: true) Rails.logger.info("[DIRECTO] Invoice #{res.attributes["docid"].value} was pushed and return is #{res.as_json.to_h.inspect}") end From d2bc6cdd86527aa94933e6608aa2658e852a40c9 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Fri, 7 Feb 2020 18:34:39 +0500 Subject: [PATCH 2/2] Add tests for storing request in invoice's irecto records --- test/models/directo_test.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/models/directo_test.rb b/test/models/directo_test.rb index 9dbbf64d4d..b1c461eb09 100644 --- a/test/models/directo_test.rb +++ b/test/models/directo_test.rb @@ -9,12 +9,21 @@ def test_xml_is_include_transaction_date @invoice.update(total: @invoice.account_activity.bank_transaction.sum) @invoice.account_activity.bank_transaction.update(paid_at: Time.zone.now) + response = <<-XML + + + + + XML + stub_request(:post, ENV['directo_invoice_url']).with do |request| request.body.include? 'TransactionDate' - end + end.to_return(status: 200, body: response) assert_nothing_raised do Directo.send_receipts end + + assert_not_empty @invoice.directo_records.first.request end end