Class: CrmNavbarCounts

Inherits:
Object
  • Object
show all
Defined in:
app/services/crm_navbar_counts.rb

Overview

Computes the badge counts shown in the CRM navbar for a given user. Single
source of truth shared by:

  • the navbar partial (initial render)
  • the badge fragment partials (replace targets for Turbo Stream broadcasts)
  • the resync endpoint

Each method returns just the integer the corresponding badge displays, so the
partials stay dumb. Manager-only "all unread" counts and watch-list-derived
scopes live here too, again to keep the partials free of conditional logic.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ CrmNavbarCounts

Returns a new instance of CrmNavbarCounts.



13
14
15
# File 'app/services/crm_navbar_counts.rb', line 13

def initialize(user)
  @user = user
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



17
18
19
# File 'app/services/crm_navbar_counts.rb', line 17

def user
  @user
end

Instance Method Details

#all_unread_emailObject



87
88
89
90
91
# File 'app/services/crm_navbar_counts.rb', line 87

def all_unread_email
  return unless manager?

  Activity.open_activities.inbound_emails.where.not(communication_id: nil).size
end

#all_unread_smsObject



35
36
37
# File 'app/services/crm_navbar_counts.rb', line 35

def all_unread_sms
  SmsMessage.inbound.unread.size if manager?
end

#all_unread_voicemailsObject



61
62
63
# File 'app/services/crm_navbar_counts.rb', line 61

def all_unread_voicemails
  CallRecord.voicemails.unread.size if manager?
end

#communications_totalObject

Communications (combined)

Aggregate "unread for me" badge for the unified Communications offcanvas
in the navbar (sms + voicemails + email). Mirrors the per-channel _total
methods so the visual badge matches what the offcanvas surfaces in its
three sections. Each underlying call is already memoised through its own
query — this just sums the integers.



100
101
102
# File 'app/services/crm_navbar_counts.rb', line 100

def communications_total
  sms_total + voicemail_total + unread_email
end

#global_sms_numbersObject



43
44
45
# File 'app/services/crm_navbar_counts.rb', line 43

def global_sms_numbers
  SmsMessage::SMS_GLOBAL_NUMBERS
end

#manager?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
# File 'app/services/crm_navbar_counts.rb', line 119

def manager?
  return @manager unless @manager.nil?

  @manager = user.try(:account).try(:is_manager?).to_b
end

#monitor_group_sms_numbers?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/services/crm_navbar_counts.rb', line 47

def monitor_group_sms_numbers?
  user.monitor_group_sms_numbers
end

#monitor_unlinked_extensions?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/services/crm_navbar_counts.rb', line 65

def monitor_unlinked_extensions?
  user.monitor_unlinked_extensions
end

#my_sms_numbersObject



39
40
41
# File 'app/services/crm_navbar_counts.rb', line 39

def my_sms_numbers
  @my_sms_numbers ||= Employee.all_active_employees_sms_numbers(rep_ids: watched_employee_ids)
end

#pinned_totalObject

Pinned items

Counts the rows of the user's currently-pinned Search (if any). Each user
has at most one pinned Search at a time (Search#remove_other_pins enforces
this), so a single JOIN-COUNT mirrors what the offcanvas list will display.



109
110
111
112
113
# File 'app/services/crm_navbar_counts.rb', line 109

def pinned_total
  SearchResult.joins(:search)
              .where(searches: { employee_id: user.id, pinned: true })
              .size
end

#sms_totalObject

SMS



21
22
23
24
25
# File 'app/services/crm_navbar_counts.rb', line 21

def sms_total
  count = unread_my_sms
  count += unread_global_sms if user.monitor_group_sms_numbers
  count
end

#unread_emailObject

Email



80
81
82
83
84
85
# File 'app/services/crm_navbar_counts.rb', line 80

def unread_email
  Activity.open_activities.inbound_emails
          .where(assigned_resource_id: watched_employee_ids)
          .where.not(communication_id: nil)
          .size
end

#unread_global_smsObject



31
32
33
# File 'app/services/crm_navbar_counts.rb', line 31

def unread_global_sms
  SmsMessage.for_numbers(SmsMessage::SMS_GLOBAL_NUMBERS).inbound.unread.size
end

#unread_my_smsObject



27
28
29
# File 'app/services/crm_navbar_counts.rb', line 27

def unread_my_sms
  SmsMessage.for_numbers(my_sms_numbers).inbound.unread.size
end

#voicemail_destination_filterObject

Mirrors what the voicemails-by-watch-list link expects: stringified watch
list ids, optionally appended with the "null" sentinel that
CallRecord#destination_filter recognises as "include unlinked extensions".



72
73
74
75
76
# File 'app/services/crm_navbar_counts.rb', line 72

def voicemail_destination_filter
  filter = watched_employee_ids.map(&:to_s)
  filter << 'null' if monitor_unlinked_extensions?
  filter
end

#voicemail_totalObject

Voicemail



53
54
55
56
57
58
59
# File 'app/services/crm_navbar_counts.rb', line 53

def voicemail_total
  if monitor_unlinked_extensions?
    CallRecord.voicemails.unread.for_destination_employees_or_unlinked(watched_employee_ids).size
  else
    CallRecord.voicemails.unread.for_destination_employees(watched_employee_ids).size
  end
end

#watched_employee_idsObject



115
116
117
# File 'app/services/crm_navbar_counts.rb', line 115

def watched_employee_ids
  @watched_employee_ids ||= user.effective_watch_list_ids
end