Module: ContactPointsHelper

Defined in:
app/helpers/contact_points_helper.rb

Instance Method Summary collapse

Instance Method Details

#can_click_to_call?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'app/helpers/contact_points_helper.rb', line 196

def can_click_to_call?
  @context_user.employee_phone_status.try(:switchvox_account_id)
end

#contact_point_category_icon(category, options = {}) ⇒ Object



4
5
6
# File 'app/helpers/contact_points_helper.rb', line 4

def contact_point_category_icon(category, options = {})
  fa_icon(contact_point_icon_for_category(category), options)
end

#contact_point_delete_button(contact_point) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/contact_points_helper.rb', line 24

def contact_point_delete_button(contact_point)
  if contact_point.persisted?
    link_to(fa_icon('trash'), contact_point_path(contact_point),
            data: {
              turbo_confirm: 'Are you sure you want to delete this contact point?',
              turbo_method: :delete,
              'class-to-remove': '.contact_point'
            },
            class: 'btn btn-outline-primary block-ui-on-click')
  else
    link_to(fa_icon('trash'), '#', class: 'btn btn-outline-primary trash-remove', data: { 'class-to-remove': '.contact_point' })
  end
end

#contact_point_email_preferences(contact_point) ⇒ Object



47
48
49
50
51
52
53
54
# File 'app/helpers/contact_points_helper.rb', line 47

def contact_point_email_preferences(contact_point)
  return if contact_point.new_record?
  return unless contact_point.email?

  link_to fa_icon('microphone-slash', title: 'Email preferences'),
          by_email_email_preferences_path(email: contact_point.detail, return_path: request.fullpath),
          class: 'btn btn-outline-primary'
end

#contact_point_icon_for_category(category) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'app/helpers/contact_points_helper.rb', line 8

def contact_point_icon_for_category(category)
  sym_matrix = {
    fax: 'fax',
    email: 'envelope',
    cell: 'mobile-alt',
    phone: 'phone'
  }
  icn = sym_matrix[category.to_sym] || 'question'
end

#contact_point_mark_as_working(contact_point, btn_class = 'btn btn-outline-primary btn-success', label = nil, return_path = request.fullpath) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/helpers/contact_points_helper.rb', line 56

def contact_point_mark_as_working(contact_point, btn_class = 'btn btn-outline-primary btn-success', label = nil, return_path = request.fullpath)
  return if contact_point.new_record?
  return unless contact_point.email? && contact_point.failed_before?

  link_to fa_icon('check', text: label, title: 'Mark email as working again'),
          mark_as_working_contact_point_path(contact_point, return_path: return_path),
          class: btn_class
end

#contact_point_options(cp, communication_options: {}, options: {}) {|OpenStruct.new(display_hash).freeze| ... } ⇒ Object

Creates actions and options for contact points based on their state
will yield a structure you must consume and render

Yields:

  • (OpenStruct.new(display_hash).freeze)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/helpers/contact_points_helper.rb', line 134

