diff --git a/app/domain/invoicing/small_invoice/address_sync.rb b/app/domain/invoicing/small_invoice/address_sync.rb index 74707f36..0d160d18 100644 --- a/app/domain/invoicing/small_invoice/address_sync.rb +++ b/app/domain/invoicing/small_invoice/address_sync.rb @@ -40,6 +40,7 @@ def record_to_params(record, prefix = 'billing_address') delegate :notify_sync_error, to: 'self.class' attr_reader :client, :remote_keys + class_attribute :rate_limiter self.rate_limiter = RateLimiter.new(Settings.small_invoice.request_rate) @@ -49,13 +50,16 @@ def initialize(client, remote_keys = nil) end def sync + failed = [] ::BillingAddress.includes(:client).where(client_id: client.id).find_each do |billing_address| key(billing_address) ? update_remote(billing_address) : create_remote(billing_address) rescue StandardError => e + failed << billing_address.id Rails.logger.error e.message Rails.logger.error e.backtrace notify_sync_error(e, billing_address) end + Rails.logger.error "Failed Address Syncs: #{failed.inspect}" if failed.any? end private diff --git a/app/domain/invoicing/small_invoice/api.rb b/app/domain/invoicing/small_invoice/api.rb index 484d0714..645b1b88 100644 --- a/app/domain/invoicing/small_invoice/api.rb +++ b/app/domain/invoicing/small_invoice/api.rb @@ -50,7 +50,7 @@ def get_raw(path, auth: true, **params) def access_token # fetch a new token if we have none yet or if the existing one is expired - @access_token, @expires_at = get_access_token unless @expires_at &. > Time.zone.now + @access_token, @expires_at = get_access_token unless @expires_at&.>(Time.zone.now) @access_token end diff --git a/app/domain/invoicing/small_invoice/client_sync.rb b/app/domain/invoicing/small_invoice/client_sync.rb index a9e75d6d..11138e8a 100644 --- a/app/domain/invoicing/small_invoice/client_sync.rb +++ b/app/domain/invoicing/small_invoice/client_sync.rb @@ -10,15 +10,18 @@ class ClientSync class << self def perform remote_keys = fetch_remote_keys + failed = [] ::Client.includes(:work_item, :contacts, :billing_addresses).find_each do |client| if client.billing_addresses.present? # required by small invoice begin new(client, remote_keys).sync rescue => error + failed << client.id notify_sync_error(error, client) end end end + Rails.logger.error "Failed Client Syncs: #{failed.inspect}" if failed.any? end def fetch_remote_keys @@ -60,6 +63,7 @@ def record_to_params(record, prefix = 'client') delegate :notify_sync_error, to: 'self.class' attr_reader :client, :remote_keys + class_attribute :rate_limiter self.rate_limiter = RateLimiter.new(Settings.small_invoice.request_rate) diff --git a/app/domain/invoicing/small_invoice/contact_sync.rb b/app/domain/invoicing/small_invoice/contact_sync.rb index 26f83c89..d8020c10 100644 --- a/app/domain/invoicing/small_invoice/contact_sync.rb +++ b/app/domain/invoicing/small_invoice/contact_sync.rb @@ -40,6 +40,7 @@ def record_to_params(record, prefix = 'billing_address') delegate :notify_sync_error, to: 'self.class' attr_reader :client, :remote_keys + class_attribute :rate_limiter self.rate_limiter = RateLimiter.new(Settings.small_invoice.request_rate) @@ -49,11 +50,14 @@ def initialize(client, remote_keys = nil) end def sync + failed = [] ::Contact.includes(:client).where(client_id: client.id).find_each do |contact| key(contact) ? update_remote(contact) : create_remote(contact) rescue StandardError => e + failed << contact.id notify_sync_error(e, contact) end + Rails.logger.error "Failed Contact Syncs: #{failed.inspect}" if failed.any? end private diff --git a/app/domain/invoicing/small_invoice/invoice_sync.rb b/app/domain/invoicing/small_invoice/invoice_sync.rb index 850f4c39..0ef70b88 100644 --- a/app/domain/invoicing/small_invoice/invoice_sync.rb +++ b/app/domain/invoicing/small_invoice/invoice_sync.rb @@ -25,18 +25,22 @@ class InvoiceSync }.freeze attr_reader :invoice + class_attribute :rate_limiter self.rate_limiter = RateLimiter.new(Settings.small_invoice.request_rate) class << self def sync_unpaid + failed = [] unpaid_invoices.find_each do |invoice| begin new(invoice).sync rescue => error + failed << invoice.id notify_sync_error(error, invoice) end end + Rails.logger.error "Failed Invoice Syncs: #{failed.inspect}" if failed.any? end private diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 094333fa..94cf0017 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -49,12 +49,12 @@ class Invoice < ActiveRecord::Base before_validation :generate_reference, on: :create before_validation :generate_due_date before_validation :update_totals - before_create :lock_client_invoice_number - after_create :update_client_invoice_number - after_save :update_order_billing_address before_save :save_remote_invoice, if: -> { Invoicing.instance.present? } before_save :assign_worktimes + before_create :lock_client_invoice_number + after_create :update_client_invoice_number after_destroy :delete_remote_invoice, if: -> { Invoicing.instance.present? } + after_save :update_order_billing_address protect_if :paid?, 'Bezahlte Rechnungen können nicht gelöscht werden.' protect_if :order_closed?, 'Rechnungen von geschlossenen Aufträgen können nicht gelöscht werden.' @@ -225,7 +225,11 @@ def save_remote_invoice self.invoicing_key = Invoicing.instance.save_invoice(self, positions) rescue Invoicing::Error => e errors.add(:base, "Fehler im Invoicing Service: #{e.message}") - Rails.logger.error(e.class.name + ': ' + e.message + "\n" + e.data.inspect + "\n" + e.backtrace.join("\n")) + Rails.logger.error <<~ERROR + #{e.class.name}: #{e.message} + #{e.data.inspect} + #{e.backtrace.join("\n")} + ERROR throw :abort end