From 44ccecbe99f25b66115a2b1bb881577e23b6e9ec Mon Sep 17 00:00:00 2001 From: LovroColic Date: Wed, 10 Jul 2024 13:13:17 +0200 Subject: [PATCH] fix (integration-customers): adapt integration customer queries (#2269) ## 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. --- app/graphql/types/credit_notes/object.rb | 2 +- app/graphql/types/invoices/object.rb | 2 +- app/models/credit_note.rb | 2 +- app/models/integration_customers/base_customer.rb | 4 ++++ app/models/invoice.rb | 4 ++-- app/models/payment.rb | 2 +- app/services/integrations/aggregator/invoices/base_service.rb | 2 +- 7 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/graphql/types/credit_notes/object.rb b/app/graphql/types/credit_notes/object.rb index b7f579e90fd..0df230791c7 100644 --- a/app/graphql/types/credit_notes/object.rb +++ b/app/graphql/types/credit_notes/object.rb @@ -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 diff --git a/app/graphql/types/invoices/object.rb b/app/graphql/types/invoices/object.rb index b8850ae3977..bc438bc297e 100644 --- a/app/graphql/types/invoices/object.rb +++ b/app/graphql/types/invoices/object.rb @@ -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 diff --git a/app/models/credit_note.rb b/app/models/credit_note.rb index f0fcd4c09dc..e8c8796b724 100644 --- a/app/models/credit_note.rb +++ b/app/models/credit_note.rb @@ -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? diff --git a/app/models/integration_customers/base_customer.rb b/app/models/integration_customers/base_customer.rb index f8d9c3f0a8d..dceaa841fe0 100644 --- a/app/models/integration_customers/base_customer.rb +++ b/app/models/integration_customers/base_customer.rb @@ -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) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 471db03396e..342d2c52f42 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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 diff --git a/app/models/payment.rb b/app/models/payment.rb index d6f1491bef6..6570d2eba87 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -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 diff --git a/app/services/integrations/aggregator/invoices/base_service.rb b/app/services/integrations/aggregator/invoices/base_service.rb index d94dc15122e..3e7b78ae758 100644 --- a/app/services/integrations/aggregator/invoices/base_service.rb +++ b/app/services/integrations/aggregator/invoices/base_service.rb @@ -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)