mirror of
https://github.com/internetee/registry.git
synced 2025-08-05 09:21:43 +02:00
Add idempotent Que job for e-invoice sending
This commit is contained in:
parent
07653fa710
commit
2663c550fa
3 changed files with 47 additions and 1 deletions
|
@ -0,0 +1,39 @@
|
|||
class SendEInvoiceJob < Que::Job
|
||||
|
||||
def run(invoice)
|
||||
return if invoice.e_invoice_sent_at
|
||||
|
||||
e_invoice = invoice.to_e_invoice
|
||||
e_invoice.deliver
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
invoice.update(e_invoice_sent_at: Time.zone.now)
|
||||
log_success(invoice)
|
||||
destroy
|
||||
end
|
||||
|
||||
rescue Savon::Error => e
|
||||
log_error(invoice: invoice, error: e)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log_success(invoice)
|
||||
message = "E-Invoice for an invoice with ID # #{invoice.id} was sent successfully"
|
||||
logger.info message
|
||||
end
|
||||
|
||||
def log_error(invoice:, error:)
|
||||
message = <<~TEXT.squish
|
||||
There was an error sending e-invoice for invoice with ID # #{invoice.id}.
|
||||
The error message was the following: #{error}.
|
||||
This job will retry
|
||||
TEXT
|
||||
logger.error message
|
||||
end
|
||||
|
||||
def logger
|
||||
Rails.logger
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue