Class: QuickbooksCustomerByCompanyResolver

Inherits:
Object
  • Object
show all
Includes:
QuickbooksCustomerHelper
Defined in:
app/resolver/quickbooks_customer_by_company_resolver.rb

Overview

QuickbooksCustomerByCompanyResolver handles the creation, updating, and integration of QuickBooks customer data with the application’s CustomerByCompany records. This resolver processes customer data from QuickBooks API and maintains company-specific customer settings and relationships.

The resolver handles three primary scenarios:

  1. Finding and updating existing integrated CustomerByCompany records

  2. Finding matching non-integrated CustomerByCompany records and integrating them

  3. Creating new CustomerByCompany records when no match is found

CustomerByCompany records store company-specific settings for customers, such as payment terms, currency preferences, and active status.

Examples:

resolver = QuickbooksCustomerByCompanyResolver.new(
  company_id: 123,
  customer_id: 456,
  quickbooks_customer: qb_customer_data
)
result = resolver.resolve
# => { action: 'create', customer_by_company: #<CustomerByCompany customer_key: 789, ...> }

Defined Under Namespace

Classes: Actions

Constant Summary collapse

USER_NAME =
'SYSTEM'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QuickbooksCustomerHelper

#check_and_create_member_customer, #check_and_set_customer_hierarchy, #external_reference_code, #extract_shipping_address_from_quickbooks_customer, #find_currency_id, #find_payment_term_id, #find_type, #format_known_as, #formatted_code

Methods included from CustomerHierarchyManagement

#assign_customer_hierarchy, #billing_shipping_addresses_match?, #create_dc_customer_for_shipping_address, #create_member_customer_for_shipping_address, #determine_hierarchy_attributes, #find_customer_with_same_address, #shipping_address_valid?

Methods included from AddressHelper

#canadian_postal_code?, #clean_city, #normalize_postal_code, #parse_address_line, #parse_address_parts, #us_zip_code?, #valid_city?, #valid_state?

Constructor Details

#initialize(company_id:, customer_id:, quickbooks_customer:) ⇒ QuickbooksCustomerByCompanyResolver

Initializes a new QuickbooksCustomerByCompanyResolver

Parameters:

  • company_id (Integer)

    ID of the company in the application

  • customer_id (Integer)

    ID of the customer in the application

  • quickbooks_customer (Hash)

    Customer data from QuickBooks API



44
45
46
47
48
# File 'app/resolver/quickbooks_customer_by_company_resolver.rb', line 44

def initialize(company_id:, customer_id:, quickbooks_customer:)
  @company_id = company_id
  @customer_id = customer_id
  @quickbooks_customer = quickbooks_customer.with_indifferent_access
end

Instance Attribute Details

#company_idObject (readonly)

Returns the value of attribute company_id.



36
37
38
# File 'app/resolver/quickbooks_customer_by_company_resolver.rb', line 36

def company_id
  @company_id
end

#customer_idObject (readonly)

Returns the value of attribute customer_id.



36
37
38
# File 'app/resolver/quickbooks_customer_by_company_resolver.rb', line 36

def customer_id
  @customer_id
end

#quickbooks_customerObject (readonly)

Returns the value of attribute quickbooks_customer.



36
37
38
# File 'app/resolver/quickbooks_customer_by_company_resolver.rb', line 36

def quickbooks_customer
  @quickbooks_customer
end

Instance Method Details

#resolveHash

Processes the QuickBooks customer data and performs the necessary actions to integrate it with the application’s CustomerByCompany data model

Returns:

  • (Hash)

    A result hash containing the action performed and the CustomerByCompany record

Raises:

  • (StandardError)

    If an error occurs during processing



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/resolver/quickbooks_customer_by_company_resolver.rb', line 55

def resolve
  ActiveRecord::Base.transaction do
    if find_integrated_customer_by_company
      update_customer_by_company
      { action: Actions::UPDATE, customer_by_company: @customer_by_company }
    elsif find_customer_by_company_to_integrate
      integrate_customer_by_company
      update_customer_by_company
      { action: Actions::INTEGRATE, customer_by_company: @customer_by_company }
    elsif create_customer_by_company
      integrate_customer_by_company
      { action: Actions::CREATE, customer_by_company: @customer_by_company }
    end
  end
rescue StandardError => e
  Rails.logger.error "QuickBooks CustomerByCompany resolve failed: #{e.message}"
  raise e
end