Skip to content

Commit

Permalink
fix (integration-customers): adapt integration customer queries (#2269)
Browse files Browse the repository at this point in the history
## Context

Improve integration customer queries

## Description

Currently Accounting integrations, Netsuite and Xero, are supported in
Lago but tax integration Anrok is currently being implemented.

This PR improves existing integration customers queries. Customer can
have attached ONLY one accounting integration and ONLY one tax
integration, but we want to avoid case that tax integration customer is
returned in accounting context.
  • Loading branch information
lovrocolic committed Jul 10, 2024
1 parent 7cf858a commit 44ccecb
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/graphql/types/credit_notes/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def integration_syncable
end

def external_integration_id
integration_customer = object.customer&.integration_customers&.first
integration_customer = object.customer&.integration_customers&.accounting_kind&.first

return nil unless integration_customer

Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/invoices/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def integration_syncable
end

def external_integration_id
integration_customer = object.customer&.integration_customers&.first
integration_customer = object.customer&.integration_customers&.accounting_kind&.first

return nil unless integration_customer

Expand Down
2 changes: 1 addition & 1 deletion app/models/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def add_on_items
end

def should_sync_credit_note?
finalized? && customer.integration_customers.any? { |c| c.integration.sync_credit_notes }
finalized? && customer.integration_customers.accounting_kind.any? { |c| c.integration.sync_credit_notes }
end

def voidable?
Expand Down
4 changes: 4 additions & 0 deletions app/models/integration_customers/base_customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class BaseCustomer < ApplicationRecord

validates :customer_id, uniqueness: {scope: :type}

scope :accounting_kind, -> do
where(type: %w[IntegrationCustomers::NetsuiteCustomer IntegrationCustomers::XeroCustomer])
end

settings_accessors :sync_with_provider

def self.customer_type(type)
Expand Down
4 changes: 2 additions & 2 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ def mark_as_dispute_lost!(timestamp = Time.current)
end

def should_sync_invoice?
finalized? && customer.integration_customers.any? { |c| c.integration.sync_invoices }
finalized? && customer.integration_customers.accounting_kind.any? { |c| c.integration.sync_invoices }
end

def should_sync_sales_order?
finalized? && customer.integration_customers.any? { |c| c.integration.sync_sales_orders }
finalized? && customer.integration_customers.accounting_kind.any? { |c| c.integration.sync_sales_orders }
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Payment < ApplicationRecord
delegate :customer, to: :invoice

def should_sync_payment?
invoice.finalized? && customer.integration_customers.any? { |c| c.integration.sync_payments }
invoice.finalized? && customer.integration_customers.accounting_kind.any? { |c| c.integration.sync_payments }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def integration
end

def integration_customer
@integration_customer ||= customer&.integration_customers&.first
@integration_customer ||= customer&.integration_customers&.accounting_kind&.first
end

def payload(type)
Expand Down

0 comments on commit 44ccecb

Please sign in to comment.