diff --git a/app/jobs/send_e_invoice_job.rb b/app/jobs/send_e_invoice_job.rb index e69de29bb..9e8af7c65 100644 --- a/app/jobs/send_e_invoice_job.rb +++ b/app/jobs/send_e_invoice_job.rb @@ -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 diff --git a/db/migrate/20200204103125_add_e_invoice_sent_at_to_invoice.rb b/db/migrate/20200204103125_add_e_invoice_sent_at_to_invoice.rb new file mode 100644 index 000000000..e0e5f2cd0 --- /dev/null +++ b/db/migrate/20200204103125_add_e_invoice_sent_at_to_invoice.rb @@ -0,0 +1,5 @@ +class AddEInvoiceSentAtToInvoice < ActiveRecord::Migration[5.0] + def change + add_column :invoices, :e_invoice_sent_at, :datetime + end +end diff --git a/db/structure.sql b/db/structure.sql index a23623bae..cd2998c07 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -886,6 +886,7 @@ CREATE TABLE public.invoices ( in_directo boolean DEFAULT false, buyer_vat_no character varying, issue_date date NOT NULL, + e_invoice_sent_at timestamp without time zone, CONSTRAINT invoices_due_date_is_not_before_issue_date CHECK ((due_date >= issue_date)) ); @@ -4339,6 +4340,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20191212133136'), ('20191227110904'), ('20200113091254'), -('20200115102202'); +('20200115102202'), +('20200204103125');