def contact_point_options(cp, communication_options: {}, options: {})
  return unless cp
  return if cp.detail.blank?

  display_title = cp.formatted_dial_string
  return unless display_title.present?

  display_title = tag.del(display_title) if cp.call_blocked? # Only when calls are blocked put this treatment
  # Because hash are by reference
  options = options.dup

  return_path = options.delete(:return_path)
  return_path ||= polymorphic_path(cp.party) if cp.party
  return_path ||= cms_link(request.path) unless request.xhr?

  link_class = options.delete(:link_class)
  link = nil
  preferences_link = ''
  mark_as_working_link = ''
  actions = []

  if cp.transmittable?
    communication_params = { recipient_contact_point_id: cp.id, return_path: return_path }.merge(communication_options)
    actions << contact_point_mark_as_working(cp, "text-success #{link_class}") if cp.email? && cp.failed_before?

    actions << link_to(fa_icon((cp.fax? ? 'fax' : 'envelope')),
                       new_communication_path(communication_params),
                       class: link_class)
    if cp.email?
      actions << link_to(fa_icon('gear', title: 'Email preferences'),
                         by_email_email_preferences_path(email: cp.detail, return_path: return_path),
                         class: "#{'text-danger' if cp.email_blocked?} #{link_class}")
    end

  elsif cp.callable?
    actions << sip_dial_link(fa_icon('headset', title: 'Call'), cp.dial_string, party_id: cp.party_id, class: link_class) unless cp.call_blocked?
    if cp.sms_enabled? && current_user&.sms_enabled? && !cp.text_blocked?
      actions << link_to(fa_icon('comment-sms', title: 'Send SMS/Text'),
                  new_sms_message_path(sms_message: { recipient: cp.dial_string, recipient_party_id: cp.party_id },
                  return_path: polymorphic_path(cp.party, tab: "sms")),
                  class: link_class)
    end

    actions << link_to(fa_icon('gear', title: 'Do Not Call/Text/Fax Preferences'),
                        new_do_not_call_path(do_not_call: { number: cp.detail }, return_path: return_path),
                        class: "#{'text-danger' if cp.dnc_blocked?} #{link_class}")

  elsif cp.website?
    actions << link_to(fa_icon('up-right-from-square', title: 'Open site in new window'), cp.detail, target: '_blank', rel: "noopener noreferrer", class: link_class)
  end

  display_title << (:span, cp.notes, class: 'ms-2 badge rounded-pill bg-secondary') if cp.notes.present?

  display_hash = {
    category: cp.category,
    title: display_title.html_safe,
    actions: actions
  }

  yield OpenStruct.new(display_hash).freeze
end

#contact_point_phone_preferences(contact_point) ⇒ Object



38
39
40
41
42
43
44
45
# File 'app/helpers/contact_points_helper.rb', line 38

def contact_point_phone_preferences(contact_point)
  return if contact_point.new_record?
  return unless contact_point.can_be_dialed?

  link_to fa_icon('microphone-slash', title: 'Put on Do Not Call List'),
          new_do_not_call_path(do_not_call: { number: contact_point.detail, country_code: contact_point.country_code }, return_path: polymorphic_path(contact_point.party)),
          class: 'btn btn-outline-primary'
end

#contact_point_status(status) ⇒ Object



18
19
20
21
22
# File 'app/helpers/contact_points_helper.rb', line 18

def contact_point_status(status)
   :span, class: "contact_point_status #{status}" do
    fa_icon('circle-arrow-right', text: status)
  end
end

#display_contact_point(cp, communication_options = {}, in_options = {}) ⇒ Object

Legacy style contact point display
Use contact_point_options for a better option which yields a struct
you can render properly in a view or partial



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/contact_points_helper.rb', line 68

