Skip to content

Commit

Permalink
Merge pull request #1520 from internetee/344-store-request-for-direct…
Browse files Browse the repository at this point in the history
…o-credit-invoices

Save request data to the directo object
  • Loading branch information
vohmar authored Feb 12, 2020
2 parents 0573773 + d2bc6cd commit fdd427a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 5 additions & 3 deletions app/models/directo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/models/directo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 version="1.0" encoding="UTF-8"?>
<results>
<Result Type="0" Desc="OK" docid="1" doctype="ARVE" submit="Invoices"/>
</results>
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

0 comments on commit fdd427a

Please sign in to comment.