mirror of
https://github.com/internetee/registry.git
synced 2025-06-08 05:34:46 +02:00
Fix async que job calling
This commit is contained in:
parent
187ce318a2
commit
1d6040cd5b
3 changed files with 17 additions and 11 deletions
|
@ -1,21 +1,26 @@
|
||||||
class SendEInvoiceJob < Que::Job
|
class SendEInvoiceJob < Que::Job
|
||||||
def run(invoice)
|
def run(invoice_id)
|
||||||
destroy if invoice.do_not_send_e_invoice?
|
invoice = run_condition(Invoice.find_by(id: invoice_id))
|
||||||
|
|
||||||
invoice.to_e_invoice.deliver
|
invoice.to_e_invoice.deliver
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
invoice.update(e_invoice_sent_at: Time.zone.now)
|
invoice.update(e_invoice_sent_at: Time.zone.now)
|
||||||
log_success(invoice)
|
log_success(invoice)
|
||||||
destroy
|
destroy
|
||||||
end
|
end
|
||||||
rescue Exception => e
|
rescue StandardError => e
|
||||||
log_error(invoice: invoice, error: e)
|
log_error(invoice: invoice, error: e)
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def run_condition(invoice)
|
||||||
|
destroy unless invoice
|
||||||
|
destroy if invoice.do_not_send_e_invoice?
|
||||||
|
invoice
|
||||||
|
end
|
||||||
|
|
||||||
def log_success(invoice)
|
def log_success(invoice)
|
||||||
id = invoice.try(:id) || invoice
|
id = invoice.try(:id) || invoice
|
||||||
message = "E-Invoice for an invoice with ID # #{id} was sent successfully"
|
message = "E-Invoice for an invoice with ID # #{id} was sent successfully"
|
||||||
|
@ -26,8 +31,8 @@ class SendEInvoiceJob < Que::Job
|
||||||
id = invoice.try(:id) || invoice
|
id = invoice.try(:id) || invoice
|
||||||
message = <<~TEXT.squish
|
message = <<~TEXT.squish
|
||||||
There was an error sending e-invoice for invoice with ID # #{id}.
|
There was an error sending e-invoice for invoice with ID # #{id}.
|
||||||
The error message was the following: #{error}.
|
The error message was the following: #{error}
|
||||||
This job will retry
|
This job will retry.
|
||||||
TEXT
|
TEXT
|
||||||
logger.error message
|
logger.error message
|
||||||
end
|
end
|
||||||
|
|
|
@ -99,8 +99,7 @@ class Registrar < ApplicationRecord
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
SendEInvoiceJob.enqueue(invoice.id)
|
||||||
SendEInvoiceJob.enqueue(invoice)
|
|
||||||
|
|
||||||
invoice
|
invoice
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,8 +13,9 @@ class SendEInvoiceJobTest < ActiveSupport::TestCase
|
||||||
EInvoice::Providers::TestProvider.deliveries.clear
|
EInvoice::Providers::TestProvider.deliveries.clear
|
||||||
|
|
||||||
assert_nothing_raised do
|
assert_nothing_raised do
|
||||||
SendEInvoiceJob.enqueue(@invoice)
|
SendEInvoiceJob.enqueue(@invoice.id)
|
||||||
end
|
end
|
||||||
|
@invoice.reload
|
||||||
|
|
||||||
assert_not @invoice.e_invoice_sent_at.blank?
|
assert_not @invoice.e_invoice_sent_at.blank?
|
||||||
assert_equal 1, EInvoice::Providers::TestProvider.deliveries.count
|
assert_equal 1, EInvoice::Providers::TestProvider.deliveries.count
|
||||||
|
@ -28,7 +29,7 @@ class SendEInvoiceJobTest < ActiveSupport::TestCase
|
||||||
stub_request(:get, "https://testfinance.post.ee/finance/erp/erpServices.wsdl").to_timeout
|
stub_request(:get, "https://testfinance.post.ee/finance/erp/erpServices.wsdl").to_timeout
|
||||||
|
|
||||||
assert_raise HTTPClient::TimeoutError do
|
assert_raise HTTPClient::TimeoutError do
|
||||||
SendEInvoiceJob.enqueue(@invoice)
|
SendEInvoiceJob.enqueue(@invoice.id)
|
||||||
end
|
end
|
||||||
assert @invoicee_invoice_sent_at.blank?
|
assert @invoicee_invoice_sent_at.blank?
|
||||||
|
|
||||||
|
@ -36,8 +37,9 @@ class SendEInvoiceJobTest < ActiveSupport::TestCase
|
||||||
EInvoice::Providers::TestProvider.deliveries.clear
|
EInvoice::Providers::TestProvider.deliveries.clear
|
||||||
|
|
||||||
assert_nothing_raised do
|
assert_nothing_raised do
|
||||||
SendEInvoiceJob.enqueue(@invoice)
|
SendEInvoiceJob.enqueue(@invoice.id)
|
||||||
end
|
end
|
||||||
|
@invoice.reload
|
||||||
|
|
||||||
assert_not @invoice.e_invoice_sent_at.blank?
|
assert_not @invoice.e_invoice_sent_at.blank?
|
||||||
assert_equal 1, EInvoice::Providers::TestProvider.deliveries.count
|
assert_equal 1, EInvoice::Providers::TestProvider.deliveries.count
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue