Skip to content

Commit

Permalink
Add summary for failed SmallInvoice syncs
Browse files Browse the repository at this point in the history
Improve formatting
  • Loading branch information
Kagemaru committed Jul 8, 2021
1 parent 09d67a4 commit 6b619e0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/domain/invoicing/small_invoice/address_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/domain/invoicing/small_invoice/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions app/domain/invoicing/small_invoice/client_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions app/domain/invoicing/small_invoice/contact_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/domain/invoicing/small_invoice/invoice_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 6b619e0

Please sign in to comment.