Module: NetSuiteRails::Routines::CompanyContactMatch

Extended by:
CompanyContactMatch
Included in:
CompanyContactMatch
Defined in:
lib/netsuite_rails/routines/company_contact_match.rb

Instance Method Summary collapse

Instance Method Details

#match(company_customer, contact_data, update_contact_name: false, update_contact_email: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/netsuite_rails/routines/company_contact_match.rb', line 6

def match(company_customer, contact_data, update_contact_name: false, update_contact_email: false)
  search = NetSuite::Records::Contact.search({
    customerJoin: [
      {
        field: 'internalId',
        operator: 'anyOf',
        value: [
          NetSuite::Records::Customer.new(internal_id: company_customer.internal_id)
        ]
      }
    ],

    preferences: {
      page_size: 1_000
    }
  })

  match_data = {
    email: (contact_data[:email] || '').dup,
    first_name: (contact_data[:first_name] || '').dup,
    last_name: (contact_data[:last_name] || '').dup
  }

  match_data.
    values.
    each(&:strip!).
    each(&:downcase!)

  # TODO search error checking

  # try name match first; NS will throw an error if a contact is created or updated if the name already exists
  search.results.each do |contact|
    contact_first_name = contact.first_name.downcase.strip rescue ''
    contact_last_name = contact.last_name.downcase.strip rescue ''

    # if no email match & name data is present try fuzzy matching
    if match_data[:first_name] && match_data[:last_name] && !contact_first_name.empty? && !contact_last_name.empty?

      # TODO add logging for these interactions with NetSuite
      if update_contact_email && order_payload[:email].present? && contact.email != order_payload[:email]
        if !result.update(email: order_payload[:email])
          raise "error updating email on contact"
        end
      end

      # TODO consider `self.fuzzy_name_matches?(contact_first_name, contact_last_name, match_data[:first_name], match_data[:last_name])`
      if contact_first_name == match_data[:first_name] && contact_last_name == match_data[:last_name]
        return contact
      end
    end
  end

  # try email match second
  search.results.each do |contact|
    contact_first_name = contact.first_name.downcase.strip rescue ''
    contact_last_name = contact.last_name.downcase.strip rescue ''

    # match on email
    if match_data[:email] && contact.email && contact.email.downcase.strip == match_data[:email]
      if match_data[:first_name] != contact_first_name || match_data[:last_name] != contact_last_name
        # first name and/or last name did not match the input, update contact information

        if update_contact_name
          result = contact.update(
            # use the first & last name from the payload; the match_data versions have been transformed
            first_name: order_payload[:shipping_address][:firstname],
            last_name: order_payload[:shipping_address][:lastname]
          )

          unless result
            raise 'error updating name on contact placing order'
          end
        end
      end

      return contact
    end
  end

  nil
end