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



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

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

#all_addressesObject Also known as: all_billing_addresses



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

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

#all_addresses_including_contactsObject



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

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

#all_contact_pointsObject



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

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



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

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

#all_my_party_idsObject



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

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

#all_notification_channelsObject



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

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


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

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



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

def contact_select_options(active_only: false)
  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)


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

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

#contacts_and_selfObject



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

def contacts_and_self
  Party.where(id: all_my_active_party_ids)
end

#contacts_and_self_callable_contact_pointsObject



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

def contacts_and_self_callable_contact_points
  all_contact_points.voice_callable
end

#contacts_and_self_callable_options_for_selectObject



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

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



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

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



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

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



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

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

#generate_guest_accountObject



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

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



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

def guess_best_callable_contact_point
  contacts_and_self_callable_contact_points.first
end

#guess_best_contact_point_by_category(category) ⇒ Object



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

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

#lead_score_pass?Boolean

Returns:

  • (Boolean)


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

def lead_score_pass?
  lead_verification_score.to_i > 50
end

#obfuscated_emailObject



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

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

#primary_contactObject



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

def primary_contact
  person? ? self : contacts.first
end

#primary_repObject



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

def primary_rep
  primary_sales_rep
end

#secondary_repObject



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

def secondary_rep
  secondary_sales_rep
end

#self_and_contacts_party_ids_arrObject



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

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