def display_contact_point(cp, communication_options = {}, in_options = {})
  return unless cp
  return if cp.detail.blank?

  options = in_options.dup
  embed_icon = options.delete(:embed_icon).to_b
  return_path = options.delete(:return_path)
  return_path ||= polymorphic_path(cp.party) if cp.party
  return_path ||= cms_link(request.path) unless request.xhr?
  display_title = cp.formatted_dial_string
  styles = [options.delete(:style)]
  info = [cp.notes]
  if cp.email?
    if cp.failed_before?
      styles << 'color:red'
      display_title += fa_icon('triangle-exclamation')
      info << cp.system_notes
    elsif cp.working?
      styles << 'color:green'
    end
  end
  if options.delete(:highlight)
    display_title = fa_icon('bullseye', text: display_title)
    styles << 'font-weight:bolder'
  end

  return unless display_title.present?

  display_title = display_title.html_safe

  link = nil
  preferences_link = ''
  mark_as_working_link = ''
  display_title = if embed_icon
                    contact_point_category_icon(cp.category, text: display_title, style: styles.compact.join(';').presence)
                  else
                     :span, display_title,
                                                style: styles.compact.join(';').presence
                  end
  display_title << (:span, cp.notes, class: 'ms-1 badge rounded-pill bg-secondary') if cp.notes.present?

  if cp.transmittable?
    display_title = link_to display_title, new_communication_path({ recipient_contact_point_id: cp.id, return_path: return_path }.merge(communication_options))
    if cp.email?
      preferences_link = link_to(fa_icon('gears', text: options[:preferences_label], title: 'Email preferences'),
                                 by_email_email_preferences_path(email: cp.detail, return_path: return_path))
    end
    mark_as_working_link = contact_point_mark_as_working(cp) if cp.email? && cp.failed_before?
  elsif cp.callable?
    display_title = sip_dial_link(display_title, cp.dial_string, party_id: cp.party_id)
  elsif cp.website?
    display_title = link_to(cp.detail, cp.detail, target: '_blank', rel: "noopener noreferrer")
  end

  copy_link = clipboard_copy(cp.detail, button_class: 'btn btn-link p-0')

  category_label = cp.category.capitalize
  if (info = info.map(&:presence).compact).present?
    category_label << " (#{info.join(', ')})"
  end
  value_display = [display_title, preferences_link, mark_as_working_link, copy_link].compact.join(' ').html_safe
  attr_display(category_label, value_display, true, options)
end

#party_contact_points_panel(parties_or_hash, options = {}) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'app/helpers/contact_points_helper.rb', line 217

def party_contact_points_panel(parties_or_hash, options = {})
  return unless parties_or_hash.present?

  render_hash = {}
  if parties_or_hash.is_a?(Hash)
    render_hash = parties_or_hash
  elsif %(Customer Contact Supplier).include?(parties_or_hash.class.name)
    render_hash = { parties_or_hash.full_name => parties_or_hash }
  elsif parties_or_hash.respond_to?(:to_a)
    render_hash = parties_or_hash.index_by { |p| p.full_name }
  end

  # Sort entries: active contacts first, then inactive ones
  sorted_entries = render_hash.sort_by do |_title, party_or_hash|
    party = party_or_hash.is_a?(Hash) ? party_or_hash[:party] : party_or_hash
    party&.inactive ? 1 : 0
  end

  capture do
    (:div, id: 'contacts_panel', class: 'row') do
      sorted_entries.each do |title, party_or_hash|
        if party_or_hash.is_a?(Hash)
          party = party_or_hash.delete(:party)
          render_options = { title: title }.merge(party_or_hash)
        else
          party = party_or_hash
          render_options = options.merge(title: title)
        end

        concat render(partial: 'crm/shared/party_contact_points_panel', locals: {
                        party: party,
                        options: render_options,
                        wrapper_class: options[:wrapper_class]
                      })
      end
    end
  end
end

#setup_context_object_for_contact_points(context_object) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'app/helpers/contact_points_helper.rb', line 256

def setup_context_object_for_contact_points(context_object)
  context_object.tap do |co|
    # Build new contact points if needed
    [ContactPoint::EMAIL, ContactPoint::PHONE].each do |mc|
      co.contact_points.build(category: mc) unless co.contact_points.detect { |cp| cp.category == mc }
    end
    co.contact_points.build if co.contact_points.all?(&:persisted?)
  end
end


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/helpers/contact_points_helper.rb', line 200

def sip_dial_link(display_title, phone, in_additional_params = nil)
  return unless current_user

  additional_params = (in_additional_params || {}).dup
  link_class = additional_params.delete(:class)

  case current_user.employee_phone_status&.click_to_call_integration&.to_sym
  when :api
    link_to(display_title, outbound_calls_path(destination: phone, **additional_params), data: { turbo_method: :post }, class: link_class)
  when :callto, :tel
    # callto does not get intercepted each time by chrome but yields the same results
    link_to(display_title, "#{current_user.employee_phone_status.click_to_call_integration}:#{phone.split('x').first}", class: link_class)
  else
    (:span, display_title, title: 'Current user not setup for click to call')
  end
end