Module: CustomerContacts

Extended by:
ActiveSupport::Concern
Includes:
Memery
Included in:
Customer
Defined in:
app/models/concerns/customer_contacts.rb

Overview

Aggregate emails, communications, contactable override, contact management.

See Also:

Instance Method Summary collapse

Instance Method Details

#account_teamObject



126
127
128
129
130
# File 'app/models/concerns/customer_contacts.rb', line 126

def 
  team = []
  team << local_sales_rep << primary_sales_rep << secondary_sales_rep
  team.compact.uniq
end

#all_addressesObject Also known as: all_billing_addresses



108
109
110
# File 'app/models/concerns/customer_contacts.rb', line 108

def all_addresses
  Address.where(party_id: [id, parent_id].compact)
end

#all_addresses_including_contactsObject



104
105
106
# File 'app/models/concerns/customer_contacts.rb', line 104

def all_addresses_including_contacts
  Address.where(party_id: [id, *contact_ids].flatten.compact).order(:id)
end

#all_contact_pointsObject



58
59
60
# File 'app/models/concerns/customer_contacts.rb', line 58

def all_contact_points
  ContactPoint.joins(:party).merge(contacts_and_self)
end

#all_emailsObject



18
19
20
# File 'app/models/concerns/customer_contacts.rb', line 18

def all_emails
  ContactPoint.emails.where(party_id: all_my_active_party_ids).pluck(:detail).uniq
end

#all_my_active_party_idsObject



53
54
55
# File 'app/models/concerns/customer_contacts.rb', line 53

def all_my_active_party_ids
  ([id] + contacts.active.order(:full_name).pluck(:id)).compact.uniq
end

#all_my_party_idsObject



49
50
51
# File 'app/models/concerns/customer_contacts.rb', line 49

def all_my_party_ids
  ([id] + contacts.pluck(:id)).compact.uniq
end

#all_notification_channelsObject



138
139
140
# File 'app/models/concerns/customer_contacts.rb', line 138

def all_notification_channels
  NotificationChannel.for_customer(self)
end

#all_phone_numbersObject



22
23
24
# File 'app/models/concerns/customer_contacts.rb', line 22

def all_phone_numbers
  ContactPoint.dialable.where(party_id: all_my_active_party_ids).pluck(:detail).uniq
end


114
115
116
# File 'app/models/concerns/customer_contacts.rb', line 114

def all_related_addresses
  ([billing_address, shipping_address, mailing_address] + addresses.to_a).flatten.compact.uniq
end


10
11
12
# File 'app/models/concerns/customer_contacts.rb', line 10

def all_related_communications
  Query::CustomerRelatedCommunications.call(self)
end

#contact_select_options(active_only: false) ⇒ Object

:reek:BooleanParameter :reek:ControlParameter



26
27
28
29
30
# File 'app/models/concerns/customer_contacts.rb', line 26

def contact_select_options(active_only: false) # :reek:BooleanParameter :reek:ControlParameter
  contacts_to_use = contacts.order(:inactive)
  contacts_to_use = contacts_to_use.active if active_only
  contacts_to_use.pluck(:full_name, :id)
end

#contactable?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'app/models/concerns/customer_contacts.rb', line 44

def contactable?
  contact_points.exists? ||
    ContactPoint.where(party_id: contacts.active.select(:id)).exists?
end

#contacts_and_selfObject



40
41
42
# File 'app/models/concerns/customer_contacts.rb', line 40

def contacts_and_self
  Party.where(id: all_my_active_party_ids)
end

#contacts_and_self_callable_contact_pointsObject



62
63
64
# File 'app/models/concerns/customer_contacts.rb', line 62

def contacts_and_self_callable_contact_points
  all_contact_points.voice_callable
end

#contacts_and_self_callable_options_for_selectObject



70
71
72
73
74
75
# File 'app/models/concerns/customer_contacts.rb', line 70

def contacts_and_self_callable_options_for_select
  contacts_and_self_callable_contact_points.includes(:party).map do |contact_point|
    party_label = party_label_for_contact_point(contact_point)
    ["#{contact_point} (#{party_label})", contact_point.id]
  end
end

#contacts_and_self_contact_points_by_category(category) ⇒ Object



66
67
68
# File 'app/models/concerns/customer_contacts.rb', line 66

def contacts_and_self_contact_points_by_category(category)
  ContactPoint.where(category: category).where(party_id: all_my_active_party_ids)
end

#contacts_and_self_contact_points_by_category_options_for_select(category) ⇒ Object



77
78
79
80
81
82
# File 'app/models/concerns/customer_contacts.rb', line 77

def contacts_and_self_contact_points_by_category_options_for_select(category)
  contacts_and_self_contact_points_by_category(category).includes(:party).map do |contact_point|
    party_label = party_label_for_contact_point(contact_point, capitalize: true)
    ["#{contact_point} (#{party_label})", contact_point.id]
  end
end

#emailObject



96
97
98
# File 'app/models/concerns/customer_contacts.rb', line 96

def email
  super || contacts.active.first&.email || contacts.active.last&.email
end

#generate_guest_accountObject



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/concerns/customer_contacts.rb', line 142

def 
  return { success: false } if .present? || email.nil?

  acct = 
  acct. = Account.(email)
  acct.email = email
  acct.require_password = false
  if acct.save
    { success: true, account: acct }
  else
    { success: false, errors: acct.errors.full_messages }
  end
end

#guess_best_callable_contact_pointObject



84
85
86
# File 'app/models/concerns/customer_contacts.rb', line 84

def guess_best_callable_contact_point
  contacts_and_self_callable_contact_points.first
end

#guess_best_contact_point_by_category(category) ⇒ Object



88
89
90
# File 'app/models/concerns/customer_contacts.rb', line 88

def guess_best_contact_point_by_category(category)
  contacts_and_self_contact_points_by_category(category).first
end

#lead_score_pass?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/concerns/customer_contacts.rb', line 36

def lead_score_pass?
  lead_verification_score.to_i > 50
end

#obfuscated_emailObject



100
101
102
# File 'app/models/concerns/customer_contacts.rb', line 100

def obfuscated_email
  email&.gsub(/(?<=.{2}).*@.*(?=\S{2})/, '****@****')
end

#primary_contactObject



92
93
94
# File 'app/models/concerns/customer_contacts.rb', line 92

def primary_contact
  person? ? self : contacts.first
end

#primary_repObject



118
119
120
# File 'app/models/concerns/customer_contacts.rb', line 118

def primary_rep
  primary_sales_rep
end

#secondary_repObject



122
123
124
# File 'app/models/concerns/customer_contacts.rb', line 122

def secondary_rep
  secondary_sales_rep
end

#self_and_contacts_party_ids_arrObject



32
33
34
# File 'app/models/concerns/customer_contacts.rb', line 32

def self_and_contacts_party_ids_arr
  [id] + contact_ids
end

#sms_enabled_numbersObject



14
15
16
# File 'app/models/concerns/customer_contacts.rb', line 14

def sms_enabled_numbers
  ContactPoint.where(party_id: [id] + contact_ids).sms_numbers.order(:detail).map(&:formatted_for_sms)
end