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!)
search.results.each do |contact|
contact_first_name = contact.first_name.downcase.strip rescue ''
contact_last_name = contact.last_name.downcase.strip rescue ''
if match_data[:first_name] && match_data[:last_name] && !contact_first_name.empty? && !contact_last_name.empty?
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
if contact_first_name == match_data[:first_name] && contact_last_name == match_data[:last_name]
return contact
end
end
end
search.results.each do |contact|
contact_first_name = contact.first_name.downcase.strip rescue ''
contact_last_name = contact.last_name.downcase.strip rescue ''
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
if update_contact_name
result = contact.update(
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
|