summaryrefslogtreecommitdiff
path: root/app/models/customer_relations/contact_state_counts.rb
blob: 31c95e166bb495228af023848958d8bfe7553b59 (plain)
1
2
3
4
5
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
# frozen_string_literal: true

module CustomerRelations
  class ContactStateCounts
    include Gitlab::Utils::StrongMemoize

    attr_reader :group

    def self.declarative_policy_class
      'CustomerRelations::ContactPolicy'
    end

    def initialize(current_user, group, params)
      @current_user = current_user
      @group = group
      @params = params
    end

    # Define method for each state
    ::CustomerRelations::Contact.states.each_key do |state|
      define_method(state) { counts[state] }
    end

    def all
      counts.values.sum
    end

    private

    attr_reader :current_user, :params

    def counts
      strong_memoize(:counts) do
        Hash.new(0).merge(counts_by_state)
      end
    end

    def counts_by_state
      ::Crm::ContactsFinder.counts_by_state(current_user, params.merge({ group: group }))
    end
  end